From: Edwin L. Punzalan Date: Wed, 22 Feb 2006 07:49:59 +0000 (+0000) Subject: PR: MRM-13 X-Git-Tag: archiva-0.9-alpha-1~921 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bb16843c6766068ce939422b5a96b47bd00ac7ad;p=archiva.git PR: MRM-13 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 --- diff --git a/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java b/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java index fa7608a75..ee96ccd00 100644 --- a/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java +++ b/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java @@ -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 ); diff --git a/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java index 046b8ad44..55032e519 100644 --- a/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java +++ b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java @@ -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 index 000000000..29ef827e8 --- /dev/null +++ b/maven-repository-converter/src/test/source-repository/test/jars/relocated-v3artifact-1.0.0.jar @@ -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 index 000000000..10823fe89 --- /dev/null +++ b/maven-repository-converter/src/test/source-repository/test/poms/relocated-v3artifact-1.0.0.pom @@ -0,0 +1,27 @@ + + 3 + relocated-v3artifact + test + 1.0.0 + + + groupId + artifactId + version + + + groupId + test-artifactId + version + + test + + + + + scm:cvs:ext:${maven.username}@localhost:/home/cvs + + + relocated-test + + \ 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 index 000000000..29ef827e8 --- /dev/null +++ b/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.jar @@ -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 index 000000000..253ef3466 --- /dev/null +++ b/maven-repository-converter/src/test/target-repository/relocated-test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom @@ -0,0 +1,22 @@ + + 4.0.0 + relocated-test + relocated-v3artifact + 1.0.0 + + scm:cvs:ext:${maven.username}@localhost:/home/cvs + + + + groupId + artifactId + version + + + groupId + test-artifactId + version + test + + + \ 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 index 000000000..b20f62f9e --- /dev/null +++ b/maven-repository-converter/src/test/target-repository/test/relocated-v3artifact/1.0.0/relocated-v3artifact-1.0.0.pom @@ -0,0 +1,12 @@ + + test + relocated-v3artifact + 1.0.0 + + + relocated-test + relocated-v3artifact + 1.0.0 + + + \ No newline at end of file