Skip to content

Commit 8132e06

Browse files
authored
Proposal to fix support for mirrors (#399)
This change relies on reusing existing API that can return proper URL (as set by user by ANY means in Maven config) to resolve from. PR contains also some cleanup. Fixes #396 Supersedes #397
1 parent 78afa6e commit 8132e06

File tree

3 files changed

+88
-63
lines changed

3 files changed

+88
-63
lines changed

maven-wrapper-plugin/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ under the License.
3737
<maven>3.6.3</maven>
3838
</prerequisites>
3939

40+
<properties>
41+
<version.mockito>4.11.0</version.mockito>
42+
</properties>
43+
4044
<dependencies>
4145
<dependency>
4246
<groupId>org.apache.maven</groupId>
@@ -109,6 +113,18 @@ under the License.
109113
<artifactId>junit-jupiter-api</artifactId>
110114
<scope>test</scope>
111115
</dependency>
116+
<dependency>
117+
<groupId>org.mockito</groupId>
118+
<artifactId>mockito-core</artifactId>
119+
<version>${version.mockito}</version>
120+
<scope>test</scope>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.mockito</groupId>
124+
<artifactId>mockito-junit-jupiter</artifactId>
125+
<version>${version.mockito}</version>
126+
<scope>test</scope>
127+
</dependency>
112128
</dependencies>
113129

114130
<build>

maven-wrapper-plugin/src/main/java/org/apache/maven/plugins/wrapper/WrapperMojo.java

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31+
import java.util.Collections;
3132
import java.util.Map;
3233
import java.util.Properties;
3334

@@ -37,13 +38,13 @@
3738
import org.apache.maven.plugin.MojoExecutionException;
3839
import org.apache.maven.plugins.annotations.Mojo;
3940
import org.apache.maven.plugins.annotations.Parameter;
40-
import org.apache.maven.settings.Mirror;
41-
import org.apache.maven.settings.Settings;
4241
import org.codehaus.plexus.archiver.UnArchiver;
4342
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
4443
import org.eclipse.aether.RepositorySystem;
44+
import org.eclipse.aether.RepositorySystemSession;
4545
import org.eclipse.aether.artifact.Artifact;
4646
import org.eclipse.aether.artifact.DefaultArtifact;
47+
import org.eclipse.aether.repository.RemoteRepository;
4748
import org.eclipse.aether.resolution.ArtifactRequest;
4849
import org.eclipse.aether.resolution.ArtifactResolutionException;
4950
import org.eclipse.aether.resolution.ArtifactResult;
@@ -59,7 +60,10 @@
5960
public class WrapperMojo extends AbstractMojo {
6061
private static final String MVNW_REPOURL = "MVNW_REPOURL";
6162

62-
protected static final String DEFAULT_REPOURL = "https://repo.maven.apache.org/maven2";
63+
// Repo ID is constant: maven distro always come from maven central
64+
protected static final String DEFAULT_REPO_ID = "central";
65+
// Repo URL may be altered (by env var or by user mirror settings)
66+
protected static final String DEFAULT_REPO_URL = "https://repo.maven.apache.org/maven2";
6367

6468
// CONFIGURATION PARAMETERS
6569

@@ -161,11 +165,6 @@ public class WrapperMojo extends AbstractMojo {
161165
@Parameter(property = "distributionUrl")
162166
private String distributionUrl;
163167

164-
// READONLY PARAMETERS
165-
166-
@Inject
167-
private MavenSession session;
168-
169168
// CONSTANTS
170169

171170
private static final String WRAPPER_DISTRIBUTION_GROUP_ID = "org.apache.maven.wrapper";
@@ -186,6 +185,12 @@ public class WrapperMojo extends AbstractMojo {
186185

187186
// COMPONENTS
188187

188+
@Parameter(readonly = true, defaultValue = "${repositorySystemSession}")
189+
private RepositorySystemSession repositorySystemSession;
190+
191+
@Inject
192+
private MavenSession session;
193+
189194
@Inject
190195
private RepositorySystem repositorySystem;
191196

@@ -201,7 +206,7 @@ public void execute() throws MojoExecutionException {
201206
distributionType = determineDistributionType(wrapperDir);
202207
}
203208

204-
if (mvndVersion != null && mvndVersion.length() > 0 && !TYPE_ONLY_SCRIPT.equals(distributionType)) {
209+
if (mvndVersion != null && !mvndVersion.isEmpty() && !TYPE_ONLY_SCRIPT.equals(distributionType)) {
205210
throw new MojoExecutionException("maven-wrapper with type=" + distributionType
206211
+ " cannot work with mvnd, please set type to '" + TYPE_ONLY_SCRIPT + "'.");
207212
}
@@ -307,7 +312,7 @@ private void replaceProperties(String wrapperVersion, Path targetFolder) throws
307312
if (distributionUrl != null && !distributionUrl.trim().isEmpty()) {
308313
// Use custom distribution URL if provided
309314
finalDistributionUrl = distributionUrl.trim();
310-
} else if (mvndVersion != null && mvndVersion.length() > 0) {
315+
} else if (mvndVersion != null && !mvndVersion.isEmpty()) {
311316
// Use Maven Daemon distribution URL
312317
finalDistributionUrl = "https://archive.apache.org/dist/maven/mvnd/" + mvndVersion + "/maven-mvnd-"
313318
+ mvndVersion + "-bin.zip";
@@ -326,23 +331,29 @@ private void replaceProperties(String wrapperVersion, Path targetFolder) throws
326331
+ buffer().strong("Maven " + mavenVersion) + " and download from " + repoUrl);
327332

328333
try (BufferedWriter out = Files.newBufferedWriter(wrapperPropertiesFile, StandardCharsets.UTF_8)) {
329-
out.append("wrapperVersion=" + wrapperVersion + System.lineSeparator());
330-
out.append(DISTRIBUTION_TYPE_PROPERTY_NAME + "=" + distributionType + System.lineSeparator());
331-
out.append("distributionUrl=" + finalDistributionUrl + System.lineSeparator());
334+
out.append("wrapperVersion=").append(wrapperVersion).append(System.lineSeparator());
335+
out.append(DISTRIBUTION_TYPE_PROPERTY_NAME + "=")
336+
.append(distributionType)
337+
.append(System.lineSeparator());
338+
out.append("distributionUrl=").append(finalDistributionUrl).append(System.lineSeparator());
332339
if (distributionSha256Sum != null) {
333-
out.append("distributionSha256Sum=" + distributionSha256Sum + System.lineSeparator());
340+
out.append("distributionSha256Sum=")
341+
.append(distributionSha256Sum)
342+
.append(System.lineSeparator());
334343
}
335344
if (!distributionType.equals(TYPE_ONLY_SCRIPT)) {
336-
out.append("wrapperUrl=" + wrapperUrl + System.lineSeparator());
345+
out.append("wrapperUrl=").append(wrapperUrl).append(System.lineSeparator());
337346
}
338347
if (wrapperSha256Sum != null) {
339-
out.append("wrapperSha256Sum=" + wrapperSha256Sum + System.lineSeparator());
348+
out.append("wrapperSha256Sum=").append(wrapperSha256Sum).append(System.lineSeparator());
340349
}
341350
if (alwaysDownload) {
342-
out.append("alwaysDownload=" + Boolean.TRUE + System.lineSeparator());
351+
out.append("alwaysDownload=")
352+
.append(String.valueOf(Boolean.TRUE))
353+
.append(System.lineSeparator());
343354
}
344355
if (alwaysUnpack) {
345-
out.append("alwaysUnpack=" + Boolean.TRUE + System.lineSeparator());
356+
out.append("alwaysUnpack=").append(String.valueOf(Boolean.TRUE)).append(System.lineSeparator());
346357
}
347358
} catch (IOException ioe) {
348359
throw new MojoExecutionException("Can't create maven-wrapper.properties", ioe);
@@ -351,7 +362,7 @@ private void replaceProperties(String wrapperVersion, Path targetFolder) throws
351362

352363
private String getVersion(String defaultVersion, Class<?> clazz, String path) {
353364
String version = defaultVersion;
354-
if (version == null || version.trim().length() == 0 || "true".equals(version)) {
365+
if (version == null || version.trim().isEmpty() || "true".equals(version)) {
355366
Properties props = new Properties();
356367
try (InputStream is = clazz.getResourceAsStream("/META-INF/maven/" + path + "/pom.properties")) {
357368
if (is != null) {
@@ -371,37 +382,27 @@ private String getVersion(String defaultVersion, Class<?> clazz, String path) {
371382
private String getRepoUrl() {
372383
// adapt to also support MVNW_REPOURL as supported by mvnw scripts from maven-wrapper
373384
String envRepoUrl = System.getenv(MVNW_REPOURL);
374-
final String repoUrl = determineRepoUrl(envRepoUrl, session.getSettings());
375-
385+
final String repoUrl = determineRepoUrl(envRepoUrl);
376386
getLog().debug("Determined repo URL to use as " + repoUrl);
377-
378387
return repoUrl;
379388
}
380389

381-
protected String determineRepoUrl(String envRepoUrl, Settings settings) {
382-
390+
protected String determineRepoUrl(String envRepoUrl) {
383391
if (envRepoUrl != null && !envRepoUrl.trim().isEmpty() && envRepoUrl.length() > 4) {
384392
String repoUrl = envRepoUrl.trim();
385-
386393
if (repoUrl.endsWith("/")) {
387394
repoUrl = repoUrl.substring(0, repoUrl.length() - 1);
388395
}
389-
390396
getLog().debug("Using repo URL from " + MVNW_REPOURL + " environment variable.");
391-
392397
return repoUrl;
393398
}
394399

395-
// otherwise mirror from settings
396-
if (settings.getMirrors() != null && !settings.getMirrors().isEmpty()) {
397-
for (Mirror current : settings.getMirrors()) {
398-
if ("*".equals(current.getMirrorOf())) {
399-
getLog().debug("Using repo URL from * mirror in settings file.");
400-
return current.getUrl();
401-
}
402-
}
403-
}
404-
405-
return DEFAULT_REPOURL;
400+
return repositorySystem
401+
.newResolutionRepositories(
402+
repositorySystemSession,
403+
Collections.singletonList(
404+
new RemoteRepository.Builder(DEFAULT_REPO_ID, "default", DEFAULT_REPO_URL).build()))
405+
.get(0)
406+
.getUrl();
406407
}
407408
}

maven-wrapper-plugin/src/test/java/org/apache/maven/plugins/wrapper/WrapperMojoTest.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,70 @@
1818
*/
1919
package org.apache.maven.plugins.wrapper;
2020

21-
import org.apache.maven.settings.Settings;
21+
import org.eclipse.aether.RepositorySystem;
22+
import org.eclipse.aether.RepositorySystemSession;
23+
import org.junit.jupiter.api.BeforeEach;
2224
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.mockito.InjectMocks;
27+
import org.mockito.Mock;
28+
import org.mockito.junit.jupiter.MockitoExtension;
2329

2430
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.mockito.ArgumentMatchers.anyList;
32+
import static org.mockito.ArgumentMatchers.same;
33+
import static org.mockito.Mockito.when;
2534

35+
@ExtendWith(MockitoExtension.class)
2636
class WrapperMojoTest {
37+
@Mock(strictness = Mock.Strictness.LENIENT)
38+
private RepositorySystem repositorySystem;
39+
40+
@Mock
41+
private RepositorySystemSession repositorySystemSession;
42+
43+
@InjectMocks
44+
private WrapperMojo wrapperMojo;
45+
46+
@BeforeEach
47+
void setupMocks() {
48+
when(repositorySystem.newResolutionRepositories(same(repositorySystemSession), anyList()))
49+
.then(i -> i.getArguments()[1]);
50+
}
2751

2852
@Test
2953
void userSuppliedRepoUrlGetsTrailingSlashTrimmed() {
30-
// given
31-
Settings settings = new Settings();
32-
WrapperMojo wrapperMojo = new WrapperMojo();
33-
3454
// when
35-
String determinedRepoUrl = wrapperMojo.determineRepoUrl(WrapperMojo.DEFAULT_REPOURL + "/", settings);
55+
String determinedRepoUrl = wrapperMojo.determineRepoUrl(WrapperMojo.DEFAULT_REPO_URL + "/");
3656

3757
// then
38-
assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
58+
assertEquals(WrapperMojo.DEFAULT_REPO_URL, determinedRepoUrl);
3959
}
4060

4161
@Test
4262
void nullRepoUrlNotUsed() {
43-
// given
44-
Settings settings = new Settings();
45-
WrapperMojo wrapperMojo = new WrapperMojo();
46-
4763
// when
48-
String determinedRepoUrl = wrapperMojo.determineRepoUrl(null, settings);
64+
String determinedRepoUrl = wrapperMojo.determineRepoUrl(null);
4965

5066
// then
51-
assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
67+
assertEquals(WrapperMojo.DEFAULT_REPO_URL, determinedRepoUrl);
5268
}
5369

5470
@Test
5571
void emptyRepoUrlNotUsed() {
56-
// given
57-
Settings settings = new Settings();
58-
WrapperMojo wrapperMojo = new WrapperMojo();
59-
6072
// when
61-
String determinedRepoUrl = wrapperMojo.determineRepoUrl("", settings);
73+
String determinedRepoUrl = wrapperMojo.determineRepoUrl("");
6274

6375
// then
64-
assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
76+
assertEquals(WrapperMojo.DEFAULT_REPO_URL, determinedRepoUrl);
6577
}
6678

6779
@Test
6880
void slashRepoUrlNotUsed() {
69-
// given
70-
Settings settings = new Settings();
71-
WrapperMojo wrapperMojo = new WrapperMojo();
72-
7381
// when
74-
String determinedRepoUrl = wrapperMojo.determineRepoUrl("/", settings);
82+
String determinedRepoUrl = wrapperMojo.determineRepoUrl("/");
7583

7684
// then
77-
assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
85+
assertEquals(WrapperMojo.DEFAULT_REPO_URL, determinedRepoUrl);
7886
}
7987
}

0 commit comments

Comments
 (0)