Enable strict validation of dependencies

Previously, if one invokes the service asking for dependency `foo` and
`foo` does not exist we created a starter for it, that is
`spring-boot-starter-foo`. This mechanism was put in place because we
don't know all starters and to offer a nice fallback for users who know
what they're doing.

That statement proved to be wrong since users actually discover new
starters via the service and don't seem to attempt to create such starter
that are located in the `org.springframework.boot` groupId anyway. Most
if not all of those are pilot errors.

This commit enables strict validation of dependencies and generate an
appropriate exception if it isn't defined in the meta-data.

Closes gh-234
This commit is contained in:
Stephane Nicoll
2016-07-08 14:37:41 +02:00
parent a3d072f20a
commit 0ac320e5ae
5 changed files with 41 additions and 18 deletions

View File

@@ -78,12 +78,7 @@ class ProjectRequest extends BasicProjectRequest {
resolvedDependencies = depIds.collect {
def dependency = metadata.dependencies.get(it)
if (dependency == null) {
if (it.contains(':')) {
throw new InvalidProjectRequestException("Unknown dependency '$it' check project metadata")
}
log.warn("No known dependency for style '$it' assuming spring-boot-starter")
dependency = new Dependency()
dependency.asSpringBootStarter(it)
throw new InvalidProjectRequestException("Unknown dependency '$it' check project metadata")
}
dependency.resolve(requestedVersion)
}

View File

@@ -550,6 +550,18 @@ class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
.hasDependenciesCount(3)
}
@Test
void invalidDependency() {
def request = createProjectRequest('foo-bar')
try {
generateMavenPom(request)
fail("Should have failed to generate project")
} catch (InvalidProjectRequestException ex) {
assertThat ex.message, containsString('foo-bar')
verifyProjectFailedEventFor(request, ex)
}
}
@Test
void invalidType() {
def request = createProjectRequest('web')

View File

@@ -100,15 +100,17 @@ class ProjectRequestTests {
}
@Test
void resolveUnknownSimpleIdAsSpringBootStarter() {
void resolveUnknownSimpleId() {
def request = new ProjectRequest()
def metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup('code', 'org.foo:bar').build()
request.style << 'org.foo:bar' << 'foo-bar'
thrown.expect(InvalidProjectRequestException)
thrown.expectMessage('foo-bar')
request.resolve(metadata)
assertDependency(request.resolvedDependencies[0], 'org.foo', 'bar', null)
assertBootStarter(request.resolvedDependencies[1], 'foo-bar')
assertEquals(1, request.resolvedDependencies.size())
}
@Test