Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions maven-wrapper-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ under the License.
<type>pom</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
public class WrapperMojo extends AbstractMojo {
private static final String MVNW_REPOURL = "MVNW_REPOURL";

private static final String DEFAULT_REPOURL = "https://repo.maven.apache.org/maven2";
protected static final String DEFAULT_REPOURL = "https://repo.maven.apache.org/maven2";

// CONFIGURATION PARAMETERS

Expand Down Expand Up @@ -334,28 +334,39 @@ private String getVersion(String defaultVersion, Class<?> clazz, String path) {
* Determine the repository URL to download Wrapper and Maven from.
*/
private String getRepoUrl() {
// default
String repoUrl = DEFAULT_REPOURL;

// adapt to also support MVNW_REPOURL as supported by mvnw scripts from maven-wrapper
String mvnwRepoUrl = System.getenv(MVNW_REPOURL);
if (mvnwRepoUrl != null && !mvnwRepoUrl.isEmpty()) {
repoUrl = mvnwRepoUrl;
String envRepoUrl = System.getenv(MVNW_REPOURL);
final String repoUrl = determineRepoUrl(envRepoUrl, this.settings);

getLog().debug("Determined repo URL to use as " + repoUrl);

return repoUrl;
}

protected String determineRepoUrl(String envRepoUrl, Settings settings) {

if (envRepoUrl != null && !envRepoUrl.trim().isEmpty() && envRepoUrl.length() > 4) {
String repoUrl = envRepoUrl.trim();

if (repoUrl.endsWith("/")) {
repoUrl = repoUrl.substring(0, repoUrl.length() - 1);
}

getLog().debug("Using repo URL from " + MVNW_REPOURL + " environment variable.");

return repoUrl;
}

// otherwise mirror from settings
else if (settings.getMirrors() != null && !settings.getMirrors().isEmpty()) {
if (settings.getMirrors() != null && !settings.getMirrors().isEmpty()) {
for (Mirror current : settings.getMirrors()) {
if ("*".equals(current.getMirrorOf())) {
repoUrl = current.getUrl();
break;
getLog().debug("Using repo URL from * mirror in settings file.");
return current.getUrl();
}
}
getLog().debug("Using repo URL from * mirror in settings file.");
}

getLog().debug("Determined repo URL to use as " + repoUrl);

return repoUrl;
return DEFAULT_REPOURL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.wrapper;

import org.apache.maven.settings.Settings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class WrapperMojoTest {

@Test
void user_supplied_repo_url_gets_trailing_slash_trimmed() {
// given
Settings settings = new Settings();
WrapperMojo wrapperMojo = new WrapperMojo();

// when
String determinedRepoUrl = wrapperMojo.determineRepoUrl(WrapperMojo.DEFAULT_REPOURL + "/", settings);

// then
Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
}

@Test
void null_repo_url_not_used() {
// given
Settings settings = new Settings();
WrapperMojo wrapperMojo = new WrapperMojo();

// when
String determinedRepoUrl = wrapperMojo.determineRepoUrl(null, settings);

// then
Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
}

@Test
void empty_repo_url_not_used() {
// given
Settings settings = new Settings();
WrapperMojo wrapperMojo = new WrapperMojo();

// when
String determinedRepoUrl = wrapperMojo.determineRepoUrl("", settings);

// then
Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
}

@Test
void slash_repo_url_not_used() {
// given
Settings settings = new Settings();
WrapperMojo wrapperMojo = new WrapperMojo();

// when
String determinedRepoUrl = wrapperMojo.determineRepoUrl("/", settings);

// then
Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, determinedRepoUrl);
}
}