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 e946355c2..e7b5a8891 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 @@ -95,6 +95,11 @@ public class DefaultRepositoryConverter */ private boolean dryrun; + /** + * @plexus.configuration default-value="true" + */ + private boolean mergeWithSourceMetadata; + /** * @plexus.requirement */ @@ -120,8 +125,8 @@ public void convert( Artifact artifact, ArtifactRepository targetRepository, Art Versioning versioning = new Versioning(); versioning.addVersion( artifact.getBaseVersion() ); metadata.setVersioning( versioning ); - updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, - transaction ); + updateMetadata( new ArtifactRepositoryMetadata( artifact ), artifact.getRepository(), + targetRepository, metadata, transaction ); metadata = createBaseMetadata( artifact ); metadata.setVersion( artifact.getBaseVersion() ); @@ -136,10 +141,9 @@ public void convert( Artifact artifact, ArtifactRepository targetRepository, Art versioning.setSnapshot( snapshot ); } - // TODO: merge latest/release/snapshot from source instead metadata.setVersioning( versioning ); - updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, - transaction ); + updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), artifact.getRepository(), + targetRepository, metadata, transaction ); if ( !dryrun ) { @@ -159,16 +163,16 @@ private static Metadata createBaseMetadata( Artifact artifact ) return metadata; } - private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository, - Metadata newMetadata, FileTransaction transaction ) + private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository sourceRepository, + ArtifactRepository targetRepository, Metadata newMetadata, FileTransaction transaction ) throws RepositoryConversionException { + Metadata metadata; + boolean changed = false; + + //merge with target repository metadata File file = new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - - Metadata metadata; - boolean changed; - if ( file.exists() ) { metadata = readMetadata( file ); @@ -180,6 +184,19 @@ private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactReposi metadata = newMetadata; } + //merge with source repository metadata + if ( mergeWithSourceMetadata ) + { + File srcfile = new File( sourceRepository.getBasedir(), + sourceRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); + + if ( srcfile.exists() ) + { + Metadata sourceMetadata = readMetadata( srcfile ); + changed = changed | metadata.merge( sourceMetadata ); + } + } + if ( changed ) { StringWriter writer = null; diff --git a/maven-repository-converter/src/test/expected-files/v4artifact-source-merging-metadata.xml b/maven-repository-converter/src/test/expected-files/v4artifact-source-merging-metadata.xml new file mode 100644 index 000000000..f6fb7b1d9 --- /dev/null +++ b/maven-repository-converter/src/test/expected-files/v4artifact-source-merging-metadata.xml @@ -0,0 +1,10 @@ + + test + correctArtifactMetadata + + + 1.0.0 + 2.0 + + + \ No newline at end of file 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 f33895eb5..54c42b96b 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 @@ -732,6 +732,39 @@ public void testInvalidSourceArtifactMetadata() assertFalse( "Check metadata not created", metadataFile.exists() ); } + public void testSourceArtifactMetadataMerging() + throws Exception + { + // test metadata in target repository is merged with the metadata in the source repository + + createModernSourceRepository(); + + Artifact artifact = createArtifact( "test", "correctArtifactMetadata", "1.0.0" ); + + repositoryConverter.convert( artifact, targetRepository, reporter ); + checkSuccess(); + + File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", artifactFile.exists() ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + + artifact = createPomArtifact( artifact ); + File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + assertTrue( "Check POM created", pomFile.exists() ); + + compareFiles( sourcePomFile, pomFile ); + + ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); + File artifactMetadataFile = new File( targetRepository.getBasedir(), + targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); + assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + + File expectedMetadataFile = getTestFile( "src/test/expected-files/v4artifact-source-merging-metadata.xml" ); + + compareFiles( expectedMetadataFile, artifactMetadataFile ); + } + public void testInvalidSourceSnapshotMetadata() throws Exception, MalformedURLException { diff --git a/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.jar b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.jar new file mode 100644 index 000000000..72af4bc10 Binary files /dev/null and b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.jar differ diff --git a/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.pom b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.pom new file mode 100644 index 000000000..a258834c1 --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/1.0.0/correctArtifactMetadata-1.0.0.pom @@ -0,0 +1,22 @@ + + + + 4.0.0 + correctArtifactMetadata + test + 1.0.0 + diff --git a/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/maven-metadata.xml b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/maven-metadata.xml new file mode 100644 index 000000000..bfef089f3 --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/correctArtifactMetadata/maven-metadata.xml @@ -0,0 +1,26 @@ + + + + test + correctArtifactMetadata + + + 1.0.0 + 2.0 + + + \ No newline at end of file