Skip to content

Commit

Permalink
[MINVOKER-249] InstallMojo extraArtifacts are always downloaded (opti…
Browse files Browse the repository at this point in the history
…onally local repo must checked first) (#5)

Signed-off-by: olivier lamy <[email protected]>
  • Loading branch information
olamy authored Sep 2, 2019
1 parent d278901 commit 9efcc6e
Showing 1 changed file with 49 additions and 19 deletions.
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;
}

}

0 comments on commit 9efcc6e

Please sign in to comment.