Author: brianf Date: Mon Jan 22 20:15:57 2007 New Revision: 498910 URL: http://svn.apache.org/viewvc?view=rev&rev=498910 Log: MDEP-50 - branch to workout versionspec artifact resolution
Modified: maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/it/mdep-50/pom.xml maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/AbstractDependencyMojo.java maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java Modified: maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/it/mdep-50/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/it/mdep-50/pom.xml?view=diff&rev=498910&r1=498909&r2=498910 ============================================================================== --- maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/it/mdep-50/pom.xml (original) +++ maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/it/mdep-50/pom.xml Mon Jan 22 20:15:57 2007 @@ -5,18 +5,6 @@ <artifactId>mdep-50-it</artifactId> <version>1</version> <packaging>jar</packaging> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>[3.7,4.0)</version> - </dependency> - <dependency> - <groupId>ant</groupId> - <artifactId>ant</artifactId> - <version>1.6</version> - </dependency> - </dependencies> <build> <defaultGoal>generate-sources</defaultGoal> <plugins> @@ -37,6 +25,11 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> <version>[3.7,4.0)</version> + </artifactItem> + <artifactItem> + <groupId>ant</groupId> + <artifactId>ant</artifactId> + <version>[1.1,1.5)</version> </artifactItem> </artifactItems> </configuration> Modified: maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/AbstractDependencyMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/AbstractDependencyMojo.java?view=diff&rev=498910&r1=498909&r2=498910 ============================================================================== --- maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/AbstractDependencyMojo.java (original) +++ maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/AbstractDependencyMojo.java Mon Jan 22 20:15:57 2007 @@ -24,6 +24,8 @@ import java.lang.reflect.Field; import java.util.List; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.resolver.ArtifactCollector; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.dependency.utils.SilentLog; @@ -61,6 +63,22 @@ */ protected org.apache.maven.artifact.resolver.ArtifactResolver resolver; + /** + * Artifact collector, needed to resolve dependencies. + * + * @component role="org.apache.maven.artifact.resolver.ArtifactCollector" + * @required + * @readonly + */ + protected ArtifactCollector artifactCollector; + + /** + * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource" hint="maven" + * @required + * @readonly + */ + protected ArtifactMetadataSource artifactMetadataSource; + /** * Location of the local repository. * Modified: maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java?view=diff&rev=498910&r1=498909&r2=498910 ============================================================================== --- maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java (original) +++ maven/plugins/branches/maven-dependency-plugin-MDEP-50/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java Mon Jan 22 20:15:57 2007 @@ -21,13 +21,30 @@ import java.io.File; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.DebugResolutionListener; +import org.apache.maven.artifact.resolver.ResolutionNode; +import org.apache.maven.artifact.resolver.WarningResolutionListener; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; +import org.apache.maven.model.Exclusion; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.dependency.AbstractDependencyMojo; import org.apache.maven.plugin.dependency.utils.DependencyUtil; @@ -170,6 +187,75 @@ } /** + * Returns the list of project artifacts. Also artifacts generated from + * referenced projects will be added, but with the <code>resolved</code> + * property set to true. + * + * @return list of projects artifacts + * @throws MojoExecutionException + * if unable to parse dependency versions + */ + private Set getProjectArtifacts() + throws MojoExecutionException + { + // keep it sorted, this should avoid random classpath order in tests + Set artifacts = new TreeSet(); + + for ( Iterator dependencies = getProject().getDependencies().iterator(); dependencies.hasNext(); ) + { + Dependency dependency = (Dependency) dependencies.next(); + + String groupId = dependency.getGroupId(); + String artifactId = dependency.getArtifactId(); + VersionRange versionRange; + try + { + versionRange = VersionRange.createFromVersionSpec( dependency.getVersion() ); + } + catch ( InvalidVersionSpecificationException e ) + { + throw new MojoExecutionException( "unable to parse version", e ); + } + + String type = dependency.getType(); + if ( type == null ) + { + type = "jar"; //$NON-NLS-1$ + } + String classifier = dependency.getClassifier(); + boolean optional = dependency.isOptional(); + String scope = dependency.getScope(); + if ( scope == null ) + { + scope = Artifact.SCOPE_COMPILE; + } + + Artifact art = factory.createDependencyArtifact( groupId, artifactId, versionRange, type, classifier, + scope, optional ); + + if ( scope.equalsIgnoreCase( Artifact.SCOPE_SYSTEM ) ) + { + art.setFile( new File( dependency.getSystemPath() ) ); + } + + List exclusions = new ArrayList(); + for ( Iterator j = dependency.getExclusions().iterator(); j.hasNext(); ) + { + Exclusion e = (Exclusion) j.next(); + exclusions.add( e.getGroupId() + ":" + e.getArtifactId() ); //$NON-NLS-1$ + } + + ArtifactFilter newFilter = new ExcludesArtifactFilter( exclusions ); + + art.setDependencyFilter( newFilter ); + + artifacts.add( art ); + } + + return artifacts; + } + + /** * Resolves the Artifact from the remote repository if nessessary. If no * version is specified, it will be retrieved from the dependency list or * from the DependencyManagement section of the pom. @@ -188,21 +274,48 @@ { Artifact artifact; + Map managedVersions = createManagedVersionMap( factory, project.getId(), project.getDependencyManagement() ); + VersionRange vr; + try + { + vr = VersionRange.createFromVersionSpec( artifactItem.getVersion() ); + } + catch ( InvalidVersionSpecificationException e1 ) + { + // TODO Auto-generated catch block + e1.printStackTrace(); + vr = VersionRange.createFromVersion( artifactItem.getVersion() ); + } + if ( StringUtils.isEmpty( artifactItem.getClassifier() ) ) { - artifact = factory.createArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem - .getVersion(), Artifact.SCOPE_PROVIDED, artifactItem.getType() ); + artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, + artifactItem.getType(), null, Artifact.SCOPE_COMPILE ); } else { - artifact = factory.createArtifactWithClassifier( artifactItem.getGroupId(), artifactItem.getArtifactId(), - artifactItem.getVersion(), artifactItem.getType(), - artifactItem.getClassifier() ); + artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, + artifactItem.getType(), artifactItem.getClassifier(), Artifact.SCOPE_COMPILE ); } try { - resolver.resolve( artifact, remoteRepos, local ); + List listeners = new ArrayList(); + + Set theSet = new HashSet(); + theSet.add( artifact ); + ArtifactResolutionResult artifactResolutionResult = artifactCollector.collect( theSet, project + .getArtifact(), managedVersions, this.local, project.getRemoteArtifactRepositories(), + artifactMetadataSource, + null, listeners ); + Iterator iter = artifactResolutionResult.getArtifactResolutionNodes().iterator(); + while ( iter.hasNext() ) + { + ResolutionNode node = (ResolutionNode) iter.next(); + artifact = node.getArtifact(); + } + + resolver.resolve( artifact, remoteRepos, local ); } catch ( ArtifactResolutionException e ) { @@ -266,6 +379,40 @@ return result; } + private Map createManagedVersionMap( ArtifactFactory artifactFactory, String projectId, + DependencyManagement dependencyManagement ) + throws MojoExecutionException + { + Map map; + if ( dependencyManagement != null && dependencyManagement.getDependencies() != null ) + { + map = new HashMap(); + for ( Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext(); ) + { + Dependency d = (Dependency) i.next(); + + try + { + VersionRange versionRange = VersionRange.createFromVersionSpec( d.getVersion() ); + Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), + versionRange, d.getType(), d + .getClassifier(), d.getScope(), d + .isOptional() ); + map.put( d.getManagementKey(), artifact ); + } + catch ( InvalidVersionSpecificationException e ) + { + throw new MojoExecutionException( "Unable to parse version", e ); + } + } + } + else + { + map = Collections.EMPTY_MAP; + } + return map; + } + /** * @return Returns the artifactItems. */ @@ -275,7 +422,8 @@ } /** - * @param theArtifactItems The artifactItems to set. + * @param theArtifactItems + * The artifactItems to set. */ public void setArtifactItems( ArrayList theArtifactItems ) { @@ -291,7 +439,8 @@ } /** - * @param theOutputDirectory The outputDirectory to set. + * @param theOutputDirectory + * The outputDirectory to set. */ public void setOutputDirectory( File theOutputDirectory ) { @@ -307,7 +456,8 @@ } /** - * @param theOverWriteIfNewer The overWriteIfNewer to set. + * @param theOverWriteIfNewer + * The overWriteIfNewer to set. */ public void setOverWriteIfNewer( boolean theOverWriteIfNewer ) { @@ -323,7 +473,8 @@ } /** - * @param theOverWriteReleases The overWriteReleases to set. + * @param theOverWriteReleases + * The overWriteReleases to set. */ public void setOverWriteReleases( boolean theOverWriteReleases ) { @@ -339,7 +490,8 @@ } /** - * @param theOverWriteSnapshots The overWriteSnapshots to set. + * @param theOverWriteSnapshots + * The overWriteSnapshots to set. */ public void setOverWriteSnapshots( boolean theOverWriteSnapshots ) {