Author: pgier
Date: Fri Oct 19 03:13:07 2012
New Revision: 1399955
URL: http://svn.apache.org/viewvc?rev=1399955&view=rev
Log:
[MDEP-173] Add options for includes when purging local repository
Patch submitted by: Nicolas Cazottes
o Patch applied with some additional improvements
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/invoker.properties
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/pom.xml
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/setup.bsh
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/test.properties
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/verify.bsh
maven/plugins/trunk/maven-dependency-plugin/src/site/apt/examples/purging-local-repository.apt.vm
Modified:
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/PurgeLocalRepositoryMojo.java
maven/plugins/trunk/maven-dependency-plugin/src/site/site.xml
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/invoker.properties
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/invoker.properties?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/invoker.properties
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/invoker.properties
Fri Oct 19 03:13:07 2012
@@ -0,0 +1 @@
+invoker.goals =
${project.groupId}:${project.artifactId}:${project.version}:purge-local-repository
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/pom.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/pom.xml?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/pom.xml
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/pom.xml
Fri Oct 19 03:13:07 2012
@@ -0,0 +1,39 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.dependency</groupId>
+ <artifactId>test</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <name>Test</name>
+ <description>
+ Test dependency:purge-local-repository-manual-include
+ </description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+</project>
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/setup.bsh
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/setup.bsh?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/setup.bsh
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/setup.bsh
Fri Oct 19 03:13:07 2012
@@ -0,0 +1,10 @@
+import java.io.*;
+
+File purgedJar = new File( localRepositoryPath,
"org/apache/maven/its/dependency/purged/1.1/purged-1.1.jar" );
+
+purgedJar.getParentFile().mkdirs();
+purgedJar.createNewFile();
+
+System.out.println( "Created dummy JAR " + purgedJar );
+
+return true;
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/test.properties
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/test.properties?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/test.properties
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/test.properties
Fri Oct 19 03:13:07 2012
@@ -0,0 +1 @@
+manualInclude = org.apache.maven.its.dependency:purged
Added:
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/verify.bsh
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/verify.bsh?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/verify.bsh
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/it/purge-local-repository-manual-include/verify.bsh
Fri Oct 19 03:13:07 2012
@@ -0,0 +1,12 @@
+import java.io.*;
+
+File purgedJar = new File( localRepositoryPath,
"org/apache/maven/its/dependency/purged/1.1/purged-1.1.jar" );
+
+System.out.println( "Checking for absence of dummy JAR " + purgedJar );
+
+if ( purgedJar.exists() )
+{
+ throw new Exception( "JAR was not purged: " + purgedJar );
+}
+
+return true;
Modified:
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/PurgeLocalRepositoryMojo.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/PurgeLocalRepositoryMojo.java?rev=1399955&r1=1399954&r2=1399955&view=diff
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/PurgeLocalRepositoryMojo.java
(original)
+++
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/PurgeLocalRepositoryMojo.java
Fri Oct 19 03:13:07 2012
@@ -82,7 +82,53 @@ public class PurgeLocalRepositoryMojo
/**
* The list of dependencies in the form of groupId:artifactId which should
- * NOT be deleted/refreshed. This is useful for third-party artifacts.
+ * BE deleted/purged from the local repository. Note that using this
+ * parameter will deactivate the normal process for purging the current
project
+ * dependency tree. If this parameter is used, only the included
artifacts will
+ * be purged.
+ *
+ * The includes parameter should not be used in combination with the
+ * includes/excludes parameters.
+ *
+ * @since 2.6
+ */
+ @Parameter
+ private List<String> manualIncludes;
+
+ /**
+ * Comma-separated list of groupId:artifactId entries, which should be used
+ * to manually include artifacts for deletion. This is a command-line
+ * alternative to the <code>manualIncludes</code> parameter, since List
+ * parameters are not currently compatible with CLI specification.
+ *
+ * @since 2.6
+ */
+ @Parameter( property = "manualInclude" )
+ private String manualInclude;
+
+ /**
+ * The list of dependencies in the form of groupId:artifactId which should
+ * BE deleted/refreshed.
+ *
+ * @since 2.6
+ */
+ @Parameter
+ private List<String> includes;
+
+ /**
+ * Comma-separated list of groupId:artifactId entries, which should be used
+ * to include artifacts for deletion/refresh. This is a command-line
+ * alternative to the <code>includes</code> parameter, since List
+ * parameters are not currently compatible with CLI specification.
+ *
+ * @since 2.6
+ */
+ @Parameter( property = "include" )
+ private String include;
+
+ /**
+ * The list of dependencies in the form of groupId:artifactId which should
+ * NOT be deleted/refreshed.
*/
@Parameter
private List<String> excludes;
@@ -176,13 +222,24 @@ public class PurgeLocalRepositoryMojo
public void execute()
throws MojoExecutionException, MojoFailureException
{
- List<String> exclusionPatterns = buildExclusionPatternsList();
+
+ List<String> manualInclusionPatterns = buildInclusionPatternsList(
manualIncludes, manualInclude );
+
+ if ( manualInclusionPatterns.size() > 0 )
+ {
+ manualPurge( manualInclusionPatterns );
+ return;
+ }
+
+ List<String> inclusionPatterns = buildInclusionPatternsList(includes,
include);
+
+ List<String> exclusionPatterns = buildInclusionPatternsList(excludes,
exclude);
for ( MavenProject project : projects )
{
try
{
- refreshDependenciesForProject( project, exclusionPatterns );
+ refreshDependenciesForProject( project, inclusionPatterns,
exclusionPatterns );
}
catch ( ArtifactResolutionException e )
{
@@ -196,30 +253,80 @@ public class PurgeLocalRepositoryMojo
}
}
- private List<String> buildExclusionPatternsList()
+ /**
+ * Purge artifacts from the local repository according to the given
patterns.
+ *
+ * @param inclusionPatterns
+ * @throws MojoExecutionException
+ */
+ private void manualPurge( List<String> inclusionPatterns )
+ throws MojoExecutionException
+ {
+ for ( String pattern : inclusionPatterns )
+ {
+ if ( pattern.isEmpty() )
+ {
+ throw new MojoExecutionException( "The groupId:artifactId for
manualIncludes cannot be empty" );
+ }
+ String relativePath = gaStringtoPath( pattern );
+ File purgeDir = new File( localRepository.getBasedir() + "/" +
relativePath );
+ if ( purgeDir.exists() )
+ {
+ try
+ {
+ verbose( "Deleting directory: " + purgeDir );
+ FileUtils.deleteDirectory( purgeDir );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Unable to purge
directory: " + purgeDir );
+ }
+ }
+ }
+ }
+
+ /**
+ * Convert a groupId:artifactId to a file system path
+ *
+ * @param ga
+ * @return
+ */
+ private String gaStringtoPath( String ga )
+ {
+ if ( ga == null || ga.equals( "" ) )
+ {
+ return null;
+ }
+ // Replace wildcard with empty path
+ String path = ga.replace( ":*", "" );
+ path = path.replace( '.', '/' ).replace( ':', '/' );
+ return path;
+ }
+
+ private List<String> buildInclusionPatternsList(List<String> includes,
String include)
{
List<String> patterns = new ArrayList<String>();
- if ( exclude != null )
+ if ( include != null )
{
- String[] elements = exclude.split( " ?, ?" );
+ String[] elements = include.split( " ?, ?" );
patterns.addAll( Arrays.asList( elements ) );
}
- else if ( excludes != null && !excludes.isEmpty() )
+ else if ( includes != null && !includes.isEmpty() )
{
- patterns.addAll( excludes );
+ patterns.addAll( includes );
}
return patterns;
}
/**
- * Map the groupId:artifactId to the artifact object
+ * Map the groupId:artifactId identifiers to the artifact objects for the
current project
* @param project The current Maven project
* @return
*/
- private Map<String, Artifact> createArtifactMap( MavenProject project )
+ private Map<String, Artifact> createProjectArtifactMap( MavenProject
project )
{
Map<String, Artifact> artifactMap = Collections.emptyMap();
@@ -304,36 +411,77 @@ public class PurgeLocalRepositoryMojo
}
}
- private void refreshDependenciesForProject( MavenProject project,
List<String> exclusionPatterns )
+ private void refreshDependenciesForProject( MavenProject project,
List<String> inclusionPatterns, List<String> exclusionPatterns )
throws ArtifactResolutionException, MojoFailureException
{
- Map<String, Artifact> deps = createArtifactMap( project );
+ Map<String, Artifact> artifactMap = createProjectArtifactMap( project
);
- if ( deps.isEmpty() )
+ if ( artifactMap.isEmpty() )
{
getLog().info( "Nothing to do for project: " + project.getId() );
return;
}
+ Map<String, Artifact> depsAfterInclusion = new HashMap<String,
Artifact>();
+
+ if ( !inclusionPatterns.isEmpty() )
+ {
+ for ( Iterator<Map.Entry<String, Artifact>> artifactIter =
artifactMap.entrySet().iterator(); artifactIter.hasNext(); )
+ {
+ Map.Entry<String, Artifact> artifactEntry =
artifactIter.next();
+
+ Artifact artifact = artifactEntry.getValue();
+
+ if ( resolutionFuzziness.equals( GROUP_ID_FUZZINESS ) )
+ {
+ if ( inclusionPatterns.contains( artifact.getGroupId() ) )
+ {
+ verbose( "Including groupId: " + artifact.getGroupId()
+ " for refresh operation for project: "
+ + project.getId() );
+ depsAfterInclusion.put( artifactEntry.getKey(),
artifactEntry.getValue() );
+ }
+ }
+ else
+ {
+ String artifactKey = ArtifactUtils.versionlessKey(
artifact );
+ if ( inclusionPatterns.contains( artifactKey ) )
+ {
+ verbose( "Including artifact: " + artifactKey + " for
refresh operation for project: "
+ + project.getId() );
+ depsAfterInclusion.put( artifactEntry.getKey(),
artifactEntry.getValue() );
+ }
+ }
+ }
+
+ if ( depsAfterInclusion.isEmpty() )
+ {
+ getLog().info( "Nothing to include for project: " +
project.getId() + ". Ending purge." );
+ return;
+ }
+
+ // replacing deps by the one included in order to apply the
exclusion pattern.
+ artifactMap = depsAfterInclusion;
+ }
+
if ( !exclusionPatterns.isEmpty() )
{
for ( String excludedKey : exclusionPatterns )
{
- if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) )
+ if ( resolutionFuzziness.equals( GROUP_ID_FUZZINESS ) )
{
verbose( "Excluding groupId: " + excludedKey + " from
refresh operation for project: "
+ project.getId() );
- for ( Iterator<Map.Entry<String, Artifact>> deps_it =
deps.entrySet().iterator();
- deps_it.hasNext(); )
+ for ( Iterator<Map.Entry<String, Artifact>> artifactIter =
artifactMap.entrySet().iterator();
+ artifactIter.hasNext(); )
{
- Map.Entry<String, Artifact> dependency =
deps_it.next();
+ Map.Entry<String, Artifact> artifactEntry =
artifactIter.next();
- Artifact artifact = dependency.getValue();
+ Artifact artifact = artifactEntry.getValue();
if ( artifact.getGroupId().equals( excludedKey ) )
{
- deps_it.remove();
+ artifactIter.remove();
}
}
}
@@ -341,7 +489,7 @@ public class PurgeLocalRepositoryMojo
{
verbose( "Excluding: " + excludedKey + " from refresh
operation for project: " + project.getId() );
- deps.remove( excludedKey );
+ artifactMap.remove( excludedKey );
}
}
}
@@ -349,7 +497,7 @@ public class PurgeLocalRepositoryMojo
verbose( "Processing dependencies for project: " + project.getId() );
List<Artifact> missingArtifacts = new ArrayList<Artifact>();
- for ( Map.Entry<String, Artifact> entry : deps.entrySet() )
+ for ( Map.Entry<String, Artifact> entry : artifactMap.entrySet() )
{
Artifact artifact = entry.getValue();
@@ -420,43 +568,8 @@ public class PurgeLocalRepositoryMojo
if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) )
{
- // get the artifactId dir.
- deleteTarget = deleteTarget.getParentFile().getParentFile();
-
- // get the first groupId dir.
- deleteTarget = deleteTarget.getParentFile();
-
- String[] path = localRepository.pathOf( artifact ).split( "\\/" );
-
- // subtract the artifact filename, version dir, artifactId dir, and
- // the first groupId
- // dir, since we've accounted for those above.
- int groupParts = path.length - 4;
-
- File parent = deleteTarget.getParentFile();
- int count = 0;
- while ( count++ < groupParts )
- {
- // prune empty dirs back to the beginning of the groupId, if
- // possible.
-
- // if the parent dir only has the one child file, then it's
okay
- // to prune.
- if ( parent.list().length < 2 )
- {
- deleteTarget = parent;
-
- // check the parent of this newly checked dir
- parent = deleteTarget.getParentFile();
- }
- else
- {
- // if there are more files than the one that we're
- // interested in killing, stop.
- break;
- }
- }
-
+ // get the groupId dir.
+ deleteTarget =
deleteTarget.getParentFile().getParentFile().getParentFile();
}
else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) )
{
Added:
maven/plugins/trunk/maven-dependency-plugin/src/site/apt/examples/purging-local-repository.apt.vm
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/site/apt/examples/purging-local-repository.apt.vm?rev=1399955&view=auto
==============================================================================
---
maven/plugins/trunk/maven-dependency-plugin/src/site/apt/examples/purging-local-repository.apt.vm
(added)
+++
maven/plugins/trunk/maven-dependency-plugin/src/site/apt/examples/purging-local-repository.apt.vm
Fri Oct 19 03:13:07 2012
@@ -0,0 +1,134 @@
+~~ 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.
+
+ ------
+ Purging project dependencies
+ ------
+ Nicolas Cazottes
+ Paul Gier
+ ------
+ Oct 2012
+ ------
+
+Purging local repository dependencies
+
+ The purpose of the <<<dependency:purge-local-repository>>> goal is to purge
+ (delete and optionally re-resolve) artifacts from the local maven repository.
+ This page describes some of the configuration options available to the
plugin.
+
+* Basic usage
+
+ The default behaviour of the plugin is to first resolve the entire dependency
+ tree, then delete the contents from the local repository, and then re-resolve
+ the dependencies from the remote repository.
+
++---+
+mvn dependency:purge-local-repository
++---+
+
+* Transitive dependency resolution
+
+ By default, the plugin operates on all transitive dependencies. This means
that
+ the plugin may download certain missing dependencies to gather the full
+ dependency tree information before beginning the purge process.
+
+ To avoid this pre-download step, the plugin can be configured to operate only
+ on the direct dependencies of the project using the "actTranstively"
parameter.
+
++---+
+mvn dependency:purge-local-repository -DactTransitively=false
++---+
+
+* Dependency includes/excludes
+
+ Specific dependencies from the current project dependency tree
+ can be included or excluded from the purging and re-resolution
+ process using the "includes" or "excludes" parameters. By default,
+ all project dependencies are purged. The "includes" option changes
+ this behaviour to only purge the included artifacts. The "excludes"
+ option can be used to prevent purging/refreshing of certain dependencies.
+
++---+
+<project>
+ [...]
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <id>purge-local-dependencies</id>
+ <phase>process-sources</phase>
+ <goals>
+ <goal>purge-local-repository</goal>
+ </goals>
+ <configuration>
+ <excludes>
+ <exclude>[groupId1]:[artifactId1]</exclude>
+ <exclude>[groupId2]:[artifactId2]</exclude>
+ </excludes>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ [...]
+</project>
++---+
+
+ The includes/excludes configuration can also be passed from the CLI using
the "include"
+ or "exclude" parameter.
+
++---+
+mvn dependency:purge-local-repository
-Dinclude=org.slf4j:slf4j-api,org.slf4j:log4j-over-slf4j
++---+
+
++---+
+mvn dependency:purge-local-repository -Dinclude=org.slf4j
-DresolutionFuzziness=groupId -Dverbose=true
++---+
+
+
+* Manual includes
+
+ Specific dependencies which are not part of the current project dependency
tree
+ can be purged by using the "manualIncludes" or "manualInclude" parameters.
Using
+ this parameter will de-activate the normal purging process and only the
included
+ groupIds/artifactIds will be purged. Any manually included purge artifacts
will
+ be removed from the local repository and will not be re-resolved until they
are
+ needed. This can be useful for example to refresh the parent pom, an
imported
+ pom, or a maven plugin.
+
++---+
+mvn dependency:purge-local-repository -DmanualInclude=org.apache:apache
++---+
+
+
+* Other purge configuration
+
+ To purge all the dependencies of a given groupId, use the
<<<resolutionFuzziness>>>
+ configuration parameter.
+
+ If neither <<<includes>>> nor <<<excludes>>> are specified, all the
dependencies
+ of the current project are purged.
+
+ You can see the complete mojo documentation in
+ {{{../purge-local-repository-mojo.html}dependency:purge-local-repository}}.
+
+
Modified: maven/plugins/trunk/maven-dependency-plugin/src/site/site.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/site/site.xml?rev=1399955&r1=1399954&r2=1399955&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/site/site.xml (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/site/site.xml Fri Oct 19
03:13:07 2012
@@ -40,6 +40,7 @@ under the License.
<item name="Failing the build on dependency analysis warnings"
href="examples/failing-the-build-on-dependency-analysis-warnings.html"/>
<item name="Filtering the dependency tree"
href="examples/filtering-the-dependency-tree.html"/>
<item name="Resolving conflicts using the dependency tree"
href="examples/resolving-conflicts-using-the-dependency-tree.html"/>
+ <item name="Purging local repository dependencies"
href="examples/purging-local-repository.html"/>
</menu>
<menu name="Resources">
<item name="Dependency Mechanism"
href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html"
/>