1 package org.apache.maven.archiva.converter.artifact;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.maven.archiva.transaction.FileTransaction;
25 import org.apache.maven.archiva.transaction.TransactionException;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
31 import org.apache.maven.artifact.repository.metadata.Metadata;
32 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.Snapshot;
34 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35 import org.apache.maven.artifact.repository.metadata.Versioning;
36 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
37 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
38 import org.apache.maven.model.DistributionManagement;
39 import org.apache.maven.model.Model;
40 import org.apache.maven.model.Relocation;
41 import org.apache.maven.model.converter.ModelConverter;
42 import org.apache.maven.model.converter.PomTranslationException;
43 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
44 import org.codehaus.plexus.digest.Digester;
45 import org.codehaus.plexus.digest.DigesterException;
46 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
49 import java.io.FileNotFoundException;
50 import java.io.FileReader;
51 import java.io.IOException;
52 import java.io.StringReader;
53 import java.io.StringWriter;
54 import java.util.ArrayList;
55 import java.util.HashMap;
56 import java.util.Iterator;
57 import java.util.List;
59 import java.util.Properties;
60 import java.util.regex.Matcher;
63 * LegacyToDefaultConverter
67 * @plexus.component role="org.apache.maven.archiva.converter.artifact.ArtifactConverter"
68 * role-hint="legacy-to-default"
70 public class LegacyToDefaultConverter
71 implements ArtifactConverter
74 * {@link List}<{@link Digester}>
76 * @plexus.requirement role="org.codehaus.plexus.digest.Digester"
78 private List digesters;
83 private ModelConverter translator;
88 private ArtifactFactory artifactFactory;
93 private ArtifactHandlerManager artifactHandlerManager;
96 * @plexus.configuration default-value="false"
98 private boolean force;
101 * @plexus.configuration default-value="false"
103 private boolean dryrun;
105 private Map warnings = new HashMap();
107 public void convert( Artifact artifact, ArtifactRepository targetRepository )
108 throws ArtifactConversionException
110 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
112 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
115 if ( !validateMetadata( artifact ) )
117 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
121 FileTransaction transaction = new FileTransaction();
123 if ( !copyPom( artifact, targetRepository, transaction ) )
125 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
129 if ( !copyArtifact( artifact, targetRepository, transaction ) )
131 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
135 Metadata metadata = createBaseMetadata( artifact );
136 Versioning versioning = new Versioning();
137 versioning.addVersion( artifact.getBaseVersion() );
138 metadata.setVersioning( versioning );
139 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
141 metadata = createBaseMetadata( artifact );
142 metadata.setVersion( artifact.getBaseVersion() );
143 versioning = new Versioning();
145 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
146 if ( matcher.matches() )
148 Snapshot snapshot = new Snapshot();
149 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
150 snapshot.setTimestamp( matcher.group( 2 ) );
151 versioning.setSnapshot( snapshot );
154 // TODO: merge latest/release/snapshot from source instead
155 metadata.setVersioning( versioning );
156 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
162 transaction.commit();
164 catch ( TransactionException e )
166 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ), e ); //$NON-NLS-1$
171 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
172 throws ArtifactConversionException
174 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact
176 pom.setBaseVersion( artifact.getBaseVersion() );
177 ArtifactRepository repository = artifact.getRepository();
178 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
180 boolean result = true;
183 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
185 String contents = null;
186 boolean checksumsValid = false;
189 if ( testChecksums( artifact, file ) )
191 checksumsValid = true;
194 // Even if the checksums for the POM are invalid we should still convert the POM
195 contents = FileUtils.readFileToString( file, null );
197 catch ( IOException e )
199 throw new ArtifactConversionException(
200 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
203 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
208 boolean matching = false;
209 if ( !force && targetFile.exists() )
211 String targetContents = FileUtils.readFileToString( targetFile, null );
212 matching = targetContents.equals( contents );
214 if ( force || !matching )
216 transaction.createFile( contents, targetFile, digesters );
219 catch ( IOException e )
221 throw new ArtifactConversionException( Messages
222 .getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
228 StringReader stringReader = new StringReader( contents );
229 StringWriter writer = null;
232 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
233 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
235 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
237 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact
238 .getArtifactId(), artifact.getVersion() );
239 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
242 Model v4Model = translator.translate( v3Model );
244 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model
245 .getVersion(), v3Model.getPackage() );
247 writer = new StringWriter();
248 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
249 Xpp3Writer.write( writer, v4Model );
251 transaction.createFile( writer.toString(), targetFile, digesters );
253 List warnings = translator.getWarnings();
255 for ( Iterator i = warnings.iterator(); i.hasNext(); )
257 String message = (String) i.next();
258 addWarning( artifact, message );
261 catch ( XmlPullParserException e )
263 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
266 catch ( IOException e )
268 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), e ); //$NON-NLS-1$
270 catch ( PomTranslationException e )
272 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
277 IOUtils.closeQuietly( writer );
283 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
288 private boolean testChecksums( Artifact artifact, File file )
291 boolean result = true;
292 Iterator it = digesters.iterator();
293 while ( it.hasNext() )
295 Digester digester = (Digester) it.next();
296 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$
297 artifact, "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
302 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
305 boolean result = true;
307 File checksumFile = new File( file.getParentFile(), fileName );
308 if ( checksumFile.exists() )
310 String checksum = FileUtils.readFileToString( checksumFile, null );
313 digester.verify( file, checksum );
315 catch ( DigesterException e )
317 addWarning( artifact, Messages.getString( key ) );
325 * File extension for checksums
326 * TODO should be moved to plexus-digester ?
328 private String getDigesterFileExtension( Digester digester )
330 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
333 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
334 throws ArtifactConversionException
336 File sourceFile = artifact.getFile();
338 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
340 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
343 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
345 boolean result = true;
348 boolean matching = false;
349 if ( !force && targetFile.exists() )
351 matching = FileUtils.contentEquals( sourceFile, targetFile );
354 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
360 if ( force || !matching )
362 if ( testChecksums( artifact, sourceFile ) )
364 transaction.copyFile( sourceFile, targetFile, digesters );
373 catch ( IOException e )
375 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
380 private Metadata createBaseMetadata( Artifact artifact )
382 Metadata metadata = new Metadata();
383 metadata.setArtifactId( artifact.getArtifactId() );
384 metadata.setGroupId( artifact.getGroupId() );
388 private Metadata readMetadata( File file )
389 throws ArtifactConversionException
392 MetadataXpp3Reader reader = new MetadataXpp3Reader();
393 FileReader fileReader = null;
396 fileReader = new FileReader( file );
397 metadata = reader.read( fileReader );
399 catch ( FileNotFoundException e )
401 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
403 catch ( IOException e )
405 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
407 catch ( XmlPullParserException e )
409 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
413 IOUtils.closeQuietly( fileReader );
418 private boolean validateMetadata( Artifact artifact )
419 throws ArtifactConversionException
421 ArtifactRepository repository = artifact.getRepository();
423 boolean result = true;
425 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
426 File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
429 Metadata metadata = readMetadata( file );
430 result = validateMetadata( metadata, repositoryMetadata, artifact );
433 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
434 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
437 Metadata metadata = readMetadata( file );
438 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
444 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
447 String artifactIdKey = null;
448 String snapshotKey = null;
449 String versionKey = null;
450 String versionsKey = null;
452 if ( repositoryMetadata.storedInGroupDirectory() )
454 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
456 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
458 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
459 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
460 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
461 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
465 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
466 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
467 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
470 boolean result = true;
472 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
474 addWarning( artifact, Messages.getString( groupIdKey ) );
477 if ( !repositoryMetadata.storedInGroupDirectory() )
479 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
481 addWarning( artifact, Messages.getString( artifactIdKey ) );
484 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
488 boolean foundVersion = false;
489 if ( metadata.getVersioning() != null )
491 for ( Iterator i = metadata.getVersioning().getVersions().iterator(); i.hasNext() && !foundVersion; )
493 String version = (String) i.next();
494 if ( version.equals( artifact.getBaseVersion() ) )
503 addWarning( artifact, Messages.getString( versionsKey ) );
510 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
512 addWarning( artifact, Messages.getString( versionKey ) );
516 if ( artifact.isSnapshot() )
518 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
519 if ( matcher.matches() )
521 boolean correct = false;
522 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
524 Snapshot snapshot = metadata.getVersioning().getSnapshot();
525 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
526 String ts = matcher.group( 2 );
527 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
535 addWarning( artifact, Messages.getString( snapshotKey ) );
545 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
546 Metadata newMetadata, FileTransaction transaction )
547 throws ArtifactConversionException
549 File file = new File( targetRepository.getBasedir(), targetRepository
550 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
557 metadata = readMetadata( file );
558 changed = metadata.merge( newMetadata );
563 metadata = newMetadata;
568 StringWriter writer = null;
571 writer = new StringWriter();
573 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
575 mappingWriter.write( writer, metadata );
577 transaction.createFile( writer.toString(), file, digesters );
579 catch ( IOException e )
581 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ), e ); //$NON-NLS-1$
585 IOUtils.closeQuietly( writer );
590 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
591 ArtifactRepository repository, FileTransaction transaction )
594 Properties properties = v3Model.getProperties();
595 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) //$NON-NLS-1$ //$NON-NLS-2$
596 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
598 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
599 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
601 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
602 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
604 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
605 properties.remove( "relocated.version" ); //$NON-NLS-1$
607 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
608 properties.remove( "relocated.message" ); //$NON-NLS-1$
610 if ( properties.isEmpty() )
612 v3Model.setProperties( null );
615 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
616 newArtifactId, newVersion, message, repository, transaction );
618 v3Model.setGroupId( newGroupId );
619 v3Model.setArtifactId( newArtifactId );
620 v3Model.setVersion( newVersion );
622 artifact.setGroupId( newGroupId );
623 artifact.setArtifactId( newArtifactId );
624 artifact.setVersion( newVersion );
634 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
635 String newArtifactId, String newVersion, String message,
636 ArtifactRepository repository, FileTransaction transaction )
639 Model pom = new Model();
640 pom.setGroupId( groupId );
641 pom.setArtifactId( artifactId );
642 pom.setVersion( version );
644 DistributionManagement dMngt = new DistributionManagement();
646 Relocation relocation = new Relocation();
647 relocation.setGroupId( newGroupId );
648 relocation.setArtifactId( newArtifactId );
649 relocation.setVersion( newVersion );
650 if ( message != null && message.length() > 0 )
652 relocation.setMessage( message );
655 dMngt.setRelocation( relocation );
657 pom.setDistributionManagement( dMngt );
659 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
660 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
662 StringWriter strWriter = new StringWriter();
663 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
664 pomWriter.write( strWriter, pom );
666 transaction.createFile( strWriter.toString(), pomFile, digesters );
669 private void addWarning( Artifact artifact, String message )
671 List messages = (List) warnings.get( artifact );
672 if ( messages == null )
674 messages = new ArrayList();
676 messages.add( message );
677 warnings.put( artifact, messages );
680 public void clearWarnings()
685 public Map getWarnings()