Customize the parent pom to use

See gh-223
This commit is contained in:
Ryan Gardner 2016-04-27 14:25:00 -04:00 committed by Stephane Nicoll
parent bd323680ea
commit 625d5bee15
8 changed files with 83 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package io.spring.initializr.generator
import groovy.util.logging.Slf4j
import io.spring.initializr.InitializrException
import io.spring.initializr.metadata.BillOfMaterials
import io.spring.initializr.metadata.Dependency
import io.spring.initializr.metadata.InitializrMetadataProvider
import io.spring.initializr.util.Version
@ -201,6 +202,15 @@ class ProjectGenerator {
def model = [:]
def metadata = metadataProvider.get()
def useCustomParent = metadata.configuration.env.customParentPomGAV
model['useCustomParentPom'] = useCustomParent != null
if (useCustomParent) {
def gavParts = useCustomParent.split(':')
model['customParentPomGroup'] = gavParts[0]
model['customParentPomArtifact'] = gavParts[1]
model['customParentPomVersion'] = gavParts[2]
request.boms.put("spring-boot", new BillOfMaterials(groupId: "org.springframework.boot", artifactId: "spring-boot-dependencies", version: request.bootVersion))
}
request.resolve(metadata)
// request resolved so we can log what has been requested

View File

@ -122,6 +122,14 @@ class InitializrConfiguration {
*/
String springBootMetadataUrl = 'https://spring.io/project_metadata/spring-boot'
/**
* The group / artifact / version of a custom parent pom to use for generated projects.
* This is only enabled if a value is expliclty provided.
*
* The value must be specified in "groupid:artifactId:versionNumber" format
*/
String customParentPomGAV
/**
* Tracking code for Google Analytics. Only enabled if a value is explicitly provided.
*/
@ -186,15 +194,27 @@ class InitializrConfiguration {
}
void validate() {
if (customParentPomGAV) {
validateGAV(customParentPomGAV);
}
boms.each {
it.value.validate()
}
}
/**
* validate that the GAV has 3 components in the format expected
*/
void validateGAV(String gav) {
if (gav.split(':').length != 3)
throw new InvalidInitializrMetadataException("The group:artifact:version of ${gav} is not a valid GAV (does not have exactly 3 components")
}
void merge(Env other) {
artifactRepository = other.artifactRepository
springBootMetadataUrl = other.springBootMetadataUrl
googleAnalyticsTrackingCode = other.googleAnalyticsTrackingCode
customParentPomGAV = other.customParentPomGAV
fallbackApplicationName = other.fallbackApplicationName
invalidApplicationNames = other.invalidApplicationNames
forceSsl = other.forceSsl

View File

@ -132,6 +132,12 @@
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env",
"defaultValue": true
},
{
"name": "initializr.env.custom-parent-pom-gav",
"type": "java.lang.String",
"description": "The group:artifactId:version of a custom parent pom to use when generating a project",
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env"
},
{
"name": "initializr.env.google-analytics-tracking-code",
"type": "java.lang.String",

View File

@ -12,16 +12,23 @@
<description>${description}</description>
<parent>
<% if (useCustomParentPom) { %>
<groupId>${customParentPomGroup}</groupId>
<artifactId>${customParentPomArtifact}</artifactId>
<version>${customParentPomVersion}</version>
<% } else { %>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${bootVersion}</version>
<% } %>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>${javaVersion}</java.version><% if (language=='kotlin') { %>
<kotlin.version>${kotlinVersion}</kotlin.version><% } %>
<kotlin.version>${kotlinVersion}</kotlin.version><% } %><% if (useCustomParentPom) { %>
<spring-boot.version>${bootVersion}</spring-boot.version><%}%>
</properties>
<dependencies><% compileDependencies.each { %>

View File

@ -309,6 +309,24 @@ class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
.hasSpringBootStarterDependency('web')
}
@Test
void defaultMavenPomHasSpringBootParent() {
def metadata = InitializrMetadataTestBuilder.withDefaults().build()
applyMetadata(metadata)
def request = createProjectRequest('whatever', 'web')
generateMavenPom(request).hasParent("org.springframework.boot", "spring-boot-starter-parent", request.bootVersion)
}
@Test
void mavenPomWithCustomParentPom() {
def customGAV = "com.foo:foo-parent:1.0.0-SNAPSHOT"
def metadata = InitializrMetadataTestBuilder.withDefaults()
.addCustomParentPomGAV(customGAV).build()
applyMetadata(metadata)
def request = createProjectRequest('whatever', 'web')
generateMavenPom(request).hasParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT")
}
@Test
void gradleBuildWithBootSnapshot() {
def request = createProjectRequest('web')

View File

@ -97,7 +97,12 @@ class PomAssert {
}
PomAssert hasBootVersion(String bootVersion) {
assertEquals bootVersion, eng.evaluate(createRootNodeXPath('parent/pom:version'), doc)
// when using a custom parent, the bootVersion comes from a bom entry and not the parent pom version
if (!eng.evaluate(createRootNodeXPath('parent/pom:artifactId'), doc).equals("spring-boot-starter-parent")) {
hasBom("org.springframework.boot", "spring-boot-dependencies", bootVersion)
} else {
assertEquals bootVersion, eng.evaluate(createRootNodeXPath('parent/pom:version'), doc)
}
this
}
@ -136,6 +141,13 @@ class PomAssert {
hasDependency(new Dependency(groupId: groupId, artifactId: artifactId, version: version))
}
PomAssert hasParent(String groupId, String artifactId, String version) {
assertEquals version, eng.evaluate(createRootNodeXPath('parent/pom:version'), doc)
assertEquals groupId, eng.evaluate(createRootNodeXPath('parent/pom:groupId'), doc)
assertEquals artifactId, eng.evaluate(createRootNodeXPath('parent/pom:artifactId'), doc)
this
}
PomAssert hasDependency(Dependency expected) {
def id = generateDependencyId(expected.groupId, expected.artifactId)
def dependency = dependencies[id]

View File

@ -152,6 +152,13 @@ class InitializrMetadataTestBuilder {
this
}
InitializrMetadataTestBuilder addCustomParentPomGAV(String customGAV) {
builder.withCustomizer {
it.configuration.env.customParentPomGAV = customGAV
}
this
}
InitializrMetadataTestBuilder addRepository(String id, String name, String url, boolean snapshotsEnabled) {
builder.withCustomizer {
Repository repo = new Repository(

View File

@ -36,6 +36,7 @@
"kotlin": {
"version": null
},
"customParentPomGAV": null,
"googleAnalyticsTrackingCode": null,
"invalidApplicationNames": [
"SpringApplication",