Author: brianf
Date: Thu Sep 11 13:59:11 2008
New Revision: 694477

URL: http://svn.apache.org/viewvc?rev=694477&view=rev
Log:
MDEP-181 use installer to produce the repository layout. Patch from Igor 
Fedorenko 

Modified:
    
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/CopyDependenciesMojo.java
    
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestCopyDependenciesMojo2.java

Modified: 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/CopyDependenciesMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/CopyDependenciesMojo.java?rev=694477&r1=694476&r2=694477&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/CopyDependenciesMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/CopyDependenciesMojo.java
 Thu Sep 11 13:59:11 2008
@@ -20,10 +20,16 @@
  */
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.util.Iterator;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.installer.ArtifactInstallationException;
+import org.apache.maven.artifact.installer.ArtifactInstaller;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
@@ -47,6 +53,20 @@
 {
 
     /**
+     * @parameter 
expression="${component.org.apache.maven.artifact.installer.ArtifactInstaller}"
+     * @required
+     * @readonly
+     */
+    protected ArtifactInstaller installer;
+
+    /**
+     * @parameter 
expression="${component.org.apache.maven.artifact.repository.ArtifactRepositoryFactory}"
+     * @required
+     * @readonly
+     */
+    protected ArtifactRepositoryFactory repositoryFactory;
+
+    /**
      * Main entry into mojo. Gets the list of dependencies and iterates through
      * calling copyArtifact.
      * 
@@ -62,9 +82,30 @@
         DependencyStatusSets dss = getDependencySets( 
this.failOnMissingClassifierArtifact );
         Set artifacts = dss.getResolvedDependencies();
 
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            copyArtifact( (Artifact) i.next(), this.stripVersion );
+       if ( !useRepositoryLayout )
+       {
+               for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+               {
+                       copyArtifact( (Artifact) i.next(), this.stripVersion );
+               }
+       }
+       else
+       {
+                       try {
+                               ArtifactRepository targetRepository = 
repositoryFactory.createDeploymentArtifactRepository(
+                                               "local", 
+                                               
outputDirectory.toURL().toExternalForm(), 
+                                               new DefaultRepositoryLayout(),
+                                               false /*uniqueVersion*/ );
+                       for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+                       {
+                                       installArtifact( (Artifact) i.next(), 
targetRepository );
+                       }
+                       } 
+                       catch ( MalformedURLException e ) 
+                       {
+                               throw new MojoExecutionException("Could not 
create outputDirectory repository", e);
+                       }
         }
 
         artifacts = dss.getSkippedDependencies();
@@ -75,7 +116,48 @@
         }
     }
 
-    /**
+    private void installArtifact( Artifact artifact, ArtifactRepository 
targetRepository) 
+    {
+               try
+               {
+                       if ( "pom".equals( artifact.getType() ) ) 
+                       {
+                               installer.install( artifact.getFile(), 
artifact, targetRepository );
+                       }
+                       else
+                       {
+                   installer.install( artifact.getFile(), artifact, 
targetRepository );
+                   installBaseSnapshot( artifact, targetRepository );
+
+                   if ( isCopyPom() )
+                   {
+                           Artifact pomArtifact = getResolvedPomArtifact( 
artifact );
+                           if ( pomArtifact.getFile() != null && 
pomArtifact.getFile().exists() )
+                           {
+                               installer.install( pomArtifact.getFile(), 
pomArtifact, targetRepository );
+                                   installBaseSnapshot( pomArtifact, 
targetRepository );
+                           }
+                   }
+                       }
+               }
+               catch ( ArtifactInstallationException e ) 
+               {
+                   getLog().info( e.getMessage() );
+               }
+       }
+
+       private void installBaseSnapshot( Artifact artifact, ArtifactRepository 
targetRepository )
+                       throws ArtifactInstallationException 
+       {
+               if ( artifact.isSnapshot() && 
!artifact.getBaseVersion().equals( artifact.getVersion() ) )
+               {
+                       Artifact baseArtifact = this.factory.createArtifact( 
artifact.getGroupId(), artifact.getArtifactId(),
+                           artifact.getBaseVersion(), artifact.getScope(), 
artifact.getType() );
+                   installer.install( artifact.getFile(), baseArtifact, 
targetRepository );
+               }
+       }
+
+       /**
      * Copies the Artifact after building the destination file name if
      * overridden. This method also checks if the classifier is set and adds it
      * to the destination file name if needed.
@@ -109,17 +191,8 @@
         if ( isCopyPom() )
         {
             // Create the pom
-            Artifact pomArtifact = this.factory.createArtifact( 
artifact.getGroupId(), artifact.getArtifactId(),
-                                                                
artifact.getVersion(), "", "pom" );
-            // Resolve the pom artifact using repos
-            try
-            {
-                this.resolver.resolve( pomArtifact, this.remoteRepos, 
this.local );
-            }
-            catch ( Exception e )
-            {
-                getLog().info( e.getMessage() );
-            }
+            Artifact pomArtifact = getResolvedPomArtifact( artifact );
+            
             // Copy the pom
             if ( pomArtifact.getFile() != null && 
pomArtifact.getFile().exists() )
             {
@@ -129,6 +202,21 @@
         }
     }
 
+       protected Artifact getResolvedPomArtifact( Artifact artifact ) {
+               Artifact pomArtifact = this.factory.createArtifact( 
artifact.getGroupId(), artifact.getArtifactId(),
+                                                                   
artifact.getVersion(), "", "pom" );
+               // Resolve the pom artifact using repos
+               try
+               {
+                   this.resolver.resolve( pomArtifact, this.remoteRepos, 
this.local );
+               }
+               catch ( Exception e )
+               {
+                   getLog().info( e.getMessage() );
+               }
+               return pomArtifact;
+       }
+
     protected ArtifactsFilter getMarkedArtifactFilter()
     {
         return new DestFileFilter( this.overWriteReleases, 
this.overWriteSnapshots, this.overWriteIfNewer,

Modified: 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestCopyDependenciesMojo2.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestCopyDependenciesMojo2.java?rev=694477&r1=694476&r2=694477&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestCopyDependenciesMojo2.java
 (original)
+++ 
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestCopyDependenciesMojo2.java
 Thu Sep 11 13:59:11 2008
@@ -25,11 +25,19 @@
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Snapshot;
+import 
org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
+import org.apache.maven.artifact.transform.SnapshotTransformation;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
 import 
org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.StringUtils;
 
 public class TestCopyDependenciesMojo2
     extends AbstractDependencyMojoTestCase
@@ -217,21 +225,65 @@
     public void testRepositoryLayout()
         throws Exception
     {
+       String baseVersion = "2.0-SNAPSHOT";
+               Artifact expandedSnapshot = this.stubFactory.createArtifact( 
"testGroupId", "expanded-snapshot", baseVersion );
+
+       SnapshotTransformation tr = new SnapshotTransformation();
+        Snapshot snapshot = new Snapshot();
+        snapshot.setTimestamp( tr.getDeploymentTimestamp() );
+        snapshot.setBuildNumber( 1 );
+        RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( 
expandedSnapshot, snapshot );
+        String newVersion = snapshot.getTimestamp() + "-" + 
snapshot.getBuildNumber();
+        expandedSnapshot.setResolvedVersion( StringUtils.replace( baseVersion, 
Artifact.SNAPSHOT_VERSION, newVersion ) );
+        expandedSnapshot.addMetadata( metadata );
+
+       mojo.project.getArtifacts().add(expandedSnapshot);
+       mojo.project.getDependencyArtifacts().add(expandedSnapshot);
+
         mojo.useRepositoryLayout = true;
         mojo.execute();
+        
+        File outputDirectory = mojo.outputDirectory;
+               ArtifactRepository targetRepository = 
mojo.repositoryFactory.createDeploymentArtifactRepository( 
+                       "local", 
+                       outputDirectory.toURL().toExternalForm(), 
+                new DefaultRepositoryLayout(),
+                false );
 
         Iterator iter = mojo.project.getArtifacts().iterator();
         while ( iter.hasNext() )
         {
             Artifact artifact = (Artifact) iter.next();
-            String fileName = DependencyUtil.getFormattedFileName( artifact, 
false );
-            File folder = DependencyUtil.getFormattedOutputDirectory( false, 
true, mojo.useRepositoryLayout, false,
-                                                                      
mojo.outputDirectory, artifact );
-            File file = new File( folder, fileName );
-            assertTrue( file.exists() );
+                       assertArtifactExists( artifact, targetRepository );
+            
+            if ( ! artifact.getBaseVersion().equals( artifact.getVersion() ) )
+            {
+               Artifact baseArtifact = mojo.factory.createArtifact( 
artifact.getGroupId(), 
+                                                                               
                     artifact.getArtifactId(),
+                                                                               
                     artifact.getBaseVersion(),
+                                                                               
                     artifact.getScope(),
+                                                                               
                     artifact.getType() );
+                       assertArtifactExists( baseArtifact, targetRepository );
+            }
+
         }
     }
 
+       private void assertArtifactExists( Artifact artifact, 
ArtifactRepository targetRepository ) {
+               File file = new File( targetRepository.getBasedir(), 
+                                                         
targetRepository.getLayout().pathOf( artifact ) );
+               assertTrue( file.exists() );
+
+               Iterator metaIter = artifact.getMetadataList().iterator();
+        while ( metaIter.hasNext() )
+        {
+               ArtifactMetadata meta = (ArtifactMetadata) metaIter.next();
+                       File metaFile = new File( 
targetRepository.getBasedir(), 
+                                                                         
targetRepository.getLayout().pathOfLocalRepositoryMetadata( meta, 
targetRepository) );
+                       assertTrue( metaFile.exists() );
+        }
+       }
+
     public void testSubPerArtifactRemoveVersion()
         throws Exception
     {


Reply via email to