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
72 @Service( "artifactConverter#legacy-to-default" )
73 public class LegacyToDefaultConverter
74 implements ArtifactConverter
77 * {@link List}<{@link Digester}
78 * plexus.requirement role="org.codehaus.plexus.digest.Digester"
80 private List<? extends Digester> digesters;
83 private PlexusSisuBridge plexusSisuBridge;
86 private DigesterUtils digesterUtils;
91 private ModelConverter translator;
96 private ArtifactFactory artifactFactory;
101 private ArtifactHandlerManager artifactHandlerManager;
104 * plexus.configuration default-value="false"
106 private boolean force;
109 * plexus.configuration default-value="false"
111 private boolean dryrun;
113 private Map<Artifact, List<String>> warnings = new HashMap<Artifact, List<String>>();
116 public void initialize()
117 throws PlexusSisuBridgeException
119 this.digesters = digesterUtils.getAllDigesters();
120 translator = plexusSisuBridge.lookup( ModelConverter.class );
121 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
122 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
125 public void convert( Artifact artifact, ArtifactRepository targetRepository )
126 throws ArtifactConversionException
128 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
130 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
133 if ( !validateMetadata( artifact ) )
135 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
139 FileTransaction transaction = new FileTransaction();
141 if ( !copyPom( artifact, targetRepository, transaction ) )
143 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
147 if ( !copyArtifact( artifact, targetRepository, transaction ) )
149 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
153 Metadata metadata = createBaseMetadata( artifact );
154 Versioning versioning = new Versioning();
155 versioning.addVersion( artifact.getBaseVersion() );
156 metadata.setVersioning( versioning );
157 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
159 metadata = createBaseMetadata( artifact );
160 metadata.setVersion( artifact.getBaseVersion() );
161 versioning = new Versioning();
163 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
164 if ( matcher.matches() )
166 Snapshot snapshot = new Snapshot();
167 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
168 snapshot.setTimestamp( matcher.group( 2 ) );
169 versioning.setSnapshot( snapshot );
172 // TODO: merge latest/release/snapshot from source instead
173 metadata.setVersioning( versioning );
174 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
180 transaction.commit();
182 catch ( TransactionException e )
184 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
190 @SuppressWarnings( "unchecked" )
191 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
192 throws ArtifactConversionException
194 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
195 artifact.getVersion() );
196 pom.setBaseVersion( artifact.getBaseVersion() );
197 ArtifactRepository repository = artifact.getRepository();
198 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
200 boolean result = true;
203 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
205 String contents = null;
206 boolean checksumsValid = false;
209 if ( testChecksums( artifact, file ) )
211 checksumsValid = true;
214 // Even if the checksums for the POM are invalid we should still convert the POM
215 contents = FileUtils.readFileToString( file, null );
217 catch ( IOException e )
219 throw new ArtifactConversionException(
220 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
223 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
228 boolean matching = false;
229 if ( !force && targetFile.exists() )
231 String targetContents = FileUtils.readFileToString( targetFile, null );
232 matching = targetContents.equals( contents );
234 if ( force || !matching )
236 transaction.createFile( contents, targetFile, digesters );
239 catch ( IOException e )
241 throw new ArtifactConversionException(
242 Messages.getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
248 StringReader stringReader = new StringReader( contents );
249 StringWriter writer = null;
252 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
253 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
254 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
256 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
258 Artifact relocatedPom =
259 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
260 artifact.getVersion() );
261 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
264 Model v4Model = translator.translate( v3Model );
266 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
267 v3Model.getVersion(), v3Model.getPackage() );
269 writer = new StringWriter();
270 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
271 Xpp3Writer.write( writer, v4Model );
273 transaction.createFile( writer.toString(), targetFile, digesters );
275 List<String> warnings = translator.getWarnings();
277 for ( String message : warnings )
279 addWarning( artifact, message );
282 catch ( XmlPullParserException e )
284 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
287 catch ( IOException e )
289 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
292 catch ( PomTranslationException e )
294 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
299 IOUtils.closeQuietly( writer );
305 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
310 private boolean testChecksums( Artifact artifact, File file )
313 boolean result = true;
314 for ( Digester digester : digesters )
316 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester,
319 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
324 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
327 boolean result = true;
329 File checksumFile = new File( file.getParentFile(), fileName );
330 if ( checksumFile.exists() )
332 String checksum = FileUtils.readFileToString( checksumFile, null );
335 digester.verify( file, checksum );
337 catch ( DigesterException e )
339 addWarning( artifact, Messages.getString( key ) );
347 * File extension for checksums
348 * TODO should be moved to plexus-digester ?
350 private String getDigesterFileExtension( Digester digester )
352 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
355 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
356 throws ArtifactConversionException
358 File sourceFile = artifact.getFile();
360 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
362 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
365 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
367 boolean result = true;
370 boolean matching = false;
371 if ( !force && targetFile.exists() )
373 matching = FileUtils.contentEquals( sourceFile, targetFile );
376 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
382 if ( force || !matching )
384 if ( testChecksums( artifact, sourceFile ) )
386 transaction.copyFile( sourceFile, targetFile, digesters );
395 catch ( IOException e )
397 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
402 private Metadata createBaseMetadata( Artifact artifact )
404 Metadata metadata = new Metadata();
405 metadata.setArtifactId( artifact.getArtifactId() );
406 metadata.setGroupId( artifact.getGroupId() );
410 private Metadata readMetadata( File file )
411 throws ArtifactConversionException
414 MetadataXpp3Reader reader = new MetadataXpp3Reader();
415 FileReader fileReader = null;
418 fileReader = new FileReader( file );
419 metadata = reader.read( fileReader );
421 catch ( FileNotFoundException e )
423 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
426 catch ( IOException e )
428 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
431 catch ( XmlPullParserException e )
433 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
438 IOUtils.closeQuietly( fileReader );
443 private boolean validateMetadata( Artifact artifact )
444 throws ArtifactConversionException
446 ArtifactRepository repository = artifact.getRepository();
448 boolean result = true;
450 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
452 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
455 Metadata metadata = readMetadata( file );
456 result = validateMetadata( metadata, repositoryMetadata, artifact );
459 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
460 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
463 Metadata metadata = readMetadata( file );
464 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
470 @SuppressWarnings( "unchecked" )
471 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
474 String artifactIdKey = null;
475 String snapshotKey = null;
476 String versionKey = null;
477 String versionsKey = null;
479 if ( repositoryMetadata.storedInGroupDirectory() )
481 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
483 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
485 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
486 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
487 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
488 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
492 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
493 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
494 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
497 boolean result = true;
499 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
501 addWarning( artifact, Messages.getString( groupIdKey ) );
504 if ( !repositoryMetadata.storedInGroupDirectory() )
506 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
508 addWarning( artifact, Messages.getString( artifactIdKey ) );
511 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
515 boolean foundVersion = false;
516 if ( metadata.getVersioning() != null )
518 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
520 if ( version.equals( artifact.getBaseVersion() ) )
530 addWarning( artifact, Messages.getString( versionsKey ) );
537 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
539 addWarning( artifact, Messages.getString( versionKey ) );
543 if ( artifact.isSnapshot() )
545 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
546 if ( matcher.matches() )
548 boolean correct = false;
549 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
551 Snapshot snapshot = metadata.getVersioning().getSnapshot();
552 int build = Integer.parseInt( matcher.group( 3 ) );
553 String ts = matcher.group( 2 );
554 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
562 addWarning( artifact, Messages.getString( snapshotKey ) );
572 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
573 Metadata newMetadata, FileTransaction transaction )
574 throws ArtifactConversionException
576 File file = new File( targetRepository.getBasedir(),
577 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
584 metadata = readMetadata( file );
585 changed = metadata.merge( newMetadata );
590 metadata = newMetadata;
595 StringWriter writer = null;
598 writer = new StringWriter();
600 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
602 mappingWriter.write( writer, metadata );
604 transaction.createFile( writer.toString(), file, digesters );
606 catch ( IOException e )
608 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
613 IOUtils.closeQuietly( writer );
618 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
619 ArtifactRepository repository, FileTransaction transaction )
622 Properties properties = v3Model.getProperties();
623 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
624 //$NON-NLS-1$ //$NON-NLS-2$
625 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
627 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
628 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
630 String newArtifactId =
631 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
632 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
634 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
635 properties.remove( "relocated.version" ); //$NON-NLS-1$
637 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
638 properties.remove( "relocated.message" ); //$NON-NLS-1$
640 if ( properties.isEmpty() )
642 v3Model.setProperties( null );
645 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
646 newArtifactId, newVersion, message, repository, transaction );
648 v3Model.setGroupId( newGroupId );
649 v3Model.setArtifactId( newArtifactId );
650 v3Model.setVersion( newVersion );
652 artifact.setGroupId( newGroupId );
653 artifact.setArtifactId( newArtifactId );
654 artifact.setVersion( newVersion );
664 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
665 String newArtifactId, String newVersion, String message,
666 ArtifactRepository repository, FileTransaction transaction )
669 Model pom = new Model();
670 pom.setGroupId( groupId );
671 pom.setArtifactId( artifactId );
672 pom.setVersion( version );
674 DistributionManagement dMngt = new DistributionManagement();
676 Relocation relocation = new Relocation();
677 relocation.setGroupId( newGroupId );
678 relocation.setArtifactId( newArtifactId );
679 relocation.setVersion( newVersion );
680 if ( message != null && message.length() > 0 )
682 relocation.setMessage( message );
685 dMngt.setRelocation( relocation );
687 pom.setDistributionManagement( dMngt );
689 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
690 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
692 StringWriter strWriter = new StringWriter();
693 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
694 pomWriter.write( strWriter, pom );
696 transaction.createFile( strWriter.toString(), pomFile, digesters );
699 private void addWarning( Artifact artifact, String message )
701 List<String> messages = warnings.get( artifact );
702 if ( messages == null )
704 messages = new ArrayList<String>();
706 messages.add( message );
707 warnings.put( artifact, messages );
710 public void clearWarnings()
715 public Map<Artifact, List<String>> getWarnings()
721 public List<? extends Digester> getDigesters()
726 public void setDigesters( List<Digester> digesters )
728 this.digesters = digesters;
731 public ModelConverter getTranslator()
736 public void setTranslator( ModelConverter translator )
738 this.translator = translator;
741 public ArtifactFactory getArtifactFactory()
743 return artifactFactory;
746 public void setArtifactFactory( ArtifactFactory artifactFactory )
748 this.artifactFactory = artifactFactory;
751 public ArtifactHandlerManager getArtifactHandlerManager()
753 return artifactHandlerManager;
756 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
758 this.artifactHandlerManager = artifactHandlerManager;
761 public boolean isForce()
766 public void setForce( boolean force )
771 public boolean isDryrun()
776 public void setDryrun( boolean dryrun )
778 this.dryrun = dryrun;