From ecb35c2e729e130581075a2bc33d8b770b8d072c Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Fri, 26 Jul 2019 16:56:23 +1000 Subject: [PATCH] [MINVOKER-249] InstallMojo extraArtifacts are always downloaded (optionally local repo must checked first) Signed-off-by: olivier lamy --- pom.xml | 6 ++ .../pom.xml | 71 ++++++++++++++++++ .../verify.bsh | 69 ++++++++++++++++++ .../maven/plugins/invoker/InstallMojo.java | 72 +++++++++++++++---- 4 files changed, 205 insertions(+), 13 deletions(-) create mode 100644 src/it/install-extra-artifacts-local-repo/pom.xml create mode 100644 src/it/install-extra-artifacts-local-repo/verify.bsh diff --git a/pom.xml b/pom.xml index 5461d7ab..342467b6 100644 --- a/pom.xml +++ b/pom.xml @@ -333,6 +333,12 @@ under the License. clean initialize + + junit:junit:3.8.2 + org.apache.maven.plugins:maven-clean-plugin:2.4:maven-plugin + org.apache.maven.plugins:maven-clean-plugin:2.4:jar:javadoc + jdom:jdom:1.1 + diff --git a/src/it/install-extra-artifacts-local-repo/pom.xml b/src/it/install-extra-artifacts-local-repo/pom.xml new file mode 100644 index 00000000..6634ce06 --- /dev/null +++ b/src/it/install-extra-artifacts-local-repo/pom.xml @@ -0,0 +1,71 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.invoker + stage-extras + 1.0-SNAPSHOT + pom + + + Test to check for staging of external dependencies directly from local repository (MINVOKER-102). + + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-invoker-plugin + @pom.version@ + + true + ${project.build.directory}/it-repo + + + junit:junit:3.8.2 + + org.apache.maven.plugins:maven-clean-plugin:2.4:maven-plugin + + org.apache.maven.plugins:maven-clean-plugin:2.4:jar:javadoc + + jdom:jdom:1.1 + + + + + integration-test + initialize + + install + + + + + + + + diff --git a/src/it/install-extra-artifacts-local-repo/verify.bsh b/src/it/install-extra-artifacts-local-repo/verify.bsh new file mode 100644 index 00000000..ba95e363 --- /dev/null +++ b/src/it/install-extra-artifacts-local-repo/verify.bsh @@ -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; diff --git a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java index 63543c9f..0610d8c7 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java @@ -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; @@ -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 @@ -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; /** @@ -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 ); + } } /** @@ -618,14 +641,37 @@ private void installExtraArtifacts( String[] extraArtifacts ) coordinate.setType( type ); coordinate.setClassifier( classifier ); - resolver.resolveDependencies( projectBuildingRequest, coordinate, - new PatternExclusionsFilter( Collections.emptyList() ) ); + Iterable artifactResults = + resolver.resolveDependencies( projectBuildingRequest, coordinate, + new PatternExclusionsFilter( Collections.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 toArtifactsList( Iterable artifactResults ) + { + List artifacts = new ArrayList<>( ); + for ( ArtifactResult artifactResult : artifactResults ) + { + artifacts.add( artifactResult.getArtifact() ); } + return artifacts; } }