2828import java .nio .file .Files ;
2929import java .nio .file .Path ;
3030import java .nio .file .Paths ;
31+ import java .util .Collections ;
3132import java .util .Map ;
3233import java .util .Properties ;
3334
3738import org .apache .maven .plugin .MojoExecutionException ;
3839import org .apache .maven .plugins .annotations .Mojo ;
3940import org .apache .maven .plugins .annotations .Parameter ;
40- import org .apache .maven .settings .Mirror ;
41- import org .apache .maven .settings .Settings ;
4241import org .codehaus .plexus .archiver .UnArchiver ;
4342import org .codehaus .plexus .components .io .fileselectors .FileSelector ;
4443import org .eclipse .aether .RepositorySystem ;
44+ import org .eclipse .aether .RepositorySystemSession ;
4545import org .eclipse .aether .artifact .Artifact ;
4646import org .eclipse .aether .artifact .DefaultArtifact ;
47+ import org .eclipse .aether .repository .RemoteRepository ;
4748import org .eclipse .aether .resolution .ArtifactRequest ;
4849import org .eclipse .aether .resolution .ArtifactResolutionException ;
4950import org .eclipse .aether .resolution .ArtifactResult ;
5960public 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}
0 commit comments