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.nio.charset.Charset;
61 import java.util.ArrayList;
62 import java.util.HashMap;
63 import java.util.List;
65 import java.util.Properties;
66 import java.util.regex.Matcher;
69 * LegacyToDefaultConverter
71 @Service ("artifactConverter#legacy-to-default")
72 public class LegacyToDefaultConverter
73 implements ArtifactConverter
76 * {@link List}<{@link Digester}
78 private List<? extends Digester> digesters;
81 private PlexusSisuBridge plexusSisuBridge;
84 private DigesterUtils digesterUtils;
89 private ModelConverter translator;
94 private ArtifactFactory artifactFactory;
99 private ArtifactHandlerManager artifactHandlerManager;
102 * default-value="false"
104 private boolean force;
107 * default-value="false"
109 private boolean dryrun;
111 private Map<Artifact, List<String>> warnings = new HashMap<Artifact, List<String>>();
114 public void initialize()
115 throws PlexusSisuBridgeException
117 this.digesters = digesterUtils.getAllDigesters();
118 translator = plexusSisuBridge.lookup( ModelConverter.class );
119 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
120 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
123 public void convert( Artifact artifact, ArtifactRepository targetRepository )
124 throws ArtifactConversionException
126 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
128 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
131 if ( !validateMetadata( artifact ) )
133 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
137 FileTransaction transaction = new FileTransaction();
139 if ( !copyPom( artifact, targetRepository, transaction ) )
141 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
145 if ( !copyArtifact( artifact, targetRepository, transaction ) )
147 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
151 Metadata metadata = createBaseMetadata( artifact );
152 Versioning versioning = new Versioning();
153 versioning.addVersion( artifact.getBaseVersion() );
154 metadata.setVersioning( versioning );
155 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
157 metadata = createBaseMetadata( artifact );
158 metadata.setVersion( artifact.getBaseVersion() );
159 versioning = new Versioning();
161 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
162 if ( matcher.matches() )
164 Snapshot snapshot = new Snapshot();
165 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
166 snapshot.setTimestamp( matcher.group( 2 ) );
167 versioning.setSnapshot( snapshot );
170 // TODO: merge latest/release/snapshot from source instead
171 metadata.setVersioning( versioning );
172 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
178 transaction.commit();
180 catch ( TransactionException e )
182 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
188 @SuppressWarnings ("unchecked")
189 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
190 throws ArtifactConversionException
192 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
193 artifact.getVersion() );
194 pom.setBaseVersion( artifact.getBaseVersion() );
195 ArtifactRepository repository = artifact.getRepository();
196 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
198 boolean result = true;
201 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
203 String contents = null;
204 boolean checksumsValid = false;
207 if ( testChecksums( artifact, file ) )
209 checksumsValid = true;
212 // Even if the checksums for the POM are invalid we should still convert the POM
213 contents = FileUtils.readFileToString( file, Charset.defaultCharset() );
215 catch ( IOException e )
217 throw new ArtifactConversionException(
218 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
221 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
226 boolean matching = false;
227 if ( !force && targetFile.exists() )
229 String targetContents = FileUtils.readFileToString( targetFile, Charset.defaultCharset() );
230 matching = targetContents.equals( contents );
232 if ( force || !matching )
234 transaction.createFile( contents, targetFile, digesters );
237 catch ( IOException e )
239 throw new ArtifactConversionException(
240 Messages.getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
246 StringReader stringReader = new StringReader( contents );
247 StringWriter writer = null;
250 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
251 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
252 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
254 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
256 Artifact relocatedPom =
257 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
258 artifact.getVersion() );
259 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
262 Model v4Model = translator.translate( v3Model );
264 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
265 v3Model.getVersion(), v3Model.getPackage() );
267 writer = new StringWriter();
268 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
269 Xpp3Writer.write( writer, v4Model );
271 transaction.createFile( writer.toString(), targetFile, digesters );
273 List<String> warnings = translator.getWarnings();
275 for ( String message : warnings )
277 addWarning( artifact, message );
280 catch ( XmlPullParserException e )
282 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
285 catch ( IOException e )
287 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
290 catch ( PomTranslationException e )
292 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
297 IOUtils.closeQuietly( writer );
303 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
308 private boolean testChecksums( Artifact artifact, File file )
311 boolean result = true;
312 for ( Digester digester : digesters )
314 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester,
317 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
322 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
325 boolean result = true;
327 File checksumFile = new File( file.getParentFile(), fileName );
328 if ( checksumFile.exists() )
330 String checksum = FileUtils.readFileToString( checksumFile, Charset.defaultCharset() );
333 digester.verify( file, checksum );
335 catch ( DigesterException e )
337 addWarning( artifact, Messages.getString( key ) );
345 * File extension for checksums
346 * TODO should be moved to plexus-digester ?
348 private String getDigesterFileExtension( Digester digester )
350 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
353 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
354 throws ArtifactConversionException
356 File sourceFile = artifact.getFile();
358 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
360 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
363 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
365 boolean result = true;
368 boolean matching = false;
369 if ( !force && targetFile.exists() )
371 matching = FileUtils.contentEquals( sourceFile, targetFile );
374 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
380 if ( force || !matching )
382 if ( testChecksums( artifact, sourceFile ) )
384 transaction.copyFile( sourceFile, targetFile, digesters );
393 catch ( IOException e )
395 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
400 private Metadata createBaseMetadata( Artifact artifact )
402 Metadata metadata = new Metadata();
403 metadata.setArtifactId( artifact.getArtifactId() );
404 metadata.setGroupId( artifact.getGroupId() );
408 private Metadata readMetadata( File file )
409 throws ArtifactConversionException
412 MetadataXpp3Reader reader = new MetadataXpp3Reader();
413 FileReader fileReader = null;
416 fileReader = new FileReader( file );
417 metadata = reader.read( fileReader );
419 catch ( FileNotFoundException e )
421 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
424 catch ( IOException e )
426 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
429 catch ( XmlPullParserException e )
431 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
436 IOUtils.closeQuietly( fileReader );
441 private boolean validateMetadata( Artifact artifact )
442 throws ArtifactConversionException
444 ArtifactRepository repository = artifact.getRepository();
446 boolean result = true;
448 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
450 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
453 Metadata metadata = readMetadata( file );
454 result = validateMetadata( metadata, repositoryMetadata, artifact );
457 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
458 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
461 Metadata metadata = readMetadata( file );
462 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
468 @SuppressWarnings ("unchecked")
469 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
472 String artifactIdKey = null;
473 String snapshotKey = null;
474 String versionKey = null;
475 String versionsKey = null;
477 if ( repositoryMetadata.storedInGroupDirectory() )
479 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
481 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
483 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
484 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
485 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
486 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
490 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
491 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
492 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
495 boolean result = true;
497 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
499 addWarning( artifact, Messages.getString( groupIdKey ) );
502 if ( !repositoryMetadata.storedInGroupDirectory() )
504 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
506 addWarning( artifact, Messages.getString( artifactIdKey ) );
509 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
513 boolean foundVersion = false;
514 if ( metadata.getVersioning() != null )
516 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
518 if ( version.equals( artifact.getBaseVersion() ) )
528 addWarning( artifact, Messages.getString( versionsKey ) );
535 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
537 addWarning( artifact, Messages.getString( versionKey ) );
541 if ( artifact.isSnapshot() )
543 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
544 if ( matcher.matches() )
546 boolean correct = false;
547 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
549 Snapshot snapshot = metadata.getVersioning().getSnapshot();
550 int build = Integer.parseInt( matcher.group( 3 ) );
551 String ts = matcher.group( 2 );
552 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
560 addWarning( artifact, Messages.getString( snapshotKey ) );
570 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
571 Metadata newMetadata, FileTransaction transaction )
572 throws ArtifactConversionException
574 File file = new File( targetRepository.getBasedir(),
575 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
582 metadata = readMetadata( file );
583 changed = metadata.merge( newMetadata );
588 metadata = newMetadata;
593 StringWriter writer = null;
596 writer = new StringWriter();
598 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
600 mappingWriter.write( writer, metadata );
602 transaction.createFile( writer.toString(), file, digesters );
604 catch ( IOException e )
606 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
611 IOUtils.closeQuietly( writer );
616 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
617 ArtifactRepository repository, FileTransaction transaction )
620 Properties properties = v3Model.getProperties();
621 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
622 //$NON-NLS-1$ //$NON-NLS-2$
623 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
625 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
626 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
628 String newArtifactId =
629 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
630 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
632 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
633 properties.remove( "relocated.version" ); //$NON-NLS-1$
635 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
636 properties.remove( "relocated.message" ); //$NON-NLS-1$
638 if ( properties.isEmpty() )
640 v3Model.setProperties( null );
643 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
644 newArtifactId, newVersion, message, repository, transaction );
646 v3Model.setGroupId( newGroupId );
647 v3Model.setArtifactId( newArtifactId );
648 v3Model.setVersion( newVersion );
650 artifact.setGroupId( newGroupId );
651 artifact.setArtifactId( newArtifactId );
652 artifact.setVersion( newVersion );
662 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
663 String newArtifactId, String newVersion, String message,
664 ArtifactRepository repository, FileTransaction transaction )
667 Model pom = new Model();
668 pom.setGroupId( groupId );
669 pom.setArtifactId( artifactId );
670 pom.setVersion( version );
672 DistributionManagement dMngt = new DistributionManagement();
674 Relocation relocation = new Relocation();
675 relocation.setGroupId( newGroupId );
676 relocation.setArtifactId( newArtifactId );
677 relocation.setVersion( newVersion );
678 if ( message != null && message.length() > 0 )
680 relocation.setMessage( message );
683 dMngt.setRelocation( relocation );
685 pom.setDistributionManagement( dMngt );
687 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
688 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
690 StringWriter strWriter = new StringWriter();
691 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
692 pomWriter.write( strWriter, pom );
694 transaction.createFile( strWriter.toString(), pomFile, digesters );
697 private void addWarning( Artifact artifact, String message )
699 List<String> messages = warnings.get( artifact );
700 if ( messages == null )
702 messages = new ArrayList<String>( 1 );
704 messages.add( message );
705 warnings.put( artifact, messages );
708 public void clearWarnings()
713 public Map<Artifact, List<String>> getWarnings()
719 public List<? extends Digester> getDigesters()
724 public void setDigesters( List<Digester> digesters )
726 this.digesters = digesters;
729 public ModelConverter getTranslator()
734 public void setTranslator( ModelConverter translator )
736 this.translator = translator;
739 public ArtifactFactory getArtifactFactory()
741 return artifactFactory;
744 public void setArtifactFactory( ArtifactFactory artifactFactory )
746 this.artifactFactory = artifactFactory;
749 public ArtifactHandlerManager getArtifactHandlerManager()
751 return artifactHandlerManager;
754 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
756 this.artifactHandlerManager = artifactHandlerManager;
759 public boolean isForce()
764 public void setForce( boolean force )
769 public boolean isDryrun()
774 public void setDryrun( boolean dryrun )
776 this.dryrun = dryrun;