Add support for Java 10

Closes gh-646
This commit is contained in:
Stephane Nicoll
2018-04-17 10:58:51 +02:00
parent 9ab9c89c40
commit 7ac71905ed
5 changed files with 96 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -27,25 +27,29 @@ import io.spring.initializr.util.Version;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* Handle corner cases when Java9 is required. * Validate that the requested java version is compatible with the chosen Spring Boot
* generation and adapt the request if necessary.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Component @Component
class Java9RequestPostProcessor implements ProjectRequestPostProcessor { class JavaVersionRequestPostProcessor implements ProjectRequestPostProcessor {
private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1"); private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1");
private static final Version VERSION_2_0_1 = Version.parse("2.0.1.RELEASE");
private static final List<String> UNSUPPORTED_LANGUAGES = Arrays.asList("groovy", private static final List<String> UNSUPPORTED_LANGUAGES = Arrays.asList("groovy",
"kotlin"); "kotlin");
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) { InitializrMetadata metadata) {
Version requestVersion = Version.safeParse(request.getBootVersion()); Integer javaGeneration = determineJavaGeneration(request.getJavaVersion());
if (!"9".equals(request.getJavaVersion())) { if (javaGeneration == null) {
return; return;
} }
Version requestVersion = Version.safeParse(request.getBootVersion());
// Not supported for Spring Boot 1.x // Not supported for Spring Boot 1.x
if (VERSION_2_0_0_M1.compareTo(requestVersion) > 0) { if (VERSION_2_0_0_M1.compareTo(requestVersion) > 0) {
request.setJavaVersion("1.8"); request.setJavaVersion("1.8");
@@ -54,6 +58,20 @@ class Java9RequestPostProcessor implements ProjectRequestPostProcessor {
if (UNSUPPORTED_LANGUAGES.contains(request.getLanguage())) { if (UNSUPPORTED_LANGUAGES.contains(request.getLanguage())) {
request.setJavaVersion("1.8"); request.setJavaVersion("1.8");
} }
// 10 support only as of 2.0.1
if (javaGeneration == 10 && VERSION_2_0_1.compareTo(requestVersion) > 0) {
request.setJavaVersion("1.8");
}
}
private Integer determineJavaGeneration(String javaVersion) {
try {
int generation = Integer.valueOf(javaVersion);
return ((generation > 8 && generation <=10) ? generation : null);
}
catch (NumberFormatException e) {
return null;
}
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ class SpringBoot2RequestPostProcessor extends AbstractProjectRequestPostProcesso
private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1"); private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1");
private static final List<String> VALID_VERSIONS = Arrays.asList("1.8", "9"); private static final List<String> VALID_VERSIONS = Arrays.asList("1.8", "9", "10");
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, public void postProcessAfterResolution(ProjectRequest request,

View File

@@ -1340,6 +1340,8 @@ initializr:
id: war id: war
default: false default: false
javaVersions: javaVersions:
- id: 10
default: false
- id: 9 - id: 9
default: false default: false
- id: 1.8 - id: 1.8

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@ import io.spring.initializr.generator.ProjectRequest;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests for {@link Java9RequestPostProcessor}. * Tests for {@link JavaVersionRequestPostProcessor}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class Java9RequestPostProcessorTests extends AbstractRequestPostProcessorTests { public class JavaVersionRequestPostProcessorTests extends AbstractRequestPostProcessorTests {
@Test @Test
public void java9CannotBeUsedWithSpringBoot1Maven() { public void java9CannotBeUsedWithSpringBoot1Maven() {
@@ -94,4 +94,53 @@ public class Java9RequestPostProcessorTests extends AbstractRequestPostProcessor
generateGradleBuild(request).hasJavaVersion("9"); generateGradleBuild(request).hasJavaVersion("9");
} }
@Test
public void java10CannotBeUsedWithSpringBoot1Maven() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("1.5.8.RELEASE");
request.setJavaVersion("10");
generateMavenPom(request).hasJavaVersion("1.8");
}
@Test
public void java10CannotBeUsedWithSpringBoot1Gradle() {
ProjectRequest request = createProjectRequest("data-jpa");
request.setBootVersion("1.99.99.BUILD-SNAPSHOT");
request.setJavaVersion("10");
generateGradleBuild(request).hasJavaVersion("1.8");
}
@Test
public void java10CannotBeUsedWithSpringBoot200Maven() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("2.0.0.RELEASE");
request.setJavaVersion("10");
generateMavenPom(request).hasJavaVersion("1.8");
}
@Test
public void java10CannotBeUsedWithSpringBoot200Gradle() {
ProjectRequest request = createProjectRequest("data-jpa");
request.setBootVersion("2.0.0.RELEASE");
request.setJavaVersion("10");
generateGradleBuild(request).hasJavaVersion("1.8");
}
@Test
public void java10CanBeUsedWithSpringBoot2Maven() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("2.0.1.RELEASE");
request.setJavaVersion("10");
generateMavenPom(request).hasJavaVersion("10");
}
@Test
public void java10CanBeUsedWithSpringBoot2Gradle() {
ProjectRequest request = createProjectRequest("data-jpa");
request.setBootVersion("2.0.2.RELEASE");
request.setJavaVersion("10");
generateGradleBuild(request).hasJavaVersion("10");
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -59,4 +59,20 @@ public class SpringBoot2RequestPostProcessorTests
generateGradleBuild(request).hasJavaVersion("9"); generateGradleBuild(request).hasJavaVersion("9");
} }
@Test
public void java10CanBeUsedMaven() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("2.1.0.BUILD-SNAPSHOT");
request.setJavaVersion("10");
generateMavenPom(request).hasJavaVersion("10");
}
@Test
public void java10CanBeUsedGradle() {
ProjectRequest request = createProjectRequest("data-jpa");
request.setBootVersion("2.0.2.RELEASE");
request.setJavaVersion("10");
generateGradleBuild(request).hasJavaVersion("10");
}
} }