mirror of
https://gitee.com/dcren/initializr.git
synced 2026-03-18 21:09:43 +08:00
Add support for configurable properties
This commit introduces a `buildProperties` property on the request that can be used to specify Gradle/Maven build-specific properties as well as an arbitrary number of version overrides. Instead of hard-coding some properties in the templates, these defaults are now inherited from the request itself. Closes gh-259
This commit is contained in:
@@ -33,32 +33,40 @@ import static io.spring.initializr.test.generator.ProjectAssert.DEFAULT_PACKAGE_
|
||||
@RunWith(Parameterized.class)
|
||||
class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
|
||||
|
||||
@Parameterized.Parameters(name = "{0} with {1}")
|
||||
@Parameterized.Parameters(name = "{0}")
|
||||
public static Object[] parameters() {
|
||||
Object[] javaMaven = ["java", "maven", "pom.xml"]
|
||||
Object[] javaGradle = ["java", "gradle", "build.gradle"]
|
||||
Object[] groovyMaven = ["groovy", "maven", "pom.xml"]
|
||||
Object[] groovyGradle = ["groovy", "gradle", "build.gradle"]
|
||||
Object[] kotlinMaven = ["kotlin", "maven", "pom.xml"]
|
||||
Object[] kotlinGradle = ["kotlin", "gradle", "build.gradle"]
|
||||
Object[] parameters = [javaMaven, javaGradle, groovyMaven, groovyGradle, kotlinMaven, kotlinGradle]
|
||||
Object[] maven = ["maven", "pom.xml"]
|
||||
Object[] gradle = ["gradle", "build.gradle"]
|
||||
Object[] parameters = [maven, gradle]
|
||||
parameters
|
||||
}
|
||||
|
||||
private final String language
|
||||
private final String build
|
||||
private final String fileName
|
||||
private final String assertFileName
|
||||
|
||||
ProjectGeneratorBuildTests(String language, String build, String fileName) {
|
||||
this.language = language
|
||||
ProjectGeneratorBuildTests(String build, String fileName) {
|
||||
this.build = build
|
||||
this.fileName = fileName
|
||||
this.assertFileName = fileName + ".gen"
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardJar() {
|
||||
public void standardJarJava() {
|
||||
testStandardJar('java')
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardJarGroovy() {
|
||||
testStandardJar('groovy')
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardJarKotlin() {
|
||||
testStandardJar('kotlin')
|
||||
}
|
||||
|
||||
private void testStandardJar(def language) {
|
||||
def request = createProjectRequest()
|
||||
request.language = language
|
||||
request.type = "$build-project"
|
||||
@@ -68,7 +76,21 @@ class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardWar() {
|
||||
public void standardWarJava() {
|
||||
testStandardWar('java')
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardWarGroovy() {
|
||||
testStandardWar('java')
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardWarKotlin() {
|
||||
testStandardWar('kotlin')
|
||||
}
|
||||
|
||||
private void testStandardWar(def language) {
|
||||
def request = createProjectRequest('web')
|
||||
request.packaging = 'war'
|
||||
request.language = language
|
||||
@@ -78,4 +100,15 @@ class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
|
||||
.equalsTo(new ClassPathResource("project/$language/war/$assertFileName"))
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionOverride() {
|
||||
def request = createProjectRequest('web')
|
||||
request.type = "$build-project"
|
||||
request.buildProperties.versions['spring-foo.version'] = {'0.1.0.RELEASE'}
|
||||
request.buildProperties.versions['spring-bar.version'] = {'0.2.0.RELEASE'}
|
||||
def project = generateProject(request)
|
||||
project.sourceCodeAssert("$fileName")
|
||||
.equalsTo(new ClassPathResource("project/$build/version-override-$assertFileName"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ package io.spring.initializr.generator
|
||||
import io.spring.initializr.metadata.BillOfMaterials
|
||||
import io.spring.initializr.metadata.Dependency
|
||||
import io.spring.initializr.test.metadata.InitializrMetadataTestBuilder
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
@@ -37,6 +39,9 @@ import static org.junit.Assert.fail
|
||||
*/
|
||||
class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none()
|
||||
|
||||
@Test
|
||||
void defaultMavenPom() {
|
||||
def request = createProjectRequest('web')
|
||||
@@ -611,6 +616,50 @@ class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
|
||||
.hasDependenciesCount(3)
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildPropertiesMaven() {
|
||||
def request = createProjectRequest('web')
|
||||
request.buildProperties.maven['name'] = { 'test' }
|
||||
request.buildProperties.versions['foo.version'] = { '1.2.3' }
|
||||
request.buildProperties.gradle['ignore.property'] = { 'yes' }
|
||||
|
||||
generateMavenPom(request)
|
||||
.hasProperty('name', 'test')
|
||||
.hasProperty('foo.version', '1.2.3')
|
||||
.hasNoProperty('ignore.property')
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildPropertiesGradle() {
|
||||
def request = createProjectRequest('web')
|
||||
request.buildProperties.gradle['name'] = { 'test' }
|
||||
request.buildProperties.versions['foo.version'] = { '1.2.3' }
|
||||
request.buildProperties.maven['ignore.property'] = { 'yes' }
|
||||
|
||||
generateGradleBuild(request)
|
||||
.contains("name = 'test'")
|
||||
.contains("ext['foo.version'] = '1.2.3'")
|
||||
.doesNotContain('ignore.property')
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidProjectTypeMavenPom() {
|
||||
def request = createProjectRequest('web')
|
||||
request.type = 'gradle-build'
|
||||
this.thrown.expect(InvalidProjectRequestException)
|
||||
this.thrown.expectMessage('gradle-build')
|
||||
projectGenerator.generateMavenPom(request)
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidProjectTypeGradleBuild() {
|
||||
def request = createProjectRequest('web')
|
||||
request.type = 'maven-build'
|
||||
this.thrown.expect(InvalidProjectRequestException)
|
||||
this.thrown.expectMessage('maven-build')
|
||||
projectGenerator.generateGradleBuild(request)
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidDependency() {
|
||||
def request = createProjectRequest('foo-bar')
|
||||
|
||||
@@ -113,6 +113,11 @@ class PomAssert {
|
||||
this
|
||||
}
|
||||
|
||||
PomAssert hasNoProperty(String name) {
|
||||
assertFalse "No property $name should have been found", properties.containsKey(name)
|
||||
this
|
||||
}
|
||||
|
||||
PomAssert hasDependenciesCount(int count) {
|
||||
assertEquals "Wrong number of declared dependencies -->'${dependencies.keySet()}",
|
||||
count, dependencies.size()
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
buildscript {
|
||||
ext {
|
||||
springBootVersion = '1.2.3.RELEASE'
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
|
||||
classpath('io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE')
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
apply plugin: 'spring-boot'
|
||||
apply plugin: 'io.spring.dependency-management'
|
||||
|
||||
jar {
|
||||
baseName = 'demo'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
}
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
|
||||
ext['spring-bar.version'] = '0.2.0.RELEASE'
|
||||
ext['spring-foo.version'] = '0.1.0.RELEASE'
|
||||
|
||||
dependencies {
|
||||
compile('org.springframework.boot:spring-boot-starter-web')
|
||||
testCompile('org.springframework.boot:spring-boot-starter-test')
|
||||
}
|
||||
|
||||
|
||||
eclipse {
|
||||
classpath {
|
||||
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
|
||||
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
buildscript {
|
||||
ext {
|
||||
springBootVersion = '1.2.3.RELEASE'
|
||||
kotlinVersion = '1.0.1'
|
||||
springBootVersion = '1.2.3.RELEASE'
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
buildscript {
|
||||
ext {
|
||||
springBootVersion = '1.2.3.RELEASE'
|
||||
kotlinVersion = '1.0.1'
|
||||
springBootVersion = '1.2.3.RELEASE'
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.2.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-bar.version>0.2.0.RELEASE</spring-bar.version>
|
||||
<spring-foo.version>0.1.0.RELEASE</spring-foo.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user