]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-13
authorEdwin L. Punzalan <epunzalan@apache.org>
Wed, 22 Feb 2006 07:49:59 +0000 (07:49 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Wed, 22 Feb 2006 07:49:59 +0000 (07:49 +0000)
Handled relocation information in v3 poms.  Includes creation of relocation pom and relocated the pom and jar to its new location.

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@379722 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java
maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java
maven-repository-converter/src/test/source-repository/test/jars/relocated-v3artifact-1.0.0.jar [new file with mode: 0644]
maven-repository-converter/src/test/source-repository/test/poms/relocated-v3artifact-1.0.0.pom [new file with mode: 0644]
maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.jar [new file with mode: 0644]
maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom [new file with mode: 0644]
maven-repository-converter/src/test/target-repository/test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom [new file with mode: 0644]

index fa7608a75761310ea8fcbe530366ccb94515599d..ee96ccd0068135e9fedfda6b9fc229b5a047a210 100644 (file)
@@ -27,7 +27,13 @@ import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryM
 import org.apache.maven.artifact.repository.metadata.Versioning;
 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
+import org.apache.maven.model.DistributionManagement;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Relocation;
 import org.apache.maven.model.converter.ArtifactPomRewriter;
+import org.apache.maven.model.converter.ModelConverter;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.repository.converter.transaction.FileTransaction;
 import org.apache.maven.repository.digest.Digester;
 import org.apache.maven.repository.reporting.ArtifactReporter;
@@ -45,6 +51,7 @@ import java.security.NoSuchAlgorithmException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Properties;
 import java.util.regex.Matcher;
 
 /**
@@ -71,6 +78,11 @@ public class DefaultRepositoryConverter
      */
     private ArtifactPomRewriter rewriter;
 
+    /**
+     * @plexus.requirement
+     */
+    private ModelConverter translator;
+
     /**
      * @plexus.configuration default-value="false"
      */
@@ -98,9 +110,9 @@ public class DefaultRepositoryConverter
         {
             FileTransaction transaction = new FileTransaction();
 
-            if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
+            if ( copyPom( artifact, targetRepository, reporter, transaction ) )
             {
-                if ( copyPom( artifact, targetRepository, reporter, transaction ) )
+                if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
                 {
                     Metadata metadata = createBaseMetadata( artifact );
                     Versioning versioning = new Versioning();
@@ -396,15 +408,29 @@ public class DefaultRepositoryConverter
                 StringWriter writer = null;
                 try
                 {
-                    writer = new StringWriter();
+                    MavenXpp3Reader v3Reader = new MavenXpp3Reader();
+                    org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
+
+                    if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
+                    {
+                        Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
+                                                                                       artifact.getArtifactId(),
+                                                                                       artifact.getVersion() );
+                        targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
+                    }
+
+                    Model v4Model = translator.translate( v3Model );
 
-                    // TODO: this api could be improved - is it worth having or go back to modelConverter?
-                    rewriter.rewrite( stringReader, writer, false, artifact.getGroupId(), artifact.getArtifactId(),
-                                      artifact.getVersion(), artifact.getType() );
+                    translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
+                                                 v3Model.getVersion(), v3Model.getPackage() );
+
+                    writer = new StringWriter();
+                    MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
+                    Xpp3Writer.write( writer, v4Model );
 
                     transaction.createFile( writer.toString(), targetFile );
 
-                    List warnings = rewriter.getWarnings();
+                    List warnings = translator.getWarnings();
 
                     for ( Iterator i = warnings.iterator(); i.hasNext(); )
                     {
@@ -434,6 +460,101 @@ public class DefaultRepositoryConverter
         return result;
     }
 
+    private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
+                                  ArtifactRepository repository, FileTransaction transaction )
+        throws IOException
+    {
+        Properties properties = v3Model.getProperties();
+        if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
+            properties.containsKey( "relocated.version" ) )
+        {
+            String newGroupId = v3Model.getGroupId();
+            if ( properties.containsKey( "relocated.groupId" ) )
+            {
+                newGroupId = properties.getProperty( "relocated.groupId" );
+                properties.remove( "relocated.groupId" );
+            }
+
+            String newArtifactId = v3Model.getArtifactId();
+            if ( properties.containsKey( "relocated.artifactId" ) )
+            {
+                newArtifactId = properties.getProperty( "relocated.artifactId" );
+                properties.remove( "relocated.artifactId" );
+            }
+
+            String newVersion = v3Model.getVersion();
+            if ( properties.containsKey( "relocated.version" ) )
+            {
+                newVersion = properties.getProperty( "relocated.version" );
+                properties.remove( "relocated.version" );
+            }
+
+            String message = "";
+            if ( properties.containsKey( "relocated.message" ) )
+            {
+                message = properties.getProperty( "relocated.message" );
+                properties.remove( "relocated.message" );
+            }
+
+            if ( properties.isEmpty() )
+            {
+                v3Model.setProperties( null );
+            }
+
+            writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
+                                newArtifactId, newVersion, message, repository, transaction );
+
+            v3Model.setGroupId( newGroupId );
+            v3Model.setArtifactId( newArtifactId );
+            v3Model.setVersion( newVersion );
+
+            artifact.setGroupId( newGroupId );
+            artifact.setArtifactId( newArtifactId );
+            artifact.setVersion( newVersion );
+
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
+                                     String newArtifactId, String newVersion, String message,
+                                     ArtifactRepository repository, FileTransaction transaction )
+        throws IOException
+    {
+        Model pom = new Model();
+        pom.setGroupId( groupId );
+        pom.setArtifactId( artifactId );
+        pom.setVersion( version );
+
+        DistributionManagement dMngt = new DistributionManagement();
+
+        Relocation relocation = new Relocation();
+        relocation.setGroupId( newGroupId );
+        relocation.setArtifactId( newArtifactId );
+        relocation.setVersion( newVersion );
+        if ( message != null && message.length() > 0 )
+        {
+            relocation.setMessage( message );
+        }
+
+        dMngt.setRelocation( relocation );
+
+        pom.setDistributionManagement( dMngt );
+
+        Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
+        File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
+
+        StringWriter strWriter = new StringWriter();
+        MavenXpp3Writer pomWriter = new MavenXpp3Writer();
+        pomWriter.write( strWriter, pom );
+
+        transaction.createFile( strWriter.toString(), pomFile );
+    }
+
     private String getI18NString( String key, String arg0 )
     {
         return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
index 046b8ad4472a2dbc7307db5b8fe631c251b38b9b..55032e5196b9f4d75a205fc55a1570f65f6bd09a 100644 (file)
@@ -82,7 +82,7 @@ public class RepositoryConverterTest
         layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
 
         File targetBase = getTestFile( "target/test-target-repository" );
-        FileUtils.copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
+        copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
 
         targetRepository =
             factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null );
@@ -184,6 +184,39 @@ public class RepositoryConverterTest
         compareFiles( expectedMetadataFile, versionMetadataFile );
     }
 
+    public void testV3PomConvertWithRelocation()
+        throws RepositoryConversionException, IOException
+    {
+        Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
+        ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
+        File artifactMetadataFile = new File( targetRepository.getBasedir(),
+                                              targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
+        artifactMetadataFile.delete();
+
+        ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
+        File versionMetadataFile = new File( targetRepository.getBasedir(),
+                                             targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
+        versionMetadataFile.delete();
+
+        repositoryConverter.convert( artifact, targetRepository, reporter );
+        //checkSuccess();  --> commented until MNG-2100 is fixed
+
+        File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
+        assertTrue( "Check if relocated artifact created", artifactFile.exists() );
+        assertTrue( "Check if relocated artifact matches",
+                    FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
+        Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
+        File pomFile = getTestFile( "src/test/target-repository/" + targetRepository.pathOf( pomArtifact ) );
+        File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
+        assertTrue( "Check if expected relocated pom matches", FileUtils.contentEquals( pomFile, testFile ) );
+
+        Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
+        artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
+        assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
+        testFile = getTestFile( "src/test/target-repository/" + targetRepository.pathOf( orig ) );
+        assertTrue( "Check if expected artifact matches", FileUtils.contentEquals( artifactFile, testFile ) );
+    }
+
     public void testV3PomWarningsOnConvert()
         throws RepositoryConversionException, IOException
     {
@@ -661,7 +694,7 @@ public class RepositoryConverterTest
 
         repositoryConverter.convert( artifact, targetRepository, reporter );
         checkFailure();
-        String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replace( "{0}", "(.*?)" ) + "$";
+        String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
         assertTrue( "Check failure message", getFailure().getReason().matches( pattern ) );
 
         assertFalse( "check artifact rolled back", artifactFile.exists() );
@@ -891,4 +924,51 @@ public class RepositoryConverterTest
             factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
     }
 
+    private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
+        throws IOException
+    {
+        if ( !sourceDirectory.exists() )
+        {
+            throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
+        }
+
+        File[] files = sourceDirectory.listFiles();
+
+        String sourcePath = sourceDirectory.getAbsolutePath();
+
+        for ( int i = 0; i < files.length; i++ )
+        {
+            File file = files[i];
+
+            String dest = file.getAbsolutePath();
+
+            dest = dest.substring( sourcePath.length() + 1 );
+
+            File destination = new File( destinationDirectory, dest );
+
+            if ( file.isFile() )
+            {
+                destination = destination.getParentFile();
+
+                FileUtils.copyFileToDirectory( file, destination );
+            }
+            else if ( file.isDirectory() )
+            {
+                if ( !file.getName().equals( ".svn" ) )
+                {
+                    if ( !destination.exists() && !destination.mkdirs() )
+                    {
+                        throw new IOException(
+                            "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
+                    }
+
+                    copyDirectoryStructure( file, destination );
+                }
+            }
+            else
+            {
+                throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
+            }
+        }
+    }
 }
diff --git a/maven-repository-converter/src/test/source-repository/test/jars/relocated-v3artifact-1.0.0.jar b/maven-repository-converter/src/test/source-repository/test/jars/relocated-v3artifact-1.0.0.jar
new file mode 100644 (file)
index 0000000..29ef827
--- /dev/null
@@ -0,0 +1 @@
+v3
diff --git a/maven-repository-converter/src/test/source-repository/test/poms/relocated-v3artifact-1.0.0.pom b/maven-repository-converter/src/test/source-repository/test/poms/relocated-v3artifact-1.0.0.pom
new file mode 100644 (file)
index 0000000..10823fe
--- /dev/null
@@ -0,0 +1,27 @@
+<project>\r
+  <pomVersion>3</pomVersion>\r
+  <artifactId>relocated-v3artifact</artifactId>\r
+  <groupId>test</groupId>\r
+  <currentVersion>1.0.0</currentVersion>\r
+  <dependencies>\r
+    <dependency>\r
+      <groupId>groupId</groupId>\r
+      <artifactId>artifactId</artifactId>\r
+      <version>version</version>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>groupId</groupId>\r
+      <artifactId>test-artifactId</artifactId>\r
+      <version>version</version>\r
+      <properties>\r
+        <scope>test</scope>\r
+      </properties>\r
+    </dependency>\r
+  </dependencies>\r
+  <repository>\r
+    <connection>scm:cvs:ext:${maven.username}@localhost:/home/cvs</connection>\r
+  </repository>\r
+  <properties>\r
+    <relocated.groupId>relocated-test</relocated.groupId>\r
+  </properties>\r
+</project>
\ No newline at end of file
diff --git a/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.jar b/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.jar
new file mode 100644 (file)
index 0000000..29ef827
--- /dev/null
@@ -0,0 +1 @@
+v3
diff --git a/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom b/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom
new file mode 100644 (file)
index 0000000..253ef34
--- /dev/null
@@ -0,0 +1,22 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>relocated-test</groupId>
+  <artifactId>relocated-v3artifact</artifactId>
+  <version>1.0.0</version>
+  <scm>
+    <connection>scm:cvs:ext:${maven.username}@localhost:/home/cvs</connection>
+  </scm>
+  <dependencies>
+    <dependency>
+      <groupId>groupId</groupId>
+      <artifactId>artifactId</artifactId>
+      <version>version</version>
+    </dependency>
+    <dependency>
+      <groupId>groupId</groupId>
+      <artifactId>test-artifactId</artifactId>
+      <version>version</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
\ No newline at end of file
diff --git a/maven-repository-converter/src/test/target-repository/test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom b/maven-repository-converter/src/test/target-repository/test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom
new file mode 100644 (file)
index 0000000..b20f62f
--- /dev/null
@@ -0,0 +1,12 @@
+<project>
+  <groupId>test</groupId>
+  <artifactId>relocated-v3artifact</artifactId>
+  <version>1.0.0</version>
+  <distributionManagement>
+    <relocation>
+      <groupId>relocated-test</groupId>
+      <artifactId>relocated-v3artifact</artifactId>
+      <version>1.0.0</version>
+    </relocation>
+  </distributionManagement>
+</project>
\ No newline at end of file