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

[MINVOKER-249] InstallMojo extraArtifacts are always downloaded (optionally local repo must checked first) #5

Merged
merged 3 commits into from
Sep 2, 2019
Merged
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
68 changes: 49 additions & 19 deletions src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
* under the License.
*/


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
Expand All @@ -46,6 +49,7 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
Expand Down Expand Up @@ -161,6 +165,15 @@ public class InstallMojo
@Component
private DependencyResolver resolver;


/**
* if the local repository is not used as test repo, the parameter can force get artifacts from local repo
* if available instead of download the artifacts again.
* @since 3.2.1
*/
@Parameter( property = "invoker.useLocalRepository", defaultValue = "false" )
private boolean useLocalRepository;

private ProjectBuildingRequest projectBuildingRequest;

/**
Expand Down Expand Up @@ -486,22 +499,6 @@ protected void collectAllProjectReferences( MavenProject project, Collection<Str
}
}

protected boolean isInProjectReferences( Collection<MavenProject> references, MavenProject project )
{
if ( references == null || references.isEmpty() )
{
return false;
}
for ( MavenProject mavenProject : references )
{
if ( StringUtils.equals( mavenProject.getId(), project.getId() ) )
{
return true;
}
}
return false;
}

private void copyArtifact( Artifact artifact )
throws MojoExecutionException
{
Expand Down Expand Up @@ -618,8 +615,30 @@ private void installExtraArtifacts( String[] extraArtifacts )
coordinate.setType( type );
coordinate.setClassifier( classifier );

resolver.resolveDependencies( projectBuildingRequest, coordinate,
new PatternExclusionsFilter( Collections.<String>emptyList() ) );

if ( !localRepository.getBasedir().equals( localRepositoryPath.getPath() ) && useLocalRepository )
{
String previousId = localRepository.getId();
try
{
// using another request with the correct target repo
ProjectBuildingRequest projectBuildingRequest = repositoryManager
.setLocalRepositoryBasedir( session.getProjectBuildingRequest(),
localRepositoryPath );
projectBuildingRequest.setRemoteRepositories( Arrays.asList( localRepository ) );
resolver.resolveDependencies( projectBuildingRequest, coordinate,
new PatternExclusionsFilter( Collections.<String>emptyList() ) );
}
finally
{
localRepository.setId( previousId );
}
}
else
{
resolver.resolveDependencies( projectBuildingRequest, coordinate,
new PatternExclusionsFilter( Collections.<String>emptyList() ) );
}
}
catch ( DependencyResolverException e )
{
Expand All @@ -628,4 +647,15 @@ private void installExtraArtifacts( String[] extraArtifacts )
}
}

// FIXME could be simplify with using lambda... maybe in the next century... :P
private List<Artifact> toArtifactsList( Iterable<ArtifactResult> artifactResults )
{
List<Artifact> artifacts = new ArrayList<>( );
for ( ArtifactResult artifactResult : artifactResults )
{
artifacts.add( artifactResult.getArtifact() );
}
return artifacts;
}

}