1 package org.apache.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.archiva.transaction.FileTransaction;
26 import org.apache.archiva.transaction.TransactionException;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.io.IOUtils;
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
72 @Service( "artifactConverter#legacy-to-default" )
73 public class LegacyToDefaultConverter
74 implements ArtifactConverter
77 * {@link List}<{@link Digester}
79 private List<? extends Digester> digesters;
82 private PlexusSisuBridge plexusSisuBridge;
85 private DigesterUtils digesterUtils;
90 private ModelConverter translator;
95 private ArtifactFactory artifactFactory;
100 private ArtifactHandlerManager artifactHandlerManager;
103 * default-value="false"
105 private boolean force;
108 * default-value="false"
110 private boolean dryrun;
112 private Map<Artifact, List<String>> warnings = new HashMap<Artifact, List<String>>();
115 public void initialize()
116 throws PlexusSisuBridgeException
118 this.digesters = digesterUtils.getAllDigesters();
119 translator = plexusSisuBridge.lookup( ModelConverter.class );
120 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
121 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
124 public void convert( Artifact artifact, ArtifactRepository targetRepository )
125 throws ArtifactConversionException
127 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
129 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
132 if ( !validateMetadata( artifact ) )
134 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
138 FileTransaction transaction = new FileTransaction();
140 if ( !copyPom( artifact, targetRepository, transaction ) )
142 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
146 if ( !copyArtifact( artifact, targetRepository, transaction ) )
148 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
152 Metadata metadata = createBaseMetadata( artifact );
153 Versioning versioning = new Versioning();
154 versioning.addVersion( artifact.getBaseVersion() );
155 metadata.setVersioning( versioning );
156 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
158 metadata = createBaseMetadata( artifact );
159 metadata.setVersion( artifact.getBaseVersion() );
160 versioning = new Versioning();
162 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
163 if ( matcher.matches() )
165 Snapshot snapshot = new Snapshot();
166 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
167 snapshot.setTimestamp( matcher.group( 2 ) );
168 versioning.setSnapshot( snapshot );
171 // TODO: merge latest/release/snapshot from source instead
172 metadata.setVersioning( versioning );
173 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
179 transaction.commit();
181 catch ( TransactionException e )
183 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
189 @SuppressWarnings( "unchecked" )
190 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
191 throws ArtifactConversionException
193 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
194 artifact.getVersion() );
195 pom.setBaseVersion( artifact.getBaseVersion() );
196 ArtifactRepository repository = artifact.getRepository();
197 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
199 boolean result = true;
202 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
204 String contents = null;
205 boolean checksumsValid = false;
208 if ( testChecksums( artifact, file ) )
210 checksumsValid = true;
213 // Even if the checksums for the POM are invalid we should still convert the POM
214 contents = FileUtils.readFileToString( file, null );
216 catch ( IOException e )
218 throw new ArtifactConversionException(
219 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
222 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
227 boolean matching = false;
228 if ( !force && targetFile.exists() )
230 String targetContents = FileUtils.readFileToString( targetFile, null );
231 matching = targetContents.equals( contents );
233 if ( force || !matching )
235 transaction.createFile( contents, targetFile, digesters );
238 catch ( IOException e )
240 throw new ArtifactConversionException(
241 Messages.getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
247 StringReader stringReader = new StringReader( contents );
248 StringWriter writer = null;
251 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
252 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
253 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
255 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
257 Artifact relocatedPom =
258 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
259 artifact.getVersion() );
260 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
263 Model v4Model = translator.translate( v3Model );
265 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
266 v3Model.getVersion(), v3Model.getPackage() );
268 writer = new StringWriter();
269 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
270 Xpp3Writer.write( writer, v4Model );
272 transaction.createFile( writer.toString(), targetFile, digesters );
274 List<String> warnings = translator.getWarnings();
276 for ( String message : warnings )
278 addWarning( artifact, message );
281 catch ( XmlPullParserException e )
283 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
286 catch ( IOException e )
288 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
291 catch ( PomTranslationException e )
293 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
298 IOUtils.closeQuietly( writer );
304 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
309 private boolean testChecksums( Artifact artifact, File file )
312 boolean result = true;
313 for ( Digester digester : digesters )
315 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester,
318 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
323 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
326 boolean result = true;
328 File checksumFile = new File( file.getParentFile(), fileName );
329 if ( checksumFile.exists() )
331 String checksum = FileUtils.readFileToString( checksumFile, null );
334 digester.verify( file, checksum );
336 catch ( DigesterException e )
338 addWarning( artifact, Messages.getString( key ) );
346 * File extension for checksums
347 * TODO should be moved to plexus-digester ?
349 private String getDigesterFileExtension( Digester digester )
351 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
354 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
355 throws ArtifactConversionException
357 File sourceFile = artifact.getFile();
359 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
361 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
364 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
366 boolean result = true;
369 boolean matching = false;
370 if ( !force && targetFile.exists() )
372 matching = FileUtils.contentEquals( sourceFile, targetFile );
375 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
381 if ( force || !matching )
383 if ( testChecksums( artifact, sourceFile ) )
385 transaction.copyFile( sourceFile, targetFile, digesters );
394 catch ( IOException e )
396 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
401 private Metadata createBaseMetadata( Artifact artifact )
403 Metadata metadata = new Metadata();
404 metadata.setArtifactId( artifact.getArtifactId() );
405 metadata.setGroupId( artifact.getGroupId() );
409 private Metadata readMetadata( File file )
410 throws ArtifactConversionException
413 MetadataXpp3Reader reader = new MetadataXpp3Reader();
414 FileReader fileReader = null;
417 fileReader = new FileReader( file );
418 metadata = reader.read( fileReader );
420 catch ( FileNotFoundException e )
422 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
425 catch ( IOException e )
427 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
430 catch ( XmlPullParserException e )
432 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
437 IOUtils.closeQuietly( fileReader );
442 private boolean validateMetadata( Artifact artifact )
443 throws ArtifactConversionException
445 ArtifactRepository repository = artifact.getRepository();
447 boolean result = true;
449 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
451 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
454 Metadata metadata = readMetadata( file );
455 result = validateMetadata( metadata, repositoryMetadata, artifact );
458 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
459 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
462 Metadata metadata = readMetadata( file );
463 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
469 @SuppressWarnings( "unchecked" )
470 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
473 String artifactIdKey = null;
474 String snapshotKey = null;
475 String versionKey = null;
476 String versionsKey = null;
478 if ( repositoryMetadata.storedInGroupDirectory() )
480 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
482 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
484 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
485 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
486 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
487 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
491 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
492 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
493 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
496 boolean result = true;
498 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
500 addWarning( artifact, Messages.getString( groupIdKey ) );
503 if ( !repositoryMetadata.storedInGroupDirectory() )
505 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
507 addWarning( artifact, Messages.getString( artifactIdKey ) );
510 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
514 boolean foundVersion = false;
515 if ( metadata.getVersioning() != null )
517 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
519 if ( version.equals( artifact.getBaseVersion() ) )
529 addWarning( artifact, Messages.getString( versionsKey ) );
536 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
538 addWarning( artifact, Messages.getString( versionKey ) );
542 if ( artifact.isSnapshot() )
544 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
545 if ( matcher.matches() )
547 boolean correct = false;
548 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
550 Snapshot snapshot = metadata.getVersioning().getSnapshot();
551 int build = Integer.parseInt( matcher.group( 3 ) );
552 String ts = matcher.group( 2 );
553 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
561 addWarning( artifact, Messages.getString( snapshotKey ) );
571 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
572 Metadata newMetadata, FileTransaction transaction )
573 throws ArtifactConversionException
575 File file = new File( targetRepository.getBasedir(),
576 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
583 metadata = readMetadata( file );
584 changed = metadata.merge( newMetadata );
589 metadata = newMetadata;
594 StringWriter writer = null;
597 writer = new StringWriter();
599 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
601 mappingWriter.write( writer, metadata );
603 transaction.createFile( writer.toString(), file, digesters );
605 catch ( IOException e )
607 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
612 IOUtils.closeQuietly( writer );
617 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
618 ArtifactRepository repository, FileTransaction transaction )
621 Properties properties = v3Model.getProperties();
622 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
623 //$NON-NLS-1$ //$NON-NLS-2$
624 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
626 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
627 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
629 String newArtifactId =
630 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
631 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
633 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
634 properties.remove( "relocated.version" ); //$NON-NLS-1$
636 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
637 properties.remove( "relocated.message" ); //$NON-NLS-1$
639 if ( properties.isEmpty() )
641 v3Model.setProperties( null );
644 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
645 newArtifactId, newVersion, message, repository, transaction );
647 v3Model.setGroupId( newGroupId );
648 v3Model.setArtifactId( newArtifactId );
649 v3Model.setVersion( newVersion );
651 artifact.setGroupId( newGroupId );
652 artifact.setArtifactId( newArtifactId );
653 artifact.setVersion( newVersion );
663 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
664 String newArtifactId, String newVersion, String message,
665 ArtifactRepository repository, FileTransaction transaction )
668 Model pom = new Model();
669 pom.setGroupId( groupId );
670 pom.setArtifactId( artifactId );
671 pom.setVersion( version );
673 DistributionManagement dMngt = new DistributionManagement();
675 Relocation relocation = new Relocation();
676 relocation.setGroupId( newGroupId );
677 relocation.setArtifactId( newArtifactId );
678 relocation.setVersion( newVersion );
679 if ( message != null && message.length() > 0 )
681 relocation.setMessage( message );
684 dMngt.setRelocation( relocation );
686 pom.setDistributionManagement( dMngt );
688 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
689 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
691 StringWriter strWriter = new StringWriter();
692 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
693 pomWriter.write( strWriter, pom );
695 transaction.createFile( strWriter.toString(), pomFile, digesters );
698 private void addWarning( Artifact artifact, String message )
700 List<String> messages = warnings.get( artifact );
701 if ( messages == null )
703 messages = new ArrayList<String>( 1 );
705 messages.add( message );
706 warnings.put( artifact, messages );
709 public void clearWarnings()
714 public Map<Artifact, List<String>> getWarnings()
720 public List<? extends Digester> getDigesters()
725 public void setDigesters( List<Digester> digesters )
727 this.digesters = digesters;
730 public ModelConverter getTranslator()
735 public void setTranslator( ModelConverter translator )
737 this.translator = translator;
740 public ArtifactFactory getArtifactFactory()
742 return artifactFactory;
745 public void setArtifactFactory( ArtifactFactory artifactFactory )
747 this.artifactFactory = artifactFactory;
750 public ArtifactHandlerManager getArtifactHandlerManager()
752 return artifactHandlerManager;
755 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
757 this.artifactHandlerManager = artifactHandlerManager;
760 public boolean isForce()
765 public void setForce( boolean force )
770 public boolean isDryrun()
775 public void setDryrun( boolean dryrun )
777 this.dryrun = dryrun;