Restore the default starter if none is set

This commit fixes a regression introduced by the library refactoring.

Previously, if no dependency was selected, the default
'spring-boot-starter' was added to provide the necessary base
dependencies.

This commit adds a addDefaultDependency on ProjectRequest that
adds that dependency. It can be overridden if the default needs to be
different.

Fixes gh-34
This commit is contained in:
Stephane Nicoll
2014-08-27 13:56:49 +02:00
parent 575ca6cf03
commit 56bead0f76
6 changed files with 40 additions and 2 deletions

View File

@@ -25,6 +25,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.util.StringUtils
/**
* The metadata using by the initializr, that is:
@@ -218,10 +219,11 @@ class InitializrMetadata {
/**
* Define this dependency as a standard spring boot starter with the specified name
* <p>If no name is specified, the root 'spring-boot-starter' is assumed.
*/
def asSpringBootStarter(String name) {
groupId = 'org.springframework.boot'
artifactId = 'spring-boot-starter-' + name
artifactId = StringUtils.hasText(name) ? 'spring-boot-starter-' + name : 'spring-boot-starter'
}
/**

View File

@@ -88,6 +88,21 @@ class ProjectRequest {
dependencies << metadata.getDependency('web')
facets << 'web'
}
if (dependencies.isEmpty()) {
addDefaultDependency()
}
}
/**
* Add a default dependency if the project does not define any
* dependency
*/
protected addDefaultDependency() {
InitializrMetadata.Dependency root = new InitializrMetadata.Dependency()
root.id = 'root_starter'
root.asSpringBootStarter('')
dependencies << root
}
/**

View File

@@ -77,6 +77,13 @@ class ProjectGeneratorTests {
verify(listener, times(1)).onGeneratedProject(request)
}
@Test
void noDependencyAddsRootStarter() {
ProjectRequest request = createProjectRequest()
generateProject(request).isJavaProject().isMavenProject().pomAssert()
.hasSpringBootStarterRootDependency()
}
@Test
void mavenPomWithBootSnapshot() {
ProjectRequest request = createProjectRequest('web')

View File

@@ -113,6 +113,10 @@ class PomAssert {
hasDependency('org.springframework.boot', 'spring-boot-starter-' + dependency)
}
PomAssert hasSpringBootStarterRootDependency() {
hasDependency('org.springframework.boot', 'spring-boot-starter')
}
PomAssert hasDependency(String groupId, String artifactId) {
hasDependency(groupId, artifactId, null)
}

View File

@@ -66,7 +66,8 @@ abstract class AbstractInitializerControllerFormIntegrationTests extends Abstrac
HomePage page = home()
ProjectAssert projectAssert = zipProjectAssert(page.generateProject())
projectAssert.isMavenProject().isJavaProject().hasStaticAndTemplatesResources(false)
.pomAssert().hasDependenciesCount(1).hasSpringBootStarterDependency('test')
.pomAssert().hasDependenciesCount(2)
.hasSpringBootStarterRootDependency().hasSpringBootStarterDependency('test')
}
@Test

View File

@@ -57,6 +57,15 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
.hasDependency('org.acme', 'bar', '2.1.0')
}
@Test
void noDependencyProject() {
downloadZip('/starter.zip').isJavaProject().isMavenProject()
.hasStaticAndTemplatesResources(false).pomAssert()
.hasDependenciesCount(2)
.hasSpringBootStarterRootDependency() // the root dep is added if none is specified
.hasSpringBootStarterDependency('test')
}
@Test
void gradleWarProject() {
downloadZip('/starter.zip?style=web&style=security&packaging=war&type=gradle.zip')