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)

Signed-off-by: olivier lamy <olamy@apache.org>
  • Loading branch information
olamy committed Jul 26, 2019
1 parent 42cdc2f commit ecb35c2
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ under the License.
<goal>clean</goal>
<goal>initialize</goal>
</goals>
<extraArtifacts>
<extraArtifact>junit:junit:3.8.2</extraArtifact>
<extraArtifact>org.apache.maven.plugins:maven-clean-plugin:2.4:maven-plugin</extraArtifact>
<extraArtifact>org.apache.maven.plugins:maven-clean-plugin:2.4:jar:javadoc</extraArtifact>
<extraArtifact>jdom:jdom:1.1</extraArtifact>
</extraArtifacts>
</configuration>
</plugin>
</plugins>
Expand Down
71 changes: 71 additions & 0 deletions src/it/install-extra-artifacts-local-repo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.invoker</groupId>
<artifactId>stage-extras</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<description>
Test to check for staging of external dependencies directly from local repository (MINVOKER-102).
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>@pom.version@</version>
<configuration>
<useLocalRepository>true</useLocalRepository>
<localRepositoryPath>${project.build.directory}/it-repo</localRepositoryPath>
<extraArtifacts>
<!-- gid:aid:version -->
<extraArtifact>junit:junit:3.8.2</extraArtifact>
<!-- gid:aid:version:type -->
<extraArtifact>org.apache.maven.plugins:maven-clean-plugin:2.4:maven-plugin</extraArtifact>
<!-- gid:aid:version:type:classifier -->
<extraArtifact>org.apache.maven.plugins:maven-clean-plugin:2.4:jar:javadoc</extraArtifact>
<!-- relocated -->
<extraArtifact>jdom:jdom:1.1</extraArtifact>
</extraArtifacts>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>initialize</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
69 changes: 69 additions & 0 deletions src/it/install-extra-artifacts-local-repo/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.*;
import java.util.*;
import java.util.regex.*;

try
{
File itRepoDir = new File( basedir, "target/it-repo" );
if ( !itRepoDir.isDirectory() )
{
System.out.println( "IT local repository missing: " + itRepoDir );
return false;
}

String[] files =
{
"org/apache/maven/plugins/maven-clean-plugin/2.4/maven-clean-plugin-2.4.jar",
"org/apache/maven/plugins/maven-clean-plugin/2.4/maven-clean-plugin-2.4.pom",
"org/apache/maven/plugins/maven-clean-plugin/2.4/maven-clean-plugin-2.4-javadoc.jar",
"org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom",
"org/apache/maven/maven-parent/15/maven-parent-15.pom",
"org/apache/apache/6/apache-6.pom",
"org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar",
"org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.pom",
"junit/junit/3.8.2/junit-3.8.2.jar",
"junit/junit/3.8.2/junit-3.8.2.pom",
"jdom/jdom/1.1/jdom-1.1.pom",
"org/jdom/jdom/1.1/jdom-1.1.pom",
"org/jdom/jdom/1.1/jdom-1.1.jar",
};
for ( String file : files )
{
File stagedFile = new File( itRepoDir, file );
System.out.println( "Checking for existence of: " + stagedFile );
if ( !stagedFile.isFile() )
{
throw new IllegalStateException( "Missing: " + stagedFile );
}
if ( file.endsWith( "jdom-1.1.jar" ) && stagedFile.length() < 1024 * 10 )
{
throw new IllegalStateException( "Corrupt: " + stagedFile );
}
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
72 changes: 59 additions & 13 deletions src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@
* under the License.
*/

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

import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
Expand All @@ -46,12 +37,25 @@
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.install.ArtifactInstallerException;
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;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
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;

/**
* Installs the project artifacts of the main build into the local repository as a preparation to run the sub projects.
* More precisely, all artifacts of the project itself, all its locally reachable parent POMs and all its dependencies
Expand Down Expand Up @@ -161,6 +165,14 @@ 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 @@ -205,8 +217,19 @@ private void createTestRepository()
{
throw new MojoExecutionException( "Failed to create directory: " + localRepositoryPath );
}
projectBuildingRequest =
repositoryManager.setLocalRepositoryBasedir( session.getProjectBuildingRequest(), localRepositoryPath );

// we just don't want to download everything again..
if ( !localRepository.getBasedir().equals( localRepositoryPath.getPath() ) && useLocalRepository )
{
projectBuildingRequest =
repositoryManager.setLocalRepositoryBasedir( session.getProjectBuildingRequest(),
new File( localRepository.getBasedir() ) );
}
else
{
projectBuildingRequest =
repositoryManager.setLocalRepositoryBasedir( session.getProjectBuildingRequest(), localRepositoryPath );
}
}

/**
Expand Down Expand Up @@ -618,14 +641,37 @@ private void installExtraArtifacts( String[] extraArtifacts )
coordinate.setType( type );
coordinate.setClassifier( classifier );

resolver.resolveDependencies( projectBuildingRequest, coordinate,
new PatternExclusionsFilter( Collections.<String>emptyList() ) );
Iterable<ArtifactResult> artifactResults =
resolver.resolveDependencies( projectBuildingRequest, coordinate,
new PatternExclusionsFilter( Collections.<String>emptyList() ) );
if ( !localRepository.getBasedir().equals( localRepositoryPath.getPath() ) && useLocalRepository )
{
// using another request with the correct target repo
installer.install( repositoryManager.setLocalRepositoryBasedir( session.getProjectBuildingRequest(),
localRepositoryPath ),
toArtifactsList( artifactResults ) );
}
}
catch ( DependencyResolverException e )
{
throw new MojoExecutionException( "Unable to resolve dependencies for: " + coordinate, e );
}
catch ( ArtifactInstallerException e )
{
throw new MojoExecutionException( "Fail to install artifacts: " + coordinate, e );
}
}
}

// 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 ecb35c2

Please sign in to comment.