Skip to content

Commit d44e7c9

Browse files
committed
Dynamically populate repositories used in Ant/Maven integration tests
Update build scripts and tests so that repository settings are copied dynamically from the build. See gh-42333
1 parent 7d8507d commit d44e7c9

File tree

10 files changed

+131
-26
lines changed

10 files changed

+131
-26
lines changed

buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void apply(Project project) {
5050
new KotlinConventions().apply(project);
5151
new WarConventions().apply(project);
5252
new EclipseConventions().apply(project);
53+
RepoistoryTransformersExtension.apply(project);
5354
}
5455

5556
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2024-2024 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 org.springframework.boot.build;
18+
19+
import javax.inject.Inject;
20+
21+
import org.gradle.api.Project;
22+
import org.gradle.api.Transformer;
23+
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
24+
25+
/**
26+
* Extension to add {@code springRepoistoryTransformers} utility methods.
27+
*
28+
* @author Phillip Webb
29+
*/
30+
public class RepoistoryTransformersExtension {
31+
32+
private static final String MARKER = "{spring.mavenRepositories}";
33+
34+
private final Project project;
35+
36+
@Inject
37+
public RepoistoryTransformersExtension(Project project) {
38+
this.project = project;
39+
}
40+
41+
public Transformer<String, String> ant() {
42+
return this::transformAnt;
43+
}
44+
45+
private String transformAnt(String line) {
46+
if (line.contains(MARKER)) {
47+
StringBuilder result = new StringBuilder();
48+
String indent = getIndent(line);
49+
this.project.getRepositories().withType(MavenArtifactRepository.class, (repository) -> {
50+
String name = repository.getName();
51+
if (name.startsWith("spring-")) {
52+
result.append(!result.isEmpty() ? "\n" : "");
53+
result.append("%s<ibiblio name=\"%s\" m2compatible=\"true\" root=\"%s\" />".formatted(indent, name,
54+
repository.getUrl()));
55+
}
56+
});
57+
return result.toString();
58+
}
59+
return line;
60+
}
61+
62+
public Transformer<String, String> mavenSettings() {
63+
return this::transformMavenSettings;
64+
}
65+
66+
private String transformMavenSettings(String line) {
67+
if (line.contains(MARKER)) {
68+
StringBuilder result = new StringBuilder();
69+
String indent = getIndent(line);
70+
this.project.getRepositories().withType(MavenArtifactRepository.class, (repository) -> {
71+
String name = repository.getName();
72+
if (name.startsWith("spring-")) {
73+
result.append(!result.isEmpty() ? "\n" : "");
74+
result.append(mavenRepositoryXml(indent, repository));
75+
}
76+
});
77+
return result.toString();
78+
}
79+
return line;
80+
}
81+
82+
private String mavenRepositoryXml(String indent, MavenArtifactRepository repository) {
83+
boolean snapshots = repository.getName().endsWith("-snapshot");
84+
StringBuilder xml = new StringBuilder();
85+
xml.append("%s<repository>%n".formatted(indent));
86+
xml.append("%s\t<id>%s</id>%n".formatted(indent, repository.getName()));
87+
xml.append("%s\t<url>%s</url>%n".formatted(indent, repository.getUrl()));
88+
xml.append("%s\t<releases>%n".formatted(indent));
89+
xml.append("%s\t\t<enabled>%s</enabled>%n".formatted(indent, !snapshots));
90+
xml.append("%s\t</releases>%n".formatted(indent));
91+
xml.append("%s\t<snapshots>%n".formatted(indent));
92+
xml.append("%s\t\t<enabled>%s</enabled>%n".formatted(indent, snapshots));
93+
xml.append("%s\t</snapshots>%n".formatted(indent));
94+
xml.append("%s</repository>".formatted(indent));
95+
return xml.toString();
96+
}
97+
98+
private String getIndent(String line) {
99+
return line.substring(0, line.length() - line.stripLeading().length());
100+
}
101+
102+
static void apply(Project project) {
103+
project.getExtensions().create("springRepoistoryTransformers", RepoistoryTransformersExtension.class, project);
104+
}
105+
106+
}

spring-boot-project/spring-boot-tools/spring-boot-antlib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
task copyIntegrationTestSources(type: Copy) {
2929
from file("src/it")
3030
into "${buildDir}/it"
31+
filter(springRepoistoryTransformers.ant())
3132
}
3233

3334
processResources {

spring-boot-project/spring-boot-tools/spring-boot-antlib/src/it/sample/ivysettings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<ivy pattern="${user.home}/.m2/[organisation]/[module]/[revision]/[module]-[revision].pom" />
99
</filesystem>
1010
<ibiblio name="ibiblio" m2compatible="true" />
11-
<ibiblio name="spring-milestones" m2compatible="true" root="https://repo.spring.io/milestone" />
12-
<ibiblio name="spring-snapshots" m2compatible="true" root="https://repo.spring.io/snapshot" />
11+
<!-- {spring.mavenRepositories} -->
1312
</chain>
1413
</resolvers>
1514
</ivysettings>
15+

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ syncDocumentationSourceForAsciidoctor {
9999
}
100100
}
101101

102+
task copySettingsXml(type: Copy) {
103+
from file("src/intTest/projects/settings.xml")
104+
into "${buildDir}/generated-resources/settings"
105+
filter(springRepoistoryTransformers.mavenSettings())
106+
}
107+
102108
sourceSets {
103109
main {
104110
output.dir("${buildDir}/generated/resources/xsd", builtBy: "xsdResources")
105111
}
106112
intTest {
107-
output.dir("${buildDir}/generated-resources", builtBy: "extractVersionProperties")
113+
output.dir("${buildDir}/generated-resources", builtBy: ["extractVersionProperties", "copySettingsXml"])
108114
}
109115
dockerTest {
110116
output.dir("${buildDir}/generated-resources", builtBy: "extractVersionProperties")

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
160160
}
161161

162162
});
163-
String settingsXml = Files.readString(Paths.get("src", "intTest", "projects", "settings.xml"))
163+
String settingsXml = Files.readString(Paths.get("build", "generated-resources", "settings", "settings.xml"))
164164
.replace("@localCentralUrl@", new File("build/test-maven-repository").toURI().toURL().toString())
165165
.replace("@localRepositoryPath@", new File("build/local-maven-repository").getAbsolutePath());
166166
Files.writeString(destination.resolve("settings.xml"), settingsXml, StandardOpenOption.CREATE_NEW);

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/settings.xml

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@
1818
<enabled>true</enabled>
1919
</snapshots>
2020
</repository>
21-
<repository>
22-
<id>spring-milestones</id>
23-
<name>Spring Milestones</name>
24-
<url>https://repo.spring.io/milestone</url>
25-
</repository>
26-
<repository>
27-
<id>spring-snapshots</id>
28-
<name>Spring Snapshots</name>
29-
<url>https://repo.spring.io/snapshot</url>
30-
<snapshots>
31-
<enabled>true</enabled>
32-
</snapshots>
33-
</repository>
21+
<!-- {spring.mavenRepositories} -->
3422
</repositories>
3523
<pluginRepositories>
3624
<pluginRepository>
@@ -43,11 +31,7 @@
4331
<enabled>true</enabled>
4432
</snapshots>
4533
</pluginRepository>
46-
<pluginRepository>
47-
<id>spring-milestones</id>
48-
<name>Spring Milestones</name>
49-
<url>https://repo.spring.io/milestone</url>
50-
</pluginRepository>
34+
<!-- {spring.mavenRepositories} -->
5135
</pluginRepositories>
5236
</profile>
5337
</profiles>

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-ant/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ task syncTestRepository(type: Sync) {
4646
}
4747
}
4848

49+
task copyAntSources(type: Copy) {
50+
from project.layout.projectDirectory
51+
include "*.xml"
52+
into "${buildDir}/antbuild"
53+
filter(springRepoistoryTransformers.ant())
54+
}
55+
4956
task antRun(type: JavaExec) {
50-
dependsOn syncTestRepository, configurations.antDependencies
57+
workingDir "${buildDir}/antbuild"
58+
dependsOn syncTestRepository, copyAntSources, configurations.antDependencies
5159
classpath = configurations.antDependencies;
5260
mainClass = "org.apache.tools.ant.launch.Launcher"
5361
args = [ "clean", "build" ]

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-ant/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</target>
3434

3535
<target name="compile" depends="init" description="compile">
36-
<javac srcdir="src/main/java" destdir="build/ant/classes" classpathref="compile.classpath" fork="true" includeantruntime="false" source="8" target="8" compiler="javac1.8"/>
36+
<javac srcdir="${projectDir}/src/main/java" destdir="build/ant/classes" classpathref="compile.classpath" fork="true" includeantruntime="false" source="8" target="8" compiler="javac1.8"/>
3737
</target>
3838

3939
<target name="clean" description="cleans all created files/dirs">

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-ant/ivysettings.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
<ivy pattern="${projectDir}/build/test-repository/[organisation]/[module]/[revision]/[module]-[revision].pom" />
1010
</filesystem>
1111
<ibiblio name="ibiblio" m2compatible="true" />
12-
<ibiblio name="spring-milestones" m2compatible="true" root="https://repo.spring.io/milestone" />
13-
<ibiblio name="spring-snapshots" m2compatible="true" root="https://repo.spring.io/snapshot" />
12+
<!-- {spring.mavenRepositories} -->
1413
</chain>
1514
</resolvers>
1615
</ivysettings>

0 commit comments

Comments
 (0)