Author: brianf
Date: Sun Mar 11 19:52:32 2007
New Revision: 517085

URL: http://svn.apache.org/viewvc?view=rev&rev=517085
Log:
mdep-67 - fixed npe and added new use case in finding missing version.

Added:
    maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/
    maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/pom.xml
Modified:
    
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
    
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java

Added: maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/pom.xml?view=auto&rev=517085
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/pom.xml (added)
+++ maven/plugins/trunk/maven-dependency-plugin/src/it/mdep-67/pom.xml Sun Mar 
11 19:52:32 2007
@@ -0,0 +1,43 @@
+<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>
+       <name>Copy Dependencies IT</name>
+       <groupId>org.apache.maven.plugins.maven-dependency-plugin-it</groupId>
+       <artifactId>mdep-67-it</artifactId>
+       <version>1</version>
+       <packaging>jar</packaging>
+       <dependencies>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <version>4.1</version>
+               </dependency>
+       </dependencies>
+       <build>
+               <defaultGoal>generate-sources</defaultGoal>
+               <plugins>
+                       <plugin>
+                       <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-dependency-plugin</artifactId>
+                             <executions>
+                                   <execution>
+                                       <id>copy</id>
+                                       <phase>compile</phase>
+                                       <goals>
+                                           <goal>copy</goal>
+                                       </goals>
+                                       <configuration>
+                                           <artifactItems>
+                                               <artifactItem>
+                                                   <groupId>junit</groupId>
+                                                   
<artifactId>junit</artifactId>
+                                                   <type>java-source</type>
+                                                   
<outputDirectory>${project.build.directory}/junit-sources</outputDirectory>     
                               
+                                               </artifactItem>
+                                           </artifactItems>
+                                       </configuration>
+                                   </execution>
+                               </executions>
+                       </plugin>
+               </plugins>
+       </build>
+</project>

Modified: 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java?view=diff&rev=517085&r1=517084&r2=517085
==============================================================================
--- 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
 Sun Mar 11 19:52:32 2007
@@ -195,7 +195,8 @@
     {
         Artifact artifact;
 
-  //      Map managedVersions = createManagedVersionMap( factory, 
project.getId(), project.getDependencyManagement() );
+        // Map managedVersions = createManagedVersionMap( factory,
+        // project.getId(), project.getDependencyManagement() );
         VersionRange vr;
         try
         {
@@ -256,6 +257,8 @@
      * Tries to find missing version from dependancy list and dependency
      * management. If found, the artifact is updated with the correct version.
      * 
+     * It will first look for an exact match on 
artifactId/groupId/classifier/type and if it doesn't find
+     * a match, it will try again looking for artifactId and groupId only.
      * @param artifact
      *            representing configured file.
      * @throws MojoExecutionException
@@ -263,8 +266,12 @@
     private void fillMissingArtifactVersion( ArtifactItem artifact )
         throws MojoExecutionException
     {
-        if ( !findDependencyVersion( artifact, project.getDependencies() )
-            && !findDependencyVersion( artifact, 
project.getDependencyManagement().getDependencies() ) )
+        if ( !findDependencyVersion( artifact, project.getDependencies(), 
false )
+            && ( project.getDependencyManagement() == null || 
!findDependencyVersion( artifact, project
+                .getDependencyManagement().getDependencies(), false ) )
+            && !findDependencyVersion( artifact, project.getDependencies(), 
true )
+            && ( project.getDependencyManagement() == null || 
!findDependencyVersion( artifact, project
+                .getDependencyManagement().getDependencies(), true ) ) )
         {
             throw new MojoExecutionException( "Unable to find artifact version 
of " + artifact.getGroupId() + ":"
                 + artifact.getArtifactId() + " in either dependency list or in 
project's dependency management." );
@@ -279,18 +286,21 @@
      *            representing configured file.
      * @param list
      *            list of dependencies to search.
+     * @param looseMatch
+     *            only look at artifactId and groupId
      * @return the found dependency
      */
-    private boolean findDependencyVersion( ArtifactItem artifact, List list )
+    private boolean findDependencyVersion( ArtifactItem artifact, List list, 
boolean looseMatch )
     {
         boolean result = false;
+
         for ( int i = 0; i < list.size(); i++ )
         {
             Dependency dependency = (Dependency) list.get( i );
             if ( StringUtils.equals( dependency.getArtifactId(), 
artifact.getArtifactId() )
                 && StringUtils.equals( dependency.getGroupId(), 
artifact.getGroupId() )
-                && StringUtils.equals( dependency.getClassifier(), 
artifact.getClassifier() )
-                && StringUtils.equals( dependency.getType(), 
artifact.getType() ) )
+                && ( looseMatch || StringUtils.equals( 
dependency.getClassifier(), artifact.getClassifier() ) )
+                && ( looseMatch || StringUtils.equals( dependency.getType(), 
artifact.getType() ) ) )
             {
 
                 artifact.setVersion( dependency.getVersion() );
@@ -299,11 +309,12 @@
                 break;
             }
         }
+
         return result;
     }
 
     private Map createManagedVersionMap( ArtifactFactory artifactFactory, 
String projectId,
-                                        DependencyManagement 
dependencyManagement )
+                                         DependencyManagement 
dependencyManagement )
         throws MojoExecutionException
     {
         Map map;

Modified: 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java?view=diff&rev=517085&r1=517084&r2=517085
==============================================================================
--- 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/fromConfiguration/TestCopyMojo.java
 Sun Mar 11 19:52:32 2007
@@ -235,6 +235,7 @@
 
         return list;
     }
+   
 
     public void testMissingVersionFromDependencies()
         throws MojoExecutionException
@@ -258,6 +259,32 @@
         assertEquals( "2.0-SNAPSHOT", item.getVersion() );
     }
 
+    public void testMissingVersionFromDependenciesLooseMatch()
+    throws MojoExecutionException
+{
+    ArtifactItem item = new ArtifactItem();
+
+    item.setArtifactId( "artifactId" );
+    item.setClassifier( "" );
+    item.setGroupId( "groupId" );
+    item.setType( "type" );
+
+    MavenProject project = mojo.getProject();
+    project.setDependencies( getDependencyList( item ) );
+    
+    item.setClassifier( "sources" );
+    item.setType( "jar" );
+    
+    ArrayList list = new ArrayList();
+    list.add( item );
+    mojo.setArtifactItems( list );
+    
+    mojo.execute();
+    this.assertFileExists( item, true );
+    assertEquals( "2.1", item.getVersion() );
+}
+
+    
     public void testMissingVersionFromDependenciesWithClassifier()
         throws MojoExecutionException
     {
@@ -336,6 +363,40 @@
         assertEquals( "3.0-SNAPSHOT", item.getVersion() );
     }
 
+    public void testMissingVersionFromDependencyMgtLooseMatch()
+    throws MojoExecutionException
+{
+    ArtifactItem item = new ArtifactItem();
+
+    item.setArtifactId( "artifactId" );
+    item.setClassifier( "" );
+    item.setGroupId( "groupId" );
+    item.setType( "type" );
+
+    MavenProject project = mojo.getProject();
+    project.setDependencies( getDependencyList( item ) );
+
+    item = new ArtifactItem();
+
+    item.setArtifactId( "artifactId-2" );
+    item.setClassifier( "" );
+    item.setGroupId( "groupId" );
+    item.setType( "type" );
+
+    ArrayList list = new ArrayList();
+    list.add( item );
+
+    mojo.setArtifactItems( list );
+
+    project.getDependencyManagement().setDependencies( getDependencyMgtList( 
item ) );
+
+    item.setType( "jar" );
+    mojo.execute();
+
+    this.assertFileExists( item, true );
+    assertEquals( "3.1", item.getVersion() );
+}
+    
     public void testMissingVersionFromDependencyMgtWithClassifier()
         throws MojoExecutionException
     {


Reply via email to