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
65 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
68 * @plexus.component role="org.apache.maven.archiva.converter.artifact.ArtifactConverter"
69 * role-hint="legacy-to-default"
71 public class LegacyToDefaultConverter
72 implements ArtifactConverter
75 * {@link List}<{@link Digester}>
77 * @plexus.requirement role="org.codehaus.plexus.digest.Digester"
79 private List digesters;
84 private ModelConverter translator;
89 private ArtifactFactory artifactFactory;
94 private ArtifactHandlerManager artifactHandlerManager;
97 * @plexus.configuration default-value="false"
99 private boolean force;
102 * @plexus.configuration default-value="false"
104 private boolean dryrun;
106 private Map warnings = new HashMap();
108 public void convert( Artifact artifact, ArtifactRepository targetRepository )
109 throws ArtifactConversionException
111 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
113 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
116 if ( !validateMetadata( artifact ) )
118 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
122 FileTransaction transaction = new FileTransaction();
124 if ( !copyPom( artifact, targetRepository, transaction ) )
126 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
130 if ( !copyArtifact( artifact, targetRepository, transaction ) )
132 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
136 Metadata metadata = createBaseMetadata( artifact );
137 Versioning versioning = new Versioning();
138 versioning.addVersion( artifact.getBaseVersion() );
139 metadata.setVersioning( versioning );
140 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
142 metadata = createBaseMetadata( artifact );
143 metadata.setVersion( artifact.getBaseVersion() );
144 versioning = new Versioning();
146 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
147 if ( matcher.matches() )
149 Snapshot snapshot = new Snapshot();
150 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
151 snapshot.setTimestamp( matcher.group( 2 ) );
152 versioning.setSnapshot( snapshot );
155 // TODO: merge latest/release/snapshot from source instead
156 metadata.setVersioning( versioning );
157 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
163 transaction.commit();
165 catch ( TransactionException e )
167 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ), e ); //$NON-NLS-1$
172 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
173 throws ArtifactConversionException
175 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact
177 pom.setBaseVersion( artifact.getBaseVersion() );
178 ArtifactRepository repository = artifact.getRepository();
179 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
181 boolean result = true;
184 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
186 String contents = null;
187 boolean checksumsValid = false;
190 if ( testChecksums( artifact, file ) )
192 checksumsValid = true;
195 // Even if the checksums for the POM are invalid we should still convert the POM
196 contents = AsciiFileUtil.readFile( file );
198 catch ( IOException e )
200 throw new ArtifactConversionException(
201 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
204 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
209 boolean matching = false;
210 if ( !force && targetFile.exists() )
212 String targetContents = AsciiFileUtil.readFile( targetFile );
213 matching = targetContents.equals( contents );
215 if ( force || !matching )
217 transaction.createFile( contents, targetFile, digesters );
220 catch ( IOException e )
222 throw new ArtifactConversionException( Messages
223 .getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
229 StringReader stringReader = new StringReader( contents );
230 StringWriter writer = null;
233 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
234 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
236 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
238 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact
239 .getArtifactId(), artifact.getVersion() );
240 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
243 Model v4Model = translator.translate( v3Model );
245 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model
246 .getVersion(), v3Model.getPackage() );
248 writer = new StringWriter();
249 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
250 Xpp3Writer.write( writer, v4Model );
252 transaction.createFile( writer.toString(), targetFile, digesters );
254 List warnings = translator.getWarnings();
256 for ( Iterator i = warnings.iterator(); i.hasNext(); )
258 String message = (String) i.next();
259 addWarning( artifact, message );
262 catch ( XmlPullParserException e )
264 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
267 catch ( IOException e )
269 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), e ); //$NON-NLS-1$
271 catch ( PomTranslationException e )
273 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
278 IOUtils.closeQuietly( writer );
284 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
289 private boolean testChecksums( Artifact artifact, File file )
292 boolean result = true;
293 Iterator it = digesters.iterator();
294 while ( it.hasNext() )
296 Digester digester = (Digester) it.next();
297 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$
298 artifact, "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
303 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
306 boolean result = true;
308 File checksumFile = new File( file.getParentFile(), fileName );
309 if ( checksumFile.exists() )
311 String checksum = AsciiFileUtil.readFile( checksumFile );
314 digester.verify( file, checksum );
316 catch ( DigesterException e )
318 addWarning( artifact, Messages.getString( key ) );
326 * File extension for checksums
327 * TODO should be moved to plexus-digester ?
329 private String getDigesterFileExtension( Digester digester )
331 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
334 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
335 throws ArtifactConversionException
337 File sourceFile = artifact.getFile();
339 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
341 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
344 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
346 boolean result = true;
349 boolean matching = false;
350 if ( !force && targetFile.exists() )
352 matching = FileUtils.contentEquals( sourceFile, targetFile );
355 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
361 if ( force || !matching )
363 if ( testChecksums( artifact, sourceFile ) )
365 transaction.copyFile( sourceFile, targetFile, digesters );
374 catch ( IOException e )
376 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
381 private Metadata createBaseMetadata( Artifact artifact )
383 Metadata metadata = new Metadata();
384 metadata.setArtifactId( artifact.getArtifactId() );
385 metadata.setGroupId( artifact.getGroupId() );
389 private Metadata readMetadata( File file )
390 throws ArtifactConversionException
393 MetadataXpp3Reader reader = new MetadataXpp3Reader();
394 FileReader fileReader = null;
397 fileReader = new FileReader( file );
398 metadata = reader.read( fileReader );
400 catch ( FileNotFoundException e )
402 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
404 catch ( IOException e )
406 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
408 catch ( XmlPullParserException e )
410 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
414 IOUtils.closeQuietly( fileReader );
419 private boolean validateMetadata( Artifact artifact )
420 throws ArtifactConversionException
422 ArtifactRepository repository = artifact.getRepository();
424 boolean result = true;
426 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
427 File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
430 Metadata metadata = readMetadata( file );
431 result = validateMetadata( metadata, repositoryMetadata, artifact );
434 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
435 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
438 Metadata metadata = readMetadata( file );
439 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
445 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
448 String artifactIdKey = null;
449 String snapshotKey = null;
450 String versionKey = null;
451 String versionsKey = null;
453 if ( repositoryMetadata.storedInGroupDirectory() )
455 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
457 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
459 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
460 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
461 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
462 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
466 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
467 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
468 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
471 boolean result = true;
473 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
475 addWarning( artifact, Messages.getString( groupIdKey ) );
478 if ( !repositoryMetadata.storedInGroupDirectory() )
480 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
482 addWarning( artifact, Messages.getString( artifactIdKey ) );
485 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
489 boolean foundVersion = false;
490 if ( metadata.getVersioning() != null )
492 for ( Iterator i = metadata.getVersioning().getVersions().iterator(); i.hasNext() && !foundVersion; )
494 String version = (String) i.next();
495 if ( version.equals( artifact.getBaseVersion() ) )
504 addWarning( artifact, Messages.getString( versionsKey ) );
511 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
513 addWarning( artifact, Messages.getString( versionKey ) );
517 if ( artifact.isSnapshot() )
519 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
520 if ( matcher.matches() )
522 boolean correct = false;
523 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
525 Snapshot snapshot = metadata.getVersioning().getSnapshot();
526 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
527 String ts = matcher.group( 2 );
528 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
536 addWarning( artifact, Messages.getString( snapshotKey ) );
546 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
547 Metadata newMetadata, FileTransaction transaction )
548 throws ArtifactConversionException
550 File file = new File( targetRepository.getBasedir(), targetRepository
551 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
558 metadata = readMetadata( file );
559 changed = metadata.merge( newMetadata );
564 metadata = newMetadata;
569 StringWriter writer = null;
572 writer = new StringWriter();
574 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
576 mappingWriter.write( writer, metadata );
578 transaction.createFile( writer.toString(), file, digesters );
580 catch ( IOException e )
582 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ), e ); //$NON-NLS-1$
586 IOUtils.closeQuietly( writer );
591 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
592 ArtifactRepository repository, FileTransaction transaction )
595 Properties properties = v3Model.getProperties();
596 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) //$NON-NLS-1$ //$NON-NLS-2$
597 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
599 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
600 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
602 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
603 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
605 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
606 properties.remove( "relocated.version" ); //$NON-NLS-1$
608 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
609 properties.remove( "relocated.message" ); //$NON-NLS-1$
611 if ( properties.isEmpty() )
613 v3Model.setProperties( null );
616 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
617 newArtifactId, newVersion, message, repository, transaction );
619 v3Model.setGroupId( newGroupId );
620 v3Model.setArtifactId( newArtifactId );
621 v3Model.setVersion( newVersion );
623 artifact.setGroupId( newGroupId );
624 artifact.setArtifactId( newArtifactId );
625 artifact.setVersion( newVersion );
635 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
636 String newArtifactId, String newVersion, String message,
637 ArtifactRepository repository, FileTransaction transaction )
640 Model pom = new Model();
641 pom.setGroupId( groupId );
642 pom.setArtifactId( artifactId );
643 pom.setVersion( version );
645 DistributionManagement dMngt = new DistributionManagement();
647 Relocation relocation = new Relocation();
648 relocation.setGroupId( newGroupId );
649 relocation.setArtifactId( newArtifactId );
650 relocation.setVersion( newVersion );
651 if ( message != null && message.length() > 0 )
653 relocation.setMessage( message );
656 dMngt.setRelocation( relocation );
658 pom.setDistributionManagement( dMngt );
660 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
661 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
663 StringWriter strWriter = new StringWriter();
664 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
665 pomWriter.write( strWriter, pom );
667 transaction.createFile( strWriter.toString(), pomFile, digesters );
670 private void addWarning( Artifact artifact, String message )
672 List messages = (List) warnings.get( artifact );
673 if ( messages == null )
675 messages = new ArrayList();
677 messages.add( message );
678 warnings.put( artifact, messages );
681 public void clearWarnings()
686 public Map getWarnings()