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.archiva.common.plexusbridge.DigesterUtils;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.archiva.transaction.FileTransaction;
28 import org.apache.archiva.transaction.TransactionException;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.factory.ArtifactFactory;
31 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
34 import org.apache.maven.artifact.repository.metadata.Metadata;
35 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
36 import org.apache.maven.artifact.repository.metadata.Snapshot;
37 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
38 import org.apache.maven.artifact.repository.metadata.Versioning;
39 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
40 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
41 import org.apache.maven.model.DistributionManagement;
42 import org.apache.maven.model.Model;
43 import org.apache.maven.model.Relocation;
44 import org.apache.maven.model.converter.ModelConverter;
45 import org.apache.maven.model.converter.PomTranslationException;
46 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
47 import org.codehaus.plexus.digest.Digester;
48 import org.codehaus.plexus.digest.DigesterException;
49 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
50 import org.springframework.stereotype.Service;
52 import javax.annotation.PostConstruct;
53 import javax.inject.Inject;
55 import java.io.FileNotFoundException;
56 import java.io.FileReader;
57 import java.io.IOException;
58 import java.io.StringReader;
59 import java.io.StringWriter;
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.List;
64 import java.util.Properties;
65 import java.util.regex.Matcher;
68 * LegacyToDefaultConverter
71 * @plexus.component role="org.apache.maven.archiva.converter.artifact.ArtifactConverter"
72 * role-hint="legacy-to-default"
74 @Service( "artifactConverter#legacy-to-default" )
75 public class LegacyToDefaultConverter
76 implements ArtifactConverter
79 * {@link List}<{@link Digester}
80 * plexus.requirement role="org.codehaus.plexus.digest.Digester"
82 private List<? extends Digester> digesters;
85 private PlexusSisuBridge plexusSisuBridge;
88 private DigesterUtils digesterUtils;
93 private ModelConverter translator;
98 private ArtifactFactory artifactFactory;
103 private ArtifactHandlerManager artifactHandlerManager;
106 * plexus.configuration default-value="false"
108 private boolean force;
111 * plexus.configuration default-value="false"
113 private boolean dryrun;
115 private Map<Artifact, List<String>> warnings = new HashMap<Artifact, List<String>>();
118 public void initialize()
119 throws PlexusSisuBridgeException
121 this.digesters = digesterUtils.getAllDigesters();
122 translator = plexusSisuBridge.lookup( ModelConverter.class );
123 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
124 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
127 public void convert( Artifact artifact, ArtifactRepository targetRepository )
128 throws ArtifactConversionException
130 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
132 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
135 if ( !validateMetadata( artifact ) )
137 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
141 FileTransaction transaction = new FileTransaction();
143 if ( !copyPom( artifact, targetRepository, transaction ) )
145 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
149 if ( !copyArtifact( artifact, targetRepository, transaction ) )
151 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
155 Metadata metadata = createBaseMetadata( artifact );
156 Versioning versioning = new Versioning();
157 versioning.addVersion( artifact.getBaseVersion() );
158 metadata.setVersioning( versioning );
159 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
161 metadata = createBaseMetadata( artifact );
162 metadata.setVersion( artifact.getBaseVersion() );
163 versioning = new Versioning();
165 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
166 if ( matcher.matches() )
168 Snapshot snapshot = new Snapshot();
169 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
170 snapshot.setTimestamp( matcher.group( 2 ) );
171 versioning.setSnapshot( snapshot );
174 // TODO: merge latest/release/snapshot from source instead
175 metadata.setVersioning( versioning );
176 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
182 transaction.commit();
184 catch ( TransactionException e )
186 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
192 @SuppressWarnings( "unchecked" )
193 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
194 throws ArtifactConversionException
196 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
197 artifact.getVersion() );
198 pom.setBaseVersion( artifact.getBaseVersion() );
199 ArtifactRepository repository = artifact.getRepository();
200 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
202 boolean result = true;
205 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
207 String contents = null;
208 boolean checksumsValid = false;
211 if ( testChecksums( artifact, file ) )
213 checksumsValid = true;
216 // Even if the checksums for the POM are invalid we should still convert the POM
217 contents = FileUtils.readFileToString( file, null );
219 catch ( IOException e )
221 throw new ArtifactConversionException(
222 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
225 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
230 boolean matching = false;
231 if ( !force && targetFile.exists() )
233 String targetContents = FileUtils.readFileToString( targetFile, null );
234 matching = targetContents.equals( contents );
236 if ( force || !matching )
238 transaction.createFile( contents, targetFile, digesters );
241 catch ( IOException e )
243 throw new ArtifactConversionException(
244 Messages.getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
250 StringReader stringReader = new StringReader( contents );
251 StringWriter writer = null;
254 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
255 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
256 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
258 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
260 Artifact relocatedPom =
261 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
262 artifact.getVersion() );
263 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
266 Model v4Model = translator.translate( v3Model );
268 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
269 v3Model.getVersion(), v3Model.getPackage() );
271 writer = new StringWriter();
272 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
273 Xpp3Writer.write( writer, v4Model );
275 transaction.createFile( writer.toString(), targetFile, digesters );
277 List<String> warnings = translator.getWarnings();
279 for ( String message : warnings )
281 addWarning( artifact, message );
284 catch ( XmlPullParserException e )
286 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
289 catch ( IOException e )
291 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
294 catch ( PomTranslationException e )
296 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
301 IOUtils.closeQuietly( writer );
307 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
312 private boolean testChecksums( Artifact artifact, File file )
315 boolean result = true;
316 for ( Digester digester : digesters )
318 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester,
321 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
326 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
329 boolean result = true;
331 File checksumFile = new File( file.getParentFile(), fileName );
332 if ( checksumFile.exists() )
334 String checksum = FileUtils.readFileToString( checksumFile, null );
337 digester.verify( file, checksum );
339 catch ( DigesterException e )
341 addWarning( artifact, Messages.getString( key ) );
349 * File extension for checksums
350 * TODO should be moved to plexus-digester ?
352 private String getDigesterFileExtension( Digester digester )
354 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
357 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
358 throws ArtifactConversionException
360 File sourceFile = artifact.getFile();
362 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
364 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
367 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
369 boolean result = true;
372 boolean matching = false;
373 if ( !force && targetFile.exists() )
375 matching = FileUtils.contentEquals( sourceFile, targetFile );
378 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
384 if ( force || !matching )
386 if ( testChecksums( artifact, sourceFile ) )
388 transaction.copyFile( sourceFile, targetFile, digesters );
397 catch ( IOException e )
399 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
404 private Metadata createBaseMetadata( Artifact artifact )
406 Metadata metadata = new Metadata();
407 metadata.setArtifactId( artifact.getArtifactId() );
408 metadata.setGroupId( artifact.getGroupId() );
412 private Metadata readMetadata( File file )
413 throws ArtifactConversionException
416 MetadataXpp3Reader reader = new MetadataXpp3Reader();
417 FileReader fileReader = null;
420 fileReader = new FileReader( file );
421 metadata = reader.read( fileReader );
423 catch ( FileNotFoundException e )
425 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
428 catch ( IOException e )
430 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
433 catch ( XmlPullParserException e )
435 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
440 IOUtils.closeQuietly( fileReader );
445 private boolean validateMetadata( Artifact artifact )
446 throws ArtifactConversionException
448 ArtifactRepository repository = artifact.getRepository();
450 boolean result = true;
452 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
454 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
457 Metadata metadata = readMetadata( file );
458 result = validateMetadata( metadata, repositoryMetadata, artifact );
461 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
462 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
465 Metadata metadata = readMetadata( file );
466 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
472 @SuppressWarnings( "unchecked" )
473 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
476 String artifactIdKey = null;
477 String snapshotKey = null;
478 String versionKey = null;
479 String versionsKey = null;
481 if ( repositoryMetadata.storedInGroupDirectory() )
483 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
485 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
487 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
488 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
489 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
490 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
494 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
495 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
496 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
499 boolean result = true;
501 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
503 addWarning( artifact, Messages.getString( groupIdKey ) );
506 if ( !repositoryMetadata.storedInGroupDirectory() )
508 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
510 addWarning( artifact, Messages.getString( artifactIdKey ) );
513 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
517 boolean foundVersion = false;
518 if ( metadata.getVersioning() != null )
520 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
522 if ( version.equals( artifact.getBaseVersion() ) )
532 addWarning( artifact, Messages.getString( versionsKey ) );
539 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
541 addWarning( artifact, Messages.getString( versionKey ) );
545 if ( artifact.isSnapshot() )
547 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
548 if ( matcher.matches() )
550 boolean correct = false;
551 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
553 Snapshot snapshot = metadata.getVersioning().getSnapshot();
554 int build = Integer.parseInt( matcher.group( 3 ) );
555 String ts = matcher.group( 2 );
556 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
564 addWarning( artifact, Messages.getString( snapshotKey ) );
574 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
575 Metadata newMetadata, FileTransaction transaction )
576 throws ArtifactConversionException
578 File file = new File( targetRepository.getBasedir(),
579 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
586 metadata = readMetadata( file );
587 changed = metadata.merge( newMetadata );
592 metadata = newMetadata;
597 StringWriter writer = null;
600 writer = new StringWriter();
602 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
604 mappingWriter.write( writer, metadata );
606 transaction.createFile( writer.toString(), file, digesters );
608 catch ( IOException e )
610 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
615 IOUtils.closeQuietly( writer );
620 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
621 ArtifactRepository repository, FileTransaction transaction )
624 Properties properties = v3Model.getProperties();
625 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
626 //$NON-NLS-1$ //$NON-NLS-2$
627 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
629 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
630 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
632 String newArtifactId =
633 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
634 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
636 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
637 properties.remove( "relocated.version" ); //$NON-NLS-1$
639 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
640 properties.remove( "relocated.message" ); //$NON-NLS-1$
642 if ( properties.isEmpty() )
644 v3Model.setProperties( null );
647 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
648 newArtifactId, newVersion, message, repository, transaction );
650 v3Model.setGroupId( newGroupId );
651 v3Model.setArtifactId( newArtifactId );
652 v3Model.setVersion( newVersion );
654 artifact.setGroupId( newGroupId );
655 artifact.setArtifactId( newArtifactId );
656 artifact.setVersion( newVersion );
666 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
667 String newArtifactId, String newVersion, String message,
668 ArtifactRepository repository, FileTransaction transaction )
671 Model pom = new Model();
672 pom.setGroupId( groupId );
673 pom.setArtifactId( artifactId );
674 pom.setVersion( version );
676 DistributionManagement dMngt = new DistributionManagement();
678 Relocation relocation = new Relocation();
679 relocation.setGroupId( newGroupId );
680 relocation.setArtifactId( newArtifactId );
681 relocation.setVersion( newVersion );
682 if ( message != null && message.length() > 0 )
684 relocation.setMessage( message );
687 dMngt.setRelocation( relocation );
689 pom.setDistributionManagement( dMngt );
691 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
692 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
694 StringWriter strWriter = new StringWriter();
695 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
696 pomWriter.write( strWriter, pom );
698 transaction.createFile( strWriter.toString(), pomFile, digesters );
701 private void addWarning( Artifact artifact, String message )
703 List<String> messages = warnings.get( artifact );
704 if ( messages == null )
706 messages = new ArrayList<String>();
708 messages.add( message );
709 warnings.put( artifact, messages );
712 public void clearWarnings()
717 public Map<Artifact, List<String>> getWarnings()
723 public List<? extends Digester> getDigesters()
728 public void setDigesters( List<Digester> digesters )
730 this.digesters = digesters;
733 public ModelConverter getTranslator()
738 public void setTranslator( ModelConverter translator )
740 this.translator = translator;
743 public ArtifactFactory getArtifactFactory()
745 return artifactFactory;
748 public void setArtifactFactory( ArtifactFactory artifactFactory )
750 this.artifactFactory = artifactFactory;
753 public ArtifactHandlerManager getArtifactHandlerManager()
755 return artifactHandlerManager;
758 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
760 this.artifactHandlerManager = artifactHandlerManager;
763 public boolean isForce()
768 public void setForce( boolean force )
773 public boolean isDryrun()
778 public void setDryrun( boolean dryrun )
780 this.dryrun = dryrun;