1 package org.apache.maven.repository.converter;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
23 import org.apache.maven.artifact.repository.metadata.Metadata;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Snapshot;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
30 import org.apache.maven.model.DistributionManagement;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Relocation;
33 import org.apache.maven.model.converter.ArtifactPomRewriter;
34 import org.apache.maven.model.converter.ModelConverter;
35 import org.apache.maven.model.converter.PomTranslationException;
36 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
37 import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
38 import org.apache.maven.repository.converter.transaction.FileTransaction;
39 import org.apache.maven.repository.digest.Digester;
40 import org.apache.maven.repository.reporting.ArtifactReporter;
41 import org.codehaus.plexus.i18n.I18N;
42 import org.codehaus.plexus.util.FileUtils;
43 import org.codehaus.plexus.util.IOUtil;
44 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
47 import java.io.FileNotFoundException;
48 import java.io.FileReader;
49 import java.io.IOException;
50 import java.io.StringReader;
51 import java.io.StringWriter;
52 import java.security.NoSuchAlgorithmException;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Properties;
57 import java.util.regex.Matcher;
60 * Implementation of repository conversion class.
62 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
63 * @plexus.component role="org.apache.maven.repository.converter.RepositoryConverter" role-hint="default"
65 public class DefaultRepositoryConverter
66 implements RepositoryConverter
71 private Digester digester;
76 private ArtifactFactory artifactFactory;
81 private ArtifactPomRewriter rewriter;
86 private ModelConverter translator;
89 * @plexus.configuration default-value="false"
91 private boolean force;
94 * @plexus.configuration default-value="false"
96 private boolean dryrun;
99 * @plexus.configuration default-value="true"
101 private boolean mergeWithSourceMetadata;
104 * @plexus.requirement
108 public void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
109 throws RepositoryConversionException
111 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
113 throw new RepositoryConversionException( getI18NString( "exception.repositories.match" ) );
116 if ( validateMetadata( artifact, reporter ) )
118 FileTransaction transaction = new FileTransaction();
120 if ( copyPom( artifact, targetRepository, reporter, transaction ) )
122 if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
124 Metadata metadata = createBaseMetadata( artifact );
125 Versioning versioning = new Versioning();
126 versioning.addVersion( artifact.getBaseVersion() );
127 metadata.setVersioning( versioning );
128 updateMetadata( new ArtifactRepositoryMetadata( artifact ), artifact.getRepository(),
129 targetRepository, metadata, transaction );
131 metadata = createBaseMetadata( artifact );
132 metadata.setVersion( artifact.getBaseVersion() );
133 versioning = new Versioning();
135 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
136 if ( matcher.matches() )
138 Snapshot snapshot = new Snapshot();
139 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
140 snapshot.setTimestamp( matcher.group( 2 ) );
141 versioning.setSnapshot( snapshot );
144 metadata.setVersioning( versioning );
145 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), artifact.getRepository(),
146 targetRepository, metadata, transaction );
150 transaction.commit();
152 reporter.addSuccess( artifact );
158 private static Metadata createBaseMetadata( Artifact artifact )
160 Metadata metadata = new Metadata();
161 metadata.setArtifactId( artifact.getArtifactId() );
162 metadata.setGroupId( artifact.getGroupId() );
166 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository sourceRepository,
167 ArtifactRepository targetRepository, Metadata newMetadata, FileTransaction transaction )
168 throws RepositoryConversionException
171 boolean changed = false;
173 //merge with target repository metadata
174 File file = new File( targetRepository.getBasedir(),
175 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
178 metadata = readMetadata( file );
179 changed = metadata.merge( newMetadata );
184 metadata = newMetadata;
187 //merge with source repository metadata
188 if ( mergeWithSourceMetadata )
190 File srcfile = new File( sourceRepository.getBasedir(),
191 sourceRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
193 if ( srcfile.exists() )
195 Metadata sourceMetadata = readMetadata( srcfile );
196 changed = changed | metadata.merge( sourceMetadata );
202 StringWriter writer = null;
205 writer = new StringWriter();
207 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
209 mappingWriter.write( writer, metadata );
211 transaction.createFile( writer.toString(), file );
213 catch ( IOException e )
215 throw new RepositoryConversionException( "Error writing target metadata", e );
219 IOUtil.close( writer );
224 private Metadata readMetadata( File file )
225 throws RepositoryConversionException
228 MetadataXpp3Reader reader = new MetadataXpp3Reader();
229 FileReader fileReader = null;
232 fileReader = new FileReader( file );
233 metadata = reader.read( fileReader );
235 catch ( FileNotFoundException e )
237 throw new RepositoryConversionException( "Error reading target metadata", e );
239 catch ( IOException e )
241 throw new RepositoryConversionException( "Error reading target metadata", e );
243 catch ( XmlPullParserException e )
245 throw new RepositoryConversionException( "Error reading target metadata", e );
249 IOUtil.close( fileReader );
254 private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter )
255 throws RepositoryConversionException
257 ArtifactRepository repository = artifact.getRepository();
259 boolean result = true;
261 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
263 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
266 Metadata metadata = readMetadata( file );
267 result = validateMetadata( metadata, repositoryMetadata, artifact, reporter );
270 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
271 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
274 Metadata metadata = readMetadata( file );
275 result = result && validateMetadata( metadata, repositoryMetadata, artifact, reporter );
281 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact,
282 ArtifactReporter reporter )
285 String artifactIdKey = null;
286 String snapshotKey = null;
287 String versionKey = null;
288 String versionsKey = null;
289 if ( repositoryMetadata.storedInGroupDirectory() )
291 groupIdKey = "failure.incorrect.groupMetadata.groupId";
293 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
295 groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
296 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
297 versionKey = "failure.incorrect.snapshotMetadata.version";
298 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
302 groupIdKey = "failure.incorrect.artifactMetadata.groupId";
303 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
304 versionsKey = "failure.incorrect.artifactMetadata.versions";
307 boolean result = true;
309 if ( !metadata.getGroupId().equals( artifact.getGroupId() ) )
311 reporter.addFailure( artifact, getI18NString( groupIdKey ) );
314 if ( !repositoryMetadata.storedInGroupDirectory() )
316 if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
318 reporter.addFailure( artifact, getI18NString( artifactIdKey ) );
321 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
325 boolean foundVersion = false;
326 if ( metadata.getVersioning() != null )
328 for ( Iterator i = metadata.getVersioning().getVersions().iterator();
329 i.hasNext() && !foundVersion; )
331 String version = (String) i.next();
332 if ( version.equals( artifact.getBaseVersion() ) )
341 reporter.addFailure( artifact, getI18NString( versionsKey ) );
348 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
350 reporter.addFailure( artifact, getI18NString( versionKey ) );
354 if ( artifact.isSnapshot() )
356 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
357 if ( matcher.matches() )
359 boolean correct = false;
360 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
362 Snapshot snapshot = metadata.getVersioning().getSnapshot();
363 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
364 String ts = matcher.group( 2 );
365 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
373 reporter.addFailure( artifact, getI18NString( snapshotKey ) );
383 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
384 FileTransaction transaction )
385 throws RepositoryConversionException
387 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
388 artifact.getVersion() );
389 pom.setBaseVersion( artifact.getBaseVersion() );
390 ArtifactRepository repository = artifact.getRepository();
391 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
393 boolean result = true;
396 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
398 String contents = null;
399 boolean checksumsValid = false;
402 if ( testChecksums( artifact, file, reporter ) )
404 checksumsValid = true;
405 contents = FileUtils.fileRead( file );
408 catch ( IOException e )
410 throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
413 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
418 boolean matching = false;
419 if ( !force && targetFile.exists() )
421 String targetContents = FileUtils.fileRead( targetFile );
422 matching = targetContents.equals( contents );
424 if ( force || !matching )
426 transaction.createFile( contents, targetFile );
429 catch ( IOException e )
431 throw new RepositoryConversionException( "Unable to write target POM: " + e.getMessage(), e );
437 StringReader stringReader = new StringReader( contents );
438 StringWriter writer = null;
441 MavenXpp3Reader v3Reader = new MavenXpp3Reader();
442 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
444 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
446 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
447 artifact.getArtifactId(),
448 artifact.getVersion() );
449 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
452 Model v4Model = translator.translate( v3Model );
454 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
455 v3Model.getVersion(), v3Model.getPackage() );
457 writer = new StringWriter();
458 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
459 Xpp3Writer.write( writer, v4Model );
461 transaction.createFile( writer.toString(), targetFile );
463 List warnings = translator.getWarnings();
465 for ( Iterator i = warnings.iterator(); i.hasNext(); )
467 String message = (String) i.next();
468 reporter.addWarning( artifact, message );
471 catch ( XmlPullParserException e )
473 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
476 catch ( IOException e )
478 throw new RepositoryConversionException( "Unable to write converted POM", e );
480 catch ( PomTranslationException e )
482 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
487 IOUtil.close( writer );
493 reporter.addWarning( artifact, getI18NString( "warning.missing.pom" ) );
498 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
499 ArtifactRepository repository, FileTransaction transaction )
502 Properties properties = v3Model.getProperties();
503 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
504 properties.containsKey( "relocated.version" ) )
506 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
507 properties.remove( "relocated.groupId" );
509 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
510 properties.remove( "relocated.artifactId" );
512 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
513 properties.remove( "relocated.version" );
515 String message = properties.getProperty( "relocated.message", "" );
516 properties.remove( "relocated.message" );
518 if ( properties.isEmpty() )
520 v3Model.setProperties( null );
523 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
524 newArtifactId, newVersion, message, repository, transaction );
526 v3Model.setGroupId( newGroupId );
527 v3Model.setArtifactId( newArtifactId );
528 v3Model.setVersion( newVersion );
530 artifact.setGroupId( newGroupId );
531 artifact.setArtifactId( newArtifactId );
532 artifact.setVersion( newVersion );
542 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
543 String newArtifactId, String newVersion, String message,
544 ArtifactRepository repository, FileTransaction transaction )
547 Model pom = new Model();
548 pom.setGroupId( groupId );
549 pom.setArtifactId( artifactId );
550 pom.setVersion( version );
552 DistributionManagement dMngt = new DistributionManagement();
554 Relocation relocation = new Relocation();
555 relocation.setGroupId( newGroupId );
556 relocation.setArtifactId( newArtifactId );
557 relocation.setVersion( newVersion );
558 if ( message != null && message.length() > 0 )
560 relocation.setMessage( message );
563 dMngt.setRelocation( relocation );
565 pom.setDistributionManagement( dMngt );
567 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
568 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
570 StringWriter strWriter = new StringWriter();
571 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
572 pomWriter.write( strWriter, pom );
574 transaction.createFile( strWriter.toString(), pomFile );
577 private String getI18NString( String key, String arg0 )
579 return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
582 private String getI18NString( String key )
584 return i18n.getString( getClass().getName(), Locale.getDefault(), key );
587 private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
588 throws IOException, RepositoryConversionException
594 result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
595 "failure.incorrect.md5" );
596 result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
597 "failure.incorrect.sha1" );
599 catch ( NoSuchAlgorithmException e )
601 throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
606 private boolean verifyChecksum( File file, String fileName, String algorithm, ArtifactReporter reporter,
607 Artifact artifact, String key )
608 throws IOException, NoSuchAlgorithmException
610 boolean result = true;
612 File md5 = new File( file.getParentFile(), fileName );
615 String checksum = FileUtils.fileRead( md5 );
616 if ( !digester.verifyChecksum( file, checksum, algorithm ) )
618 reporter.addFailure( artifact, getI18NString( key ) );
625 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
626 FileTransaction transaction )
627 throws RepositoryConversionException
629 File sourceFile = artifact.getFile();
631 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
633 boolean result = true;
636 boolean matching = false;
637 if ( !force && targetFile.exists() )
639 matching = FileUtils.contentEquals( sourceFile, targetFile );
642 reporter.addFailure( artifact, getI18NString( "failure.target.already.exists" ) );
648 if ( force || !matching )
650 if ( testChecksums( artifact, sourceFile, reporter ) )
652 transaction.copyFile( sourceFile, targetFile );
661 catch ( IOException e )
663 throw new RepositoryConversionException( "Error copying artifact", e );
668 public void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
669 throws RepositoryConversionException
671 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
673 Artifact artifact = (Artifact) i.next();
674 convert( artifact, targetRepository, reporter );