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;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Properties;
import java.util.regex.Matcher;
/**
*/
private ArtifactPomRewriter rewriter;
+ /**
+ * @plexus.requirement
+ */
+ private ModelConverter translator;
+
/**
* @plexus.configuration default-value="false"
*/
{
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();
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(); )
{
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 );
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 );
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
{
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() );
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() );
+ }
+ }
+ }
}