Fix version format for Gradle versions

Closes gh-777
This commit is contained in:
Stephane Nicoll
2018-11-27 17:57:36 +01:00
parent 0ed8aea677
commit 9c5dfb34e9
12 changed files with 64 additions and 39 deletions
@@ -516,7 +516,7 @@ public class ProjectGenerator {
private String computeVersionProperty(ProjectRequest request, private String computeVersionProperty(ProjectRequest request,
VersionProperty property) { VersionProperty property) {
if (isGradleBuild(request)) { if (isGradleBuild(request) && property.isInternal()) {
return property.toCamelCaseFormat(); return property.toCamelCaseFormat();
} }
return property.toStandardFormat(); return property.toStandardFormat();
@@ -243,11 +243,11 @@ public class ProjectRequest extends BasicProjectRequest {
() -> "UTF-8"); () -> "UTF-8");
this.buildProperties.getMaven().put("project.reporting.outputEncoding", this.buildProperties.getMaven().put("project.reporting.outputEncoding",
() -> "UTF-8"); () -> "UTF-8");
this.buildProperties.getVersions().put(new VersionProperty("java.version"), this.buildProperties.getVersions().put(VersionProperty.of("java.version"),
this::getJavaVersion); this::getJavaVersion);
if ("kotlin".equals(getLanguage())) { if ("kotlin".equals(getLanguage())) {
this.buildProperties.getVersions() this.buildProperties.getVersions()
.put(new VersionProperty("kotlin.version"), kotlinVersion); .put(VersionProperty.of("kotlin.version"), kotlinVersion);
} }
} }
} }
@@ -109,7 +109,7 @@ public class BillOfMaterials {
} }
public void setVersionProperty(String versionPropertyName) { public void setVersionProperty(String versionPropertyName) {
setVersionProperty(new VersionProperty(versionPropertyName)); setVersionProperty(VersionProperty.of(versionPropertyName));
} }
/** /**
@@ -246,7 +246,7 @@ public class InitializrMetadata {
String versionProperty) { String versionProperty) {
BillOfMaterials bom = BillOfMaterials.create("org.springframework.boot", BillOfMaterials bom = BillOfMaterials.create("org.springframework.boot",
"spring-boot-dependencies", bootVersion); "spring-boot-dependencies", bootVersion);
bom.setVersionProperty(new VersionProperty(versionProperty)); bom.setVersionProperty(VersionProperty.of(versionProperty));
bom.setOrder(100); bom.setOrder(100);
return bom; return bom;
} }
@@ -31,14 +31,46 @@ import org.springframework.util.StringUtils;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class VersionProperty implements Serializable, Comparable<VersionProperty> { public final class VersionProperty implements Serializable, Comparable<VersionProperty> {
private static final List<Character> SUPPORTED_CHARS = Arrays.asList('.', '-'); private static final List<Character> SUPPORTED_CHARS = Arrays.asList('.', '-');
private final String property; private final String property;
public VersionProperty(String property) { private final boolean internal;
private VersionProperty(String property, boolean internal) {
this.property = validateFormat(property); this.property = validateFormat(property);
this.internal = internal;
}
/**
* Create a {@link VersionProperty}.
* @param property the name of the property
* @param internal whether the property is internal and can be tuned according to the
* build system
* @return a version property
*/
public static VersionProperty of(String property, boolean internal) {
return new VersionProperty(property, internal);
}
/**
* Create an internal {@link VersionProperty}.
* @param property the name of the property
* @return a version property whose format can be tuned according to the build system
*/
public static VersionProperty of(String property) {
return of(property, true);
}
/**
* Specify if the property is internally defined and can be tuned according to the
* build system.
* @return {@code true} if the property is defined within the scope of this project
*/
public boolean isInternal() {
return this.internal;
} }
/** /**
@@ -78,15 +78,10 @@ configurations {
providedRuntime providedRuntime
} }
{{/providedDependencies}} {{/providedDependencies}}
{{#buildPropertiesVersions}}
{{^buildPropertiesVersions.empty}} ext['{{key}}'] = '{{value}}'
ext {
{{#buildPropertiesVersions}}
{{key}} = '{{value}}'
{{/buildPropertiesVersions}} {{/buildPropertiesVersions}}
}
{{/buildPropertiesVersions.empty}}
dependencies { dependencies {
{{#compileDependencies}} {{#compileDependencies}}
{{gradleCompileConfig}}('{{groupId}}:{{artifactId}}{{#version}}:{{version}}{{/version}}{{#type}}@{{type}}{{/type}}') {{gradleCompileConfig}}('{{groupId}}:{{artifactId}}{{#version}}:{{version}}{{/version}}{{#type}}@{{type}}{{/type}}')
@@ -105,10 +105,10 @@ public class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
@Test @Test
public void versionOverride() { public void versionOverride() {
ProjectRequest request = createProjectRequest("web"); ProjectRequest request = createProjectRequest("web");
request.getBuildProperties().getVersions().put(
VersionProperty.of("spring-foo.version", false), () -> "0.1.0.RELEASE");
request.getBuildProperties().getVersions() request.getBuildProperties().getVersions()
.put(new VersionProperty("spring-foo.version"), () -> "0.1.0.RELEASE"); .put(VersionProperty.of("spring-bar.version"), () -> "0.2.0.RELEASE");
request.getBuildProperties().getVersions()
.put(new VersionProperty("spring-bar.version"), () -> "0.2.0.RELEASE");
ProjectAssert project = generateProject(request); ProjectAssert project = generateProject(request);
project.sourceCodeAssert(this.fileName).equalsTo(new ClassPathResource( project.sourceCodeAssert(this.fileName).equalsTo(new ClassPathResource(
"project/" + this.build + "/version-override-" + this.assertFileName)); "project/" + this.build + "/version-override-" + this.assertFileName));
@@ -818,7 +818,7 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
public void buildPropertiesMaven() { public void buildPropertiesMaven() {
ProjectRequest request = createProjectRequest("web"); ProjectRequest request = createProjectRequest("web");
request.getBuildProperties().getMaven().put("name", () -> "test"); request.getBuildProperties().getMaven().put("name", () -> "test");
request.getBuildProperties().getVersions().put(new VersionProperty("foo.version"), request.getBuildProperties().getVersions().put(VersionProperty.of("foo.version"),
() -> "1.2.3"); () -> "1.2.3");
request.getBuildProperties().getGradle().put("ignore.property", () -> "yes"); request.getBuildProperties().getGradle().put("ignore.property", () -> "yes");
@@ -830,12 +830,16 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
public void buildPropertiesGradle() { public void buildPropertiesGradle() {
ProjectRequest request = createProjectRequest("web"); ProjectRequest request = createProjectRequest("web");
request.getBuildProperties().getGradle().put("name", () -> "test"); request.getBuildProperties().getGradle().put("name", () -> "test");
request.getBuildProperties().getVersions().put(new VersionProperty("foo.version"), request.getBuildProperties().getVersions()
() -> "1.2.3"); .put(VersionProperty.of("foo.version", false), () -> "1.2.3");
request.getBuildProperties().getVersions()
.put(VersionProperty.of("internal.version"), () -> "4.5.6");
request.getBuildProperties().getMaven().put("ignore.property", () -> "yes"); request.getBuildProperties().getMaven().put("ignore.property", () -> "yes");
generateGradleBuild(request).contains("name = 'test'").contains("ext {") generateGradleBuild(request).contains("name = 'test'")
.contains("fooVersion = '1.2.3'").doesNotContain("ignore.property"); .contains("ext['foo.version'] = '1.2.3'")
.contains("ext['internalVersion'] = '4.5.6'")
.doesNotContain("ignore.property");
} }
@Test @Test
@@ -39,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class ProjectRequestResolverTests { public class ProjectRequestResolverTests {
private static final VersionProperty VERSION_PROPERTY = new VersionProperty( private static final VersionProperty VERSION_PROPERTY = VersionProperty
"java.version"); .of("java.version");
private InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() private InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("test", "web", "security", "data-jpa").build(); .addDependencyGroup("test", "web", "security", "data-jpa").build();
@@ -34,25 +34,25 @@ public class VersionPropertyTests {
@Test @Test
public void testStandardProperty() { public void testStandardProperty() {
assertThat(new VersionProperty("spring-boot.version").toStandardFormat()) assertThat(VersionProperty.of("spring-boot.version").toStandardFormat())
.isEqualTo("spring-boot.version"); .isEqualTo("spring-boot.version");
} }
@Test @Test
public void testCamelCaseProperty() { public void testCamelCaseProperty() {
assertThat(new VersionProperty("spring-boot.version").toCamelCaseFormat()) assertThat(VersionProperty.of("spring-boot.version").toCamelCaseFormat())
.isEqualTo("springBootVersion"); .isEqualTo("springBootVersion");
} }
@Test @Test
public void testStandardPropertyWithNoSeparator() { public void testStandardPropertyWithNoSeparator() {
assertThat(new VersionProperty("springbootversion").toStandardFormat()) assertThat(VersionProperty.of("springbootversion").toStandardFormat())
.isEqualTo("springbootversion"); .isEqualTo("springbootversion");
} }
@Test @Test
public void testCamelCasePropertyWithNoSeparator() { public void testCamelCasePropertyWithNoSeparator() {
assertThat(new VersionProperty("springbootversion").toCamelCaseFormat()) assertThat(VersionProperty.of("springbootversion").toCamelCaseFormat())
.isEqualTo("springbootversion"); .isEqualTo("springbootversion");
} }
@@ -60,14 +60,14 @@ public class VersionPropertyTests {
public void testInvalidPropertyUpperCase() { public void testInvalidPropertyUpperCase() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("upper case"); this.thrown.expectMessage("upper case");
new VersionProperty("Spring-boot.version"); VersionProperty.of("Spring-boot.version");
} }
@Test @Test
public void testInvalidPropertyIllegalCharacter() { public void testInvalidPropertyIllegalCharacter() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Unsupported character"); this.thrown.expectMessage("Unsupported character");
new VersionProperty("spring-boot_version"); VersionProperty.of("spring-boot_version");
} }
} }
@@ -24,10 +24,7 @@ repositories {
mavenCentral() mavenCentral()
} }
ext['fooVersion'] = '1.3.3'
ext {
fooVersion = '1.3.3'
}
dependencies { dependencies {
compile('org.acme:foo') compile('org.acme:foo')
@@ -24,11 +24,8 @@ repositories {
mavenCentral() mavenCentral()
} }
ext['springBarVersion'] = '0.2.0.RELEASE'
ext { ext['spring-foo.version'] = '0.1.0.RELEASE'
springBarVersion = '0.2.0.RELEASE'
springFooVersion = '0.1.0.RELEASE'
}
dependencies { dependencies {
compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-web')