Skip to content

Commit 1ffd955

Browse files
committed
Support Custom Maven Repository Locations
Closes gh-23
1 parent 2aa4766 commit 1ffd955

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ testImplementation ('org.springframework.experimental.boot:spring-boot-testjars:
110110
</dependency>
111111
----
112112

113+
==== Custom Maven Repositories
114+
115+
By default, only Maven Central is searched.
116+
You can customize the repositories that are searched by injecting the repositories like the example below:
117+
118+
[source,java]
119+
----
120+
List<RemoteRepository> repositories = new ArrayList<>();
121+
repositories.add(new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build());
122+
repositories.add(new RemoteRepository.Builder("spring-milestone", "default", "https://repo.spring.io/milestone/").build());
123+
MavenClasspathEntry classpathEntry = new MavenClasspathEntry("org.springframework:spring-core:6.1.0-RC1", repositories);
124+
----
125+
113126
=== Default Java Main
114127

115128
In some cases you need to provide a Java main class without any additional configuration.

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/MavenClasspathEntry.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class MavenClasspathEntry implements ClasspathEntry {
5959

6060
private final Log logger = LogFactory.getLog(getClass());
6161

62+
private final List<RemoteRepository> repositories;
63+
6264
/**
6365
* The maven coordinates (e.g. "org.springframework:spring-core:6.1.0")
6466
*/
@@ -71,7 +73,27 @@ public class MavenClasspathEntry implements ClasspathEntry {
7173
* SpringBootVersion.getVersion()).
7274
*/
7375
public MavenClasspathEntry(String coords) {
76+
this(coords, newRepositories());
77+
}
78+
79+
/**
80+
* Creates a new instance.
81+
*
82+
* <code>
83+
* List$lt;RemoteRepository&gt; repositories = new ArrayList&lt;&gt;();
84+
* repositories.add(new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build());
85+
* repositories.add(new RemoteRepository.Builder("spring-milestone", "default", "https://repo.spring.io/milestone/").build());
86+
* MavenClasspathEntry entry = new MavenClasspathEntry("org.springframework:spring-core:6.2.0-RC1", repositories);
87+
* </code>
88+
* @param coords the maven coordinates (e.g. *
89+
* "org.springframework.boot:spring-boot-starter-web:" + *
90+
* SpringBootVersion.getVersion()).
91+
* @param repositories a {@link List} of the {@link RemoteRepository} instances to
92+
* use.
93+
*/
94+
public MavenClasspathEntry(String coords, List<RemoteRepository> repositories) {
7495
this.coords = coords;
96+
this.repositories = repositories;
7597
}
7698

7799
/**
@@ -105,7 +127,7 @@ public List<String> resolve() {
105127

106128
CollectRequest collectRequest = new CollectRequest();
107129
collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
108-
collectRequest.setRepositories(newRepositories(system, session));
130+
collectRequest.setRepositories(this.repositories);
109131

110132
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
111133

@@ -166,7 +188,7 @@ private static DefaultRepositorySystemSession newRepositorySystemSession(Reposit
166188
return session;
167189
}
168190

169-
private static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
191+
private static List<RemoteRepository> newRepositories() {
170192
return new ArrayList<>(Collections.singletonList(newCentralRepository()));
171193
}
172194

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/MavenClasspathEntryTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.springframework.experimental.boot.server.exec;
1818

1919
import java.io.File;
20+
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Optional;
2223

24+
import org.eclipse.aether.repository.RemoteRepository;
2325
import org.junit.jupiter.api.Test;
2426

2527
import org.springframework.boot.SpringBootVersion;
@@ -45,4 +47,26 @@ void springBootStarter() {
4547
.isTrue();
4648
}
4749

50+
@Test
51+
void resolveDependencyWhenCustomRepository() {
52+
List<RemoteRepository> repositories = new ArrayList<>();
53+
repositories.add(
54+
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build());
55+
repositories
56+
.add(new RemoteRepository.Builder("spring-milestone", "default", "https://repo.spring.io/milestone/")
57+
.build());
58+
MavenClasspathEntry classpathEntry = new MavenClasspathEntry("org.springframework:spring-core:6.1.0-RC1",
59+
repositories);
60+
List<String> entries = classpathEntry.resolve();
61+
assertThat(entries).hasSize(2);
62+
String mavenLocal = new File(System.getProperty("user.home"), ".m2/repository").getAbsolutePath();
63+
entries.forEach((entry) -> assertThat(entry).startsWith(mavenLocal));
64+
String springCorePathPartial = "/org/springframework/spring-core/6.1.0-RC1/spring-core-6.1.0-RC1.jar";
65+
Optional<String> springCoreEntry = entries.stream().filter((entry) -> entry.contains(springCorePathPartial))
66+
.findFirst();
67+
assertThat(springCoreEntry.isPresent())
68+
.withFailMessage("Unable to find spring-core with path that contains " + springCorePathPartial)
69+
.isTrue();
70+
}
71+
4872
}

0 commit comments

Comments
 (0)