Skip to content

Commit

Permalink
[MWRAPPER-96] Require Maven 3.2.5
Browse files Browse the repository at this point in the history
- Use Resolver API
- add missing since tag
  • Loading branch information
slawekjaranowski committed Mar 1, 2023
1 parent 8524c56 commit 3bfb14d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@
import java.util.Properties;

import org.apache.maven.Maven;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;

Expand All @@ -68,12 +69,16 @@ public class WrapperMojo extends AbstractMojo {
/**
* The version of Maven to require, default value is the Runtime version of Maven.
* Can be any valid release above 2.0.9
*
* @since 3.0.0
*/
@Parameter(property = "maven")
private String mavenVersion;

/**
* The version of Maven Daemon to require.
*
* @since 3.2.0
*/
@Parameter(property = "mvnd")
private String mvndVersion;
Expand All @@ -88,44 +93,55 @@ public class WrapperMojo extends AbstractMojo {
* <dt>source</dt>
* <dd>Java source code, will be compiled on the fly</dd>
* <dt>only-script</dt>
* <dd>the new lite implementation of mvnw/mvnw.cmd scripts downloads the maven directly and skips maven-wrapper.jar</dd>
* <dd>the new lite implementation of mvnw/mvnw.cmd scripts downloads the maven directly and skips maven-wrapper.jar - since 3.2.0</dd>
* </dl>
*
* Value will be used as classifier of the downloaded file
*
* @since 3.0.0
*/
@Parameter(defaultValue = "bin", property = "type")
private String distributionType;

/**
* Include <code>mvnwDebug*</code> scripts?
*
* @since 3.0.0
*/
@Parameter(defaultValue = "false", property = "includeDebug")
private boolean includeDebugScript;

/**
* The expected SHA-256 checksum of the <i>maven-wrapper.jar</i> that is
* used to load the configured Maven distribution.
*
* @since 3.2.0
*/
@Parameter(property = "wrapperSha256Sum")
private String wrapperSha256Sum;

/**
* The expected SHA-256 checksum of the Maven distribution that is
* executed by the installed wrapper.
*
* @since 3.2.0
*/
@Parameter(property = "distributionSha256Sum")
private String distributionSha256Sum;

/**
* Determines if the Maven distribution should be downloaded
* on every execution of the Maven wrapper.
*
* @since 3.2.0
*/
@Parameter(defaultValue = "false", property = "alwaysDownload")
private boolean alwaysDownload;

/**
* Determines if the Maven distribution should be unpacked
* on every execution of the Maven wrapper.
*
* @since 3.2.0
*/
@Parameter(defaultValue = "false", property = "alwaysUnpack")
private boolean alwaysUnpack;
Expand Down Expand Up @@ -198,34 +214,25 @@ private void cleanup(Path wrapperDir) throws MojoExecutionException {
}

private Artifact downloadWrapperDistribution(String wrapperVersion) throws MojoExecutionException {
Artifact artifact = repositorySystem.createArtifactWithClassifier(

Artifact artifact = new DefaultArtifact(
WRAPPER_DISTRIBUTION_GROUP_ID,
WRAPPER_DISTRIBUTION_ARTIFACT_ID,
wrapperVersion,
distributionType,
WRAPPER_DISTRIBUTION_EXTENSION,
distributionType);
wrapperVersion);

MavenExecutionRequest executionRequest = session.getRequest();
ArtifactRequest request = new ArtifactRequest();
request.setRepositories(session.getCurrentProject().getRemotePluginRepositories());
request.setArtifact(artifact);

ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest()
.setArtifact(artifact)
.setLocalRepository(session.getLocalRepository())
.setRemoteRepositories(session.getCurrentProject().getPluginArtifactRepositories())
.setOffline(executionRequest.isOffline())
.setForceUpdate(executionRequest.isUpdateSnapshots());

ArtifactResolutionResult resolveResult = repositorySystem.resolve(resolutionRequest);
try {
ArtifactResult artifactResult = repositorySystem.resolveArtifact(session.getRepositorySession(), request);
return artifactResult.getArtifact();

if (!resolveResult.isSuccess()) {
if (executionRequest.isShowErrors()) {
for (Exception e : resolveResult.getExceptions()) {
getLog().error(e.getMessage(), e);
}
}
throw new MojoExecutionException("artifact: " + artifact + " not resolved.");
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException("artifact: " + artifact + " not resolved.", e);
}

return artifact;
}

private void unpack(Artifact artifact, Path targetFolder) {
Expand All @@ -245,7 +252,7 @@ private void unpack(Artifact artifact, Path targetFolder) {
* No need to look for other properties, restore them, respecting comments, etc.
*
* @param wrapperVersion the wrapper version
* @param targetFolder the folder containing the wrapper.properties
* @param targetFolder the folder containing the wrapper.properties
* @throws MojoExecutionException if writing fails
*/
private void replaceProperties(String wrapperVersion, Path targetFolder) throws MojoExecutionException {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ under the License.

<properties>
<maven.site.path>wrapper-archives/wrapper-LATEST</maven.site.path>
<mavenVersion>3.1.0</mavenVersion>
<mavenVersion>3.2.5</mavenVersion>
<javaVersion>8</javaVersion>
<project.build.outputTimestamp>2022-05-08T09:15:34Z</project.build.outputTimestamp>
</properties>
Expand Down

0 comments on commit 3bfb14d

Please sign in to comment.