Forked from vincent-zurczak/ ReactorDependenciesResolverMojoTest.java
Created
November 9, 2017 01:28
-
-
Save ql-owo-lp/9735e2858c01ea0cb52ffdf5dd663795 to your computer and use it in GitHub Desktop.
A Maven mojo that shows how to resolve dependencies locations, be it in remote or in the local repository, or even in the reactor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import org.apache.maven.artifact.Artifact; | |
import org.apache.maven.artifact.DefaultArtifact; | |
import org.apache.maven.artifact.handler.DefaultArtifactHandler; | |
import org.apache.maven.plugin.AbstractMojo; | |
import org.apache.maven.plugin.MojoExecutionException; | |
import org.apache.maven.project.MavenProject; | |
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; | |
import org.eclipse.aether.DefaultRepositorySystemSession; | |
import org.eclipse.aether.RepositorySystem; | |
import org.eclipse.aether.RepositorySystemSession; | |
import org.eclipse.aether.impl.DefaultServiceLocator; | |
import org.eclipse.aether.repository.LocalRepository; | |
import org.eclipse.aether.repository.LocalRepositoryManager; | |
import org.eclipse.aether.repository.RemoteRepository; | |
import org.junit.Assert; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
public class CopyOfResolveMojoTest extends AbstractTest { | |
@Rule | |
public TemporaryFolder folder = new TemporaryFolder(); | |
@Test( expected = MojoExecutionException.class ) | |
public void testWithInvalidRoboconfDependencies() throws Exception { | |
// Prepare the project | |
final String projectName = "project--valid"; | |
File baseDir = this.resources.getBasedir( projectName ); | |
Assert.assertNotNull( baseDir ); | |
Assert.assertTrue( baseDir.isDirectory()); | |
AbstractMojo mojo = ...; | |
this.rule.setVariableValueToObject( mojo, "repoSession", newRepositorySession()); | |
this.rule.setVariableValueToObject( mojo, "repositories", new ArrayList<RemoteRepository>( 0 )); | |
mojo.execute(); | |
} | |
private RepositorySystemSession newRepositorySession() throws IOException { | |
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); | |
RepositorySystem repositorySystem = locator.getService( RepositorySystem.class ); | |
LocalRepository localRepo = new LocalRepository( this.folder.newFolder()); | |
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); | |
LocalRepositoryManager lrm = repositorySystem.newLocalRepositoryManager( session, localRepo ); | |
session.setLocalRepositoryManager( lrm ); | |
return session; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import java.io.File; | |
import java.util.List; | |
import org.apache.maven.artifact.Artifact; | |
import org.apache.maven.plugin.AbstractMojo; | |
import org.apache.maven.plugin.MojoExecutionException; | |
import org.apache.maven.plugin.MojoFailureException; | |
import org.apache.maven.plugins.annotations.Component; | |
import org.apache.maven.plugins.annotations.Mojo; | |
import org.apache.maven.plugins.annotations.Parameter; | |
import org.apache.maven.project.MavenProject; | |
import org.eclipse.aether.RepositorySystem; | |
import org.eclipse.aether.RepositorySystemSession; | |
import org.eclipse.aether.artifact.DefaultArtifact; | |
import org.eclipse.aether.repository.RemoteRepository; | |
import org.eclipse.aether.resolution.ArtifactRequest; | |
import org.eclipse.aether.resolution.ArtifactResolutionException; | |
import org.eclipse.aether.resolution.ArtifactResult; | |
@Mojo( name="mojoName" ) | |
public class ReactorDependenciesResolverMojo extends AbstractMojo { | |
@Parameter( defaultValue = "${project}", readonly = true ) | |
private MavenProject project; | |
@Component | |
private RepositorySystem repoSystem; | |
@Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true ) | |
private RepositorySystemSession repoSession; | |
@Parameter( defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true ) | |
private List<RemoteRepository> repositories; | |
@Override | |
public void execute() throws MojoExecutionException, MojoFailureException { | |
for( Artifact unresolvedArtifact : this.project.getDependencyArtifacts()) { | |
// Here, it becomes messy. We ask Maven to resolve the artifact's location. | |
// It may imply downloading it from a remote repository, | |
// searching the local repository or looking into the reactor's cache. | |
// To achieve this, we must use Aether | |
// (the dependency mechanism behind Maven). | |
String artifactId = unresolvedArtifact.getArtifactId(); | |
org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact( | |
unresolvedArtifact.getGroupId(), | |
unresolvedArtifact.getArtifactId(), | |
unresolvedArtifact.getClassifier(), | |
unresolvedArtifact.getType(), | |
unresolvedArtifact.getVersion()); | |
ArtifactRequest req = new ArtifactRequest().setRepositories( this.repositories ).setArtifact( aetherArtifact ); | |
ArtifactResult resolutionResult; | |
try { | |
resolutionResult = this.repoSystem.resolveArtifact( this.repoSession, req ); | |
} catch( ArtifactResolutionException e ) { | |
throw new MojoExecutionException( "Artifact " + artifactId + " could not be resolved.", e ); | |
} | |
// The file should exists, but we never know. | |
File file = resolutionResult.getArtifact().getFile(); | |
if( file == null || ! file.exists()) { | |
getLog().warn( "Artifact " + artifactId + " has no attached file. Its content will not be copied in the target model directory." ); | |
continue; | |
} | |
// Do whatever you want with the file... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment