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-ffa450edef68tags/archiva-0.9-alpha-1
@@ -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 ); |
@@ -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() ); | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1 @@ | |||
v3 |
@@ -0,0 +1,27 @@ | |||
<project> | |||
<pomVersion>3</pomVersion> | |||
<artifactId>relocated-v3artifact</artifactId> | |||
<groupId>test</groupId> | |||
<currentVersion>1.0.0</currentVersion> | |||
<dependencies> | |||
<dependency> | |||
<groupId>groupId</groupId> | |||
<artifactId>artifactId</artifactId> | |||
<version>version</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>groupId</groupId> | |||
<artifactId>test-artifactId</artifactId> | |||
<version>version</version> | |||
<properties> | |||
<scope>test</scope> | |||
</properties> | |||
</dependency> | |||
</dependencies> | |||
<repository> | |||
<connection>scm:cvs:ext:${maven.username}@localhost:/home/cvs</connection> | |||
</repository> | |||
<properties> | |||
<relocated.groupId>relocated-test</relocated.groupId> | |||
</properties> | |||
</project> |
@@ -0,0 +1 @@ | |||
v3 |
@@ -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> |
@@ -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> |