Support for compileOnly scope

start.spring.io exposes an annotation processor (Lombok) that's quite
popular but is exposed as a compile/transitive dependency. This commit
introduces a `compileOnly` scope that maps to Gradle's `compileOnly`
scope and Maven's `optional` flag.

There is a case to support optional dependencies in general but
unfortunately, Gradle doesn't have support for such feature.

Closes gh-128
This commit is contained in:
Stephane Nicoll
2016-11-04 11:38:15 +01:00
parent e3112811b7
commit 079f46f51c
8 changed files with 123 additions and 5 deletions

View File

@@ -20,7 +20,6 @@ import groovy.util.logging.Slf4j
import io.spring.initializr.InitializrException
import io.spring.initializr.metadata.Dependency
import io.spring.initializr.metadata.InitializrMetadataProvider
import io.spring.initializr.util.Agent
import io.spring.initializr.util.GroovyTemplate
import io.spring.initializr.util.Version
@@ -259,6 +258,7 @@ class ProjectGenerator {
model['compileDependencies'] = filterDependencies(dependencies, Dependency.SCOPE_COMPILE)
model['runtimeDependencies'] = filterDependencies(dependencies, Dependency.SCOPE_RUNTIME)
model['compileOnlyDependencies'] = filterDependencies(dependencies, Dependency.SCOPE_COMPILE_ONLY)
model['providedDependencies'] = filterDependencies(dependencies, Dependency.SCOPE_PROVIDED)
model['testDependencies'] = filterDependencies(dependencies, Dependency.SCOPE_TEST)

View File

@@ -37,12 +37,14 @@ import io.spring.initializr.util.VersionRange
class Dependency extends MetadataElement {
static final String SCOPE_COMPILE = 'compile'
static final String SCOPE_COMPILE_ONLY = 'compileOnly'
static final String SCOPE_RUNTIME = 'runtime'
static final String SCOPE_PROVIDED = 'provided'
static final String SCOPE_TEST = 'test'
static final List<String> SCOPE_ALL = [
SCOPE_COMPILE,
SCOPE_RUNTIME,
SCOPE_COMPILE_ONLY,
SCOPE_PROVIDED,
SCOPE_TEST
]

View File

@@ -44,7 +44,8 @@ dependencies {<% compileDependencies.each { %>
compile('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %><% if (language=='groovy') { %>
compile('org.codehaus.groovy:groovy')<% } %><% if (language=='kotlin') { %>
compile("org.jetbrains.kotlin:kotlin-stdlib:\${kotlinVersion}")<% } %><% runtimeDependencies.each { %>
runtime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %><% providedDependencies.each { %>
runtime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %><% compileOnlyDependencies.each { %>
compileOnly('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %><% providedDependencies.each { %>
providedRuntime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %>
testCompile('org.springframework.boot:spring-boot-starter-test')<% testDependencies.each { %>
testCompile('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}${it.type ? "@$it.type" : ""}')<% } %>

View File

@@ -46,6 +46,13 @@
<version>${it.version}</version><% } %>
<scope>runtime</scope><% if (it.type != null) { %>
<type>${it.type}</type><% } %>
</dependency><% } %><% compileOnlyDependencies.each { %>
<dependency>
<groupId>${it.groupId}</groupId>
<artifactId>${it.artifactId}</artifactId><% if (it.version != null) { %>
<version>${it.version}</version><% } %>
<optional>true</optional><% if (it.type != null) { %>
<type>${it.type}</type><% } %>
</dependency><% } %><% providedDependencies.each { %>
<dependency>
<groupId>${it.groupId}</groupId>

View File

@@ -25,9 +25,6 @@ import org.junit.runners.Parameterized
import org.springframework.core.io.ClassPathResource
import static io.spring.initializr.test.generator.ProjectAssert.DEFAULT_APPLICATION_NAME
import static io.spring.initializr.test.generator.ProjectAssert.DEFAULT_PACKAGE_NAME
/**
* Project generator tests for supported build systems.
*
@@ -126,6 +123,21 @@ class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
.equalsTo(new ClassPathResource("project/$build/bom-property-$assertFileName"))
}
@Test
void compileOnlyDependency() {
def foo = new Dependency(id: 'foo', groupId: 'org.acme', artifactId: 'foo',
scope: Dependency.SCOPE_COMPILE_ONLY)
def metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup('core', 'web', 'data-jpa')
.addDependencyGroup('foo', foo)
.build()
applyMetadata(metadata)
def request = createProjectRequest('foo', 'web', 'data-jpa')
def project = generateProject(request)
project.sourceCodeAssert("$fileName")
.equalsTo(new ClassPathResource("project/$build/compile-only-dependency-$assertFileName"))
}
@Override
ProjectRequest createProjectRequest(String... styles) {
def request = super.createProjectRequest(styles)

View File

@@ -0,0 +1,36 @@
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()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.acme:foo')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

View File

@@ -0,0 +1,59 @@
<?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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.acme</groupId>
<artifactId>foo</artifactId>
<optional>true</optional>
</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>

View File

@@ -112,6 +112,7 @@ initializr:
id: lombok
groupId: org.projectlombok
artifactId: lombok
scope: compileOnly
description: Java annotation library which helps to reduce boilerplate code and code faster
mappings:
- versionRange: "[1.2.0.RELEASE,1.4.0.M1)"