Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Resolver Api for dependency resolving #349

Merged
merged 1 commit into from
Nov 24, 2022
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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<!-- bound version with used Maven -->
<version>1.0.0.v20140518</version>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down
56 changes: 30 additions & 26 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
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.plugins.annotations.ResolutionScope;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.DependencyResult;
import org.eclipse.aether.util.filter.DependencyFilterUtils;

/**
* Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.
Expand All @@ -47,10 +53,6 @@ public class ExecJavaMojo
@Component
private RepositorySystem repositorySystem;

@Component
private ResolutionErrorHandler resolutionErrorHandler;


/**
* @since 1.0
*/
Expand Down Expand Up @@ -642,10 +644,8 @@ private void addRelevantPluginDependenciesToClasspath( List<Path> path )
* Add any relevant project dependencies to the classpath. Takes includeProjectDependencies into consideration.
*
* @param path classpath of {@link java.net.URL} objects
* @throws MojoExecutionException if a problem happens
*/
private void addRelevantProjectDependenciesToClasspath( List<Path> path )
throws MojoExecutionException
{
if ( this.includeProjectDependencies )
{
Expand Down Expand Up @@ -692,7 +692,7 @@ private Set<Artifact> determineRelevantPluginDependencies()
if ( this.executableDependency == null )
{
getLog().debug( "All Plugin Dependencies will be included." );
relevantDependencies = new HashSet<Artifact>( this.getPluginDependencies() );
relevantDependencies = new HashSet<>( this.getPluginDependencies() );
}
else
{
Expand All @@ -712,32 +712,36 @@ private Set<Artifact> determineRelevantPluginDependencies()
/**
* Resolve the executable dependencies for the specified project
*
* @param executablePomArtifact the project's POM
* @param executableArtifact the executable plugin dependency
* @return a set of Artifacts
* @throws MojoExecutionException if a failure happens
*/
private Set<Artifact> resolveExecutableDependencies( Artifact executablePomArtifact )
private Set<Artifact> resolveExecutableDependencies( Artifact executableArtifact )
throws MojoExecutionException
{
try
{
ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setArtifact( executablePomArtifact )
.setLocalRepository( getSession().getLocalRepository() )
.setRemoteRepositories( getSession().getRequest().getRemoteRepositories() )
.setForceUpdate( getSession().getRequest().isUpdateSnapshots() )
.setOffline( getSession().isOffline() )
.setResolveTransitively( true );
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(
new Dependency( RepositoryUtils.toArtifact( executableArtifact ), classpathScope ) );
collectRequest.setRepositories( project.getRemotePluginRepositories() );

DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter( classpathScope );

DependencyRequest dependencyRequest = new DependencyRequest( collectRequest, classpathFilter );

ArtifactResolutionResult result = repositorySystem.resolve( request );
resolutionErrorHandler.throwErrors( request, result );
DependencyResult dependencyResult =
repositorySystem.resolveDependencies( getSession().getRepositorySession(), dependencyRequest );

return result.getArtifacts();
return dependencyResult.getArtifactResults().stream()
.map( ArtifactResult::getArtifact )
.map( RepositoryUtils::toArtifact )
.collect( Collectors.toSet() );
}
catch ( ArtifactResolutionException ex )
catch ( DependencyResolutionException ex )
{
throw new MojoExecutionException( "Encountered problems resolving dependencies of the executable "
+ "in preparation for its execution.", ex );
+ "in preparation for its execution.", ex );
}
}

Expand Down