Skip to content

Commit e8438e7

Browse files
committed
Merge pull request #1406 from mhalbritter
* pr/1406: Polish "Make optional/developmentOnly more generic" Make optional/developmentOnly more generic Closes gh-1406
2 parents 38a4926 + 4f85c00 commit e8438e7

File tree

8 files changed

+292
-70
lines changed

8 files changed

+292
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build.gradle;
18+
19+
import io.spring.initializr.generator.buildsystem.Dependency;
20+
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
21+
import io.spring.initializr.generator.buildsystem.gradle.GradleDependency;
22+
import io.spring.initializr.generator.spring.build.BuildCustomizer;
23+
24+
/**
25+
* Gradle {@link BuildCustomizer} that sets the "developmentOnly" configuration for a
26+
* dependency.
27+
*
28+
* @author Stephane Nicoll
29+
* @author Moritz Halbritter
30+
*/
31+
public class DevelopmentOnlyDependencyGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
32+
33+
private final String dependencyId;
34+
35+
/**
36+
* Create a new instance with the identifier for the dependency.
37+
* @param dependencyId the id of the dependency
38+
*/
39+
public DevelopmentOnlyDependencyGradleBuildCustomizer(String dependencyId) {
40+
this.dependencyId = dependencyId;
41+
}
42+
43+
@Override
44+
public void customize(GradleBuild build) {
45+
Dependency dependency = build.dependencies().get(this.dependencyId);
46+
if (dependency != null) {
47+
build.dependencies()
48+
.add(this.dependencyId, GradleDependency.from(dependency).configuration("developmentOnly"));
49+
}
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build.maven;
18+
19+
import io.spring.initializr.generator.buildsystem.Dependency;
20+
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
21+
import io.spring.initializr.generator.buildsystem.maven.MavenDependency;
22+
import io.spring.initializr.generator.spring.build.BuildCustomizer;
23+
24+
/**
25+
* Maven {@link BuildCustomizer} that sets the "optional" flag for a dependency.
26+
*
27+
* @author Stephane Nicoll
28+
* @author Moritz Halbritter
29+
*/
30+
public class OptionalDependencyMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {
31+
32+
private final String dependencyId;
33+
34+
/**
35+
* Create a new instance with the identifier for the dependency.
36+
* @param dependencyId the id of the dependency
37+
*/
38+
public OptionalDependencyMavenBuildCustomizer(String dependencyId) {
39+
this.dependencyId = dependencyId;
40+
}
41+
42+
@Override
43+
public void customize(MavenBuild build) {
44+
Dependency dependency = build.dependencies().get(this.dependencyId);
45+
if (dependency != null) {
46+
build.dependencies().add(this.dependencyId, MavenDependency.from(dependency).optional(true));
47+
}
48+
}
49+
50+
}

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/dependency/devtools/DevToolsGradleBuildCustomizer.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,19 @@
1616

1717
package io.spring.initializr.generator.spring.dependency.devtools;
1818

19-
import io.spring.initializr.generator.buildsystem.Dependency;
20-
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
21-
import io.spring.initializr.generator.buildsystem.gradle.GradleDependency;
2219
import io.spring.initializr.generator.spring.build.BuildCustomizer;
20+
import io.spring.initializr.generator.spring.build.gradle.DevelopmentOnlyDependencyGradleBuildCustomizer;
2321
import io.spring.initializr.generator.version.Version;
24-
import io.spring.initializr.generator.version.VersionParser;
25-
import io.spring.initializr.generator.version.VersionRange;
2622

2723
/**
2824
* Gradle {@link BuildCustomizer} that creates a dedicated "developmentOnly" configuration
2925
* when devtools is selected.
3026
*
3127
* @author Stephane Nicoll
28+
* @deprecated in favor of {@link DevelopmentOnlyDependencyGradleBuildCustomizer}
3229
*/
33-
public class DevToolsGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
34-
35-
private static final VersionRange SPRING_BOOT_2_3_0_RC1_OR_LATER = VersionParser.DEFAULT.parseRange("2.3.0.RC1");
36-
37-
private final Version platformVersion;
38-
39-
private final String devtoolsDependencyId;
30+
@Deprecated(since = "0.20.0", forRemoval = true)
31+
public class DevToolsGradleBuildCustomizer extends DevelopmentOnlyDependencyGradleBuildCustomizer {
4032

4133
/**
4234
* Create a new instance with the requested {@link Version platform version} and the
@@ -45,23 +37,7 @@ public class DevToolsGradleBuildCustomizer implements BuildCustomizer<GradleBuil
4537
* @param devtoolsDependencyId the id of the devtools dependency
4638
*/
4739
public DevToolsGradleBuildCustomizer(Version platformVersion, String devtoolsDependencyId) {
48-
this.platformVersion = platformVersion;
49-
this.devtoolsDependencyId = devtoolsDependencyId;
50-
}
51-
52-
@Override
53-
public void customize(GradleBuild build) {
54-
Dependency devtools = build.dependencies().get(this.devtoolsDependencyId);
55-
if (devtools == null) {
56-
return;
57-
}
58-
if (!SPRING_BOOT_2_3_0_RC1_OR_LATER.match(this.platformVersion)) {
59-
build.configurations().add("developmentOnly");
60-
build.configurations()
61-
.customize("runtimeClasspath", (runtimeClasspath) -> runtimeClasspath.extendsFrom("developmentOnly"));
62-
}
63-
build.dependencies()
64-
.add(this.devtoolsDependencyId, GradleDependency.from(devtools).configuration("developmentOnly"));
40+
super(devtoolsDependencyId);
6541
}
6642

6743
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,34 +16,24 @@
1616

1717
package io.spring.initializr.generator.spring.dependency.devtools;
1818

19-
import io.spring.initializr.generator.buildsystem.Dependency;
20-
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
21-
import io.spring.initializr.generator.buildsystem.maven.MavenDependency;
2219
import io.spring.initializr.generator.spring.build.BuildCustomizer;
20+
import io.spring.initializr.generator.spring.build.maven.OptionalDependencyMavenBuildCustomizer;
2321

2422
/**
2523
* Maven {@link BuildCustomizer} that sets the "optional" flag when devtools is selected.
2624
*
2725
* @author Stephane Nicoll
26+
* @deprecated in favor of {@link OptionalDependencyMavenBuildCustomizer}
2827
*/
29-
public class DevToolsMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {
30-
31-
private final String devtoolsDependencyId;
28+
@Deprecated(since = "0.20.0", forRemoval = true)
29+
public class DevToolsMavenBuildCustomizer extends OptionalDependencyMavenBuildCustomizer {
3230

3331
/**
3432
* Create a new instance with the identifier for the devtools dependency.
3533
* @param devtoolsDependencyId the id of the devtools dependency
3634
*/
3735
public DevToolsMavenBuildCustomizer(String devtoolsDependencyId) {
38-
this.devtoolsDependencyId = devtoolsDependencyId;
39-
}
40-
41-
@Override
42-
public void customize(MavenBuild build) {
43-
Dependency devtools = build.dependencies().get(this.devtoolsDependencyId);
44-
if (devtools != null) {
45-
build.dependencies().add("devtools", MavenDependency.from(devtools).optional(true));
46-
}
36+
super(devtoolsDependencyId);
4737
}
4838

4939
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build.gradle;
18+
19+
import io.spring.initializr.generator.buildsystem.Dependency;
20+
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
21+
import io.spring.initializr.generator.buildsystem.gradle.GradleDependency;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link DevelopmentOnlyDependencyGradleBuildCustomizer}.
28+
*
29+
* @author Stephane Nicoll
30+
* @author Moritz Halbritter
31+
*/
32+
class DevelopmentOnlyDependencyGradleBuildCustomizerTests {
33+
34+
private static final Dependency WEB_DEPENDENCY = Dependency
35+
.withCoordinates("org.springframework.boot", "spring-boot-starter-web")
36+
.build();
37+
38+
private static final Dependency DEVTOOLS_DEPENDENCY = Dependency
39+
.withCoordinates("org.springframework.boot", "spring-boot-devtools")
40+
.build();
41+
42+
@Test
43+
void shouldAddDevelopmentOnlyConfiguration() {
44+
GradleBuild build = new GradleBuild();
45+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
46+
new DevelopmentOnlyDependencyGradleBuildCustomizer("devtools").customize(build);
47+
Dependency devtools = build.dependencies().get("devtools");
48+
assertThat(devtools).isInstanceOf(GradleDependency.class);
49+
assertThat(((GradleDependency) devtools).getConfiguration()).isEqualTo("developmentOnly");
50+
}
51+
52+
@Test
53+
void shouldNotFailOnDuplicateDependencies() {
54+
GradleBuild build = new GradleBuild();
55+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
56+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
57+
build.dependencies().add("web", WEB_DEPENDENCY);
58+
new DevelopmentOnlyDependencyGradleBuildCustomizer("devtools").customize(build);
59+
Dependency devtools = build.dependencies().get("devtools");
60+
assertThat(devtools).isInstanceOf(GradleDependency.class);
61+
assertThat(((GradleDependency) devtools).getConfiguration()).isEqualTo("developmentOnly");
62+
}
63+
64+
@Test
65+
void shouldIgnoreOtherDependencies() {
66+
GradleBuild build = new GradleBuild();
67+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
68+
build.dependencies().add("web", WEB_DEPENDENCY);
69+
new DevelopmentOnlyDependencyGradleBuildCustomizer("devtools").customize(build);
70+
Dependency devtools = build.dependencies().get("devtools");
71+
assertThat(devtools).isInstanceOf(GradleDependency.class);
72+
assertThat(((GradleDependency) devtools).getConfiguration()).isEqualTo("developmentOnly");
73+
Dependency web = build.dependencies().get("web");
74+
assertThat(web).isNotInstanceOf(GradleDependency.class);
75+
}
76+
77+
@Test
78+
void shouldNotChangeDependencies() {
79+
GradleBuild build = new GradleBuild();
80+
build.dependencies().add("web", WEB_DEPENDENCY);
81+
new DevelopmentOnlyDependencyGradleBuildCustomizer("devtools").customize(build);
82+
assertThat(build.dependencies().ids()).containsOnly("web");
83+
}
84+
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build.maven;
18+
19+
import io.spring.initializr.generator.buildsystem.Dependency;
20+
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
21+
import io.spring.initializr.generator.buildsystem.maven.MavenDependency;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link OptionalDependencyMavenBuildCustomizer}.
28+
*
29+
* @author Stephane Nicoll
30+
* @author Moritz Halbritter
31+
*/
32+
class OptionalDependencyMavenBuildCustomizerTests {
33+
34+
private static final Dependency WEB_DEPENDENCY = Dependency
35+
.withCoordinates("org.springframework.boot", "spring-boot-starter-web")
36+
.build();
37+
38+
private static final Dependency DEVTOOLS_DEPENDENCY = Dependency
39+
.withCoordinates("org.springframework.boot", "spring-boot-devtools")
40+
.build();
41+
42+
@Test
43+
void shouldAddOptionalScope() {
44+
MavenBuild build = new MavenBuild();
45+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
46+
new OptionalDependencyMavenBuildCustomizer("devtools").customize(build);
47+
Dependency devtools = build.dependencies().get("devtools");
48+
assertThat(devtools).isInstanceOf(MavenDependency.class);
49+
assertThat(((MavenDependency) devtools).isOptional()).isTrue();
50+
}
51+
52+
@Test
53+
void shouldNotFailOnDuplicateDependencies() {
54+
MavenBuild build = new MavenBuild();
55+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
56+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
57+
new OptionalDependencyMavenBuildCustomizer("devtools").customize(build);
58+
Dependency devtools = build.dependencies().get("devtools");
59+
assertThat(devtools).isInstanceOf(MavenDependency.class);
60+
assertThat(((MavenDependency) devtools).isOptional()).isTrue();
61+
}
62+
63+
@Test
64+
void shouldIgnoreOtherDependencies() {
65+
MavenBuild build = new MavenBuild();
66+
build.dependencies().add("devtools", DEVTOOLS_DEPENDENCY);
67+
build.dependencies().add("web", WEB_DEPENDENCY);
68+
new OptionalDependencyMavenBuildCustomizer("devtools").customize(build);
69+
Dependency devtools = build.dependencies().get("devtools");
70+
assertThat(devtools).isInstanceOf(MavenDependency.class);
71+
assertThat(((MavenDependency) devtools).isOptional()).isTrue();
72+
Dependency web = build.dependencies().get("web");
73+
assertThat(web).isNotInstanceOf(MavenDependency.class);
74+
}
75+
76+
@Test
77+
void shouldNotChangeDependencies() {
78+
MavenBuild build = new MavenBuild();
79+
build.dependencies().add("web", WEB_DEPENDENCY);
80+
new OptionalDependencyMavenBuildCustomizer("devtools").customize(build);
81+
assertThat(build.dependencies().ids()).containsOnly("web");
82+
}
83+
84+
}

0 commit comments

Comments
 (0)