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
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
33 import java.util.regex.Matcher;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.maven.archiva.transaction.FileTransaction;
38 import org.apache.maven.archiva.transaction.TransactionException;
39 import org.apache.maven.artifact.Artifact;
40 import org.apache.maven.artifact.factory.ArtifactFactory;
41 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
42 import org.apache.maven.artifact.repository.ArtifactRepository;
43 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
44 import org.apache.maven.artifact.repository.metadata.Metadata;
45 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
46 import org.apache.maven.artifact.repository.metadata.Snapshot;
47 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
48 import org.apache.maven.artifact.repository.metadata.Versioning;
49 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
50 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
51 import org.apache.maven.model.DistributionManagement;
52 import org.apache.maven.model.Model;
53 import org.apache.maven.model.Relocation;
54 import org.apache.maven.model.converter.ModelConverter;
55 import org.apache.maven.model.converter.PomTranslationException;
56 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
57 import org.codehaus.plexus.digest.Digester;
58 import org.codehaus.plexus.digest.DigesterException;
59 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
62 * LegacyToDefaultConverter
66 * @plexus.component role="org.apache.maven.archiva.converter.artifact.ArtifactConverter"
67 * role-hint="legacy-to-default"
69 public class LegacyToDefaultConverter
70 implements ArtifactConverter
73 * {@link List}<{@link Digester}>
75 * @plexus.requirement role="org.codehaus.plexus.digest.Digester"
77 private List<Digester> digesters;
82 private ModelConverter translator;
87 private ArtifactFactory artifactFactory;
92 private ArtifactHandlerManager artifactHandlerManager;
95 * @plexus.configuration default-value="false"
97 private boolean force;
100 * @plexus.configuration default-value="false"
102 private boolean dryrun;
104 private Map<Artifact,List<String>> warnings = new HashMap<Artifact,List<String>>();
106 public void convert( Artifact artifact, ArtifactRepository targetRepository )
107 throws ArtifactConversionException
109 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
111 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
114 if ( !validateMetadata( artifact ) )
116 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
120 FileTransaction transaction = new FileTransaction();
122 if ( !copyPom( artifact, targetRepository, transaction ) )
124 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
128 if ( !copyArtifact( artifact, targetRepository, transaction ) )
130 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
134 Metadata metadata = createBaseMetadata( artifact );
135 Versioning versioning = new Versioning();
136 versioning.addVersion( artifact.getBaseVersion() );
137 metadata.setVersioning( versioning );
138 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
140 metadata = createBaseMetadata( artifact );
141 metadata.setVersion( artifact.getBaseVersion() );
142 versioning = new Versioning();
144 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
145 if ( matcher.matches() )
147 Snapshot snapshot = new Snapshot();
148 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
149 snapshot.setTimestamp( matcher.group( 2 ) );
150 versioning.setSnapshot( snapshot );
153 // TODO: merge latest/release/snapshot from source instead
154 metadata.setVersioning( versioning );
155 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
161 transaction.commit();
163 catch ( TransactionException e )
165 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ), e ); //$NON-NLS-1$
170 @SuppressWarnings("unchecked")
171 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
172 throws ArtifactConversionException
174 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact
176 pom.setBaseVersion( artifact.getBaseVersion() );
177 ArtifactRepository repository = artifact.getRepository();
178 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
180 boolean result = true;
183 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
185 String contents = null;
186 boolean checksumsValid = false;
189 if ( testChecksums( artifact, file ) )
191 checksumsValid = true;
194 // Even if the checksums for the POM are invalid we should still convert the POM
195 contents = FileUtils.readFileToString( file, null );
197 catch ( IOException e )
199 throw new ArtifactConversionException(
200 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
203 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
208 boolean matching = false;
209 if ( !force && targetFile.exists() )
211 String targetContents = FileUtils.readFileToString( targetFile, null );
212 matching = targetContents.equals( contents );
214 if ( force || !matching )
216 transaction.createFile( contents, targetFile, digesters );
219 catch ( IOException e )
221 throw new ArtifactConversionException( Messages
222 .getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
228 StringReader stringReader = new StringReader( contents );
229 StringWriter writer = null;
232 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
233 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
235 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
237 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact
238 .getArtifactId(), artifact.getVersion() );
239 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
242 Model v4Model = translator.translate( v3Model );
244 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model
245 .getVersion(), v3Model.getPackage() );
247 writer = new StringWriter();
248 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
249 Xpp3Writer.write( writer, v4Model );
251 transaction.createFile( writer.toString(), targetFile, digesters );
253 List<String> warnings = translator.getWarnings();
255 for ( String message : warnings )
257 addWarning( artifact, message );
260 catch ( XmlPullParserException e )
262 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
265 catch ( IOException e )
267 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), e ); //$NON-NLS-1$
269 catch ( PomTranslationException e )
271 addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
276 IOUtils.closeQuietly( writer );
282 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
287 private boolean testChecksums( Artifact artifact, File file )
290 boolean result = true;
291 for ( Digester digester : digesters )
293 result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$
294 artifact, "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
299 private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
302 boolean result = true;
304 File checksumFile = new File( file.getParentFile(), fileName );
305 if ( checksumFile.exists() )
307 String checksum = FileUtils.readFileToString( checksumFile, null );
310 digester.verify( file, checksum );
312 catch ( DigesterException e )
314 addWarning( artifact, Messages.getString( key ) );
322 * File extension for checksums
323 * TODO should be moved to plexus-digester ?
325 private String getDigesterFileExtension( Digester digester )
327 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
330 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
331 throws ArtifactConversionException
333 File sourceFile = artifact.getFile();
335 if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
337 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
340 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
342 boolean result = true;
345 boolean matching = false;
346 if ( !force && targetFile.exists() )
348 matching = FileUtils.contentEquals( sourceFile, targetFile );
351 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
357 if ( force || !matching )
359 if ( testChecksums( artifact, sourceFile ) )
361 transaction.copyFile( sourceFile, targetFile, digesters );
370 catch ( IOException e )
372 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
377 private Metadata createBaseMetadata( Artifact artifact )
379 Metadata metadata = new Metadata();
380 metadata.setArtifactId( artifact.getArtifactId() );
381 metadata.setGroupId( artifact.getGroupId() );
385 private Metadata readMetadata( File file )
386 throws ArtifactConversionException
389 MetadataXpp3Reader reader = new MetadataXpp3Reader();
390 FileReader fileReader = null;
393 fileReader = new FileReader( file );
394 metadata = reader.read( fileReader );
396 catch ( FileNotFoundException e )
398 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
400 catch ( IOException e )
402 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
404 catch ( XmlPullParserException e )
406 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
410 IOUtils.closeQuietly( fileReader );
415 private boolean validateMetadata( Artifact artifact )
416 throws ArtifactConversionException
418 ArtifactRepository repository = artifact.getRepository();
420 boolean result = true;
422 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
423 File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
426 Metadata metadata = readMetadata( file );
427 result = validateMetadata( metadata, repositoryMetadata, artifact );
430 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
431 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
434 Metadata metadata = readMetadata( file );
435 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
441 @SuppressWarnings("unchecked")
442 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
445 String artifactIdKey = null;
446 String snapshotKey = null;
447 String versionKey = null;
448 String versionsKey = null;
450 if ( repositoryMetadata.storedInGroupDirectory() )
452 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
454 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
456 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
457 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
458 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
459 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
463 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
464 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
465 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
468 boolean result = true;
470 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
472 addWarning( artifact, Messages.getString( groupIdKey ) );
475 if ( !repositoryMetadata.storedInGroupDirectory() )
477 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
479 addWarning( artifact, Messages.getString( artifactIdKey ) );
482 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
486 boolean foundVersion = false;
487 if ( metadata.getVersioning() != null )
489 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
491 if ( version.equals( artifact.getBaseVersion() ) )
501 addWarning( artifact, Messages.getString( versionsKey ) );
508 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
510 addWarning( artifact, Messages.getString( versionKey ) );
514 if ( artifact.isSnapshot() )
516 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
517 if ( matcher.matches() )
519 boolean correct = false;
520 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
522 Snapshot snapshot = metadata.getVersioning().getSnapshot();
523 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
524 String ts = matcher.group( 2 );
525 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
533 addWarning( artifact, Messages.getString( snapshotKey ) );
543 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
544 Metadata newMetadata, FileTransaction transaction )
545 throws ArtifactConversionException
547 File file = new File( targetRepository.getBasedir(), targetRepository
548 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
555 metadata = readMetadata( file );
556 changed = metadata.merge( newMetadata );
561 metadata = newMetadata;
566 StringWriter writer = null;
569 writer = new StringWriter();
571 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
573 mappingWriter.write( writer, metadata );
575 transaction.createFile( writer.toString(), file, digesters );
577 catch ( IOException e )
579 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ), e ); //$NON-NLS-1$
583 IOUtils.closeQuietly( writer );
588 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
589 ArtifactRepository repository, FileTransaction transaction )
592 Properties properties = v3Model.getProperties();
593 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) //$NON-NLS-1$ //$NON-NLS-2$
594 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
596 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
597 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
599 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
600 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
602 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
603 properties.remove( "relocated.version" ); //$NON-NLS-1$
605 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
606 properties.remove( "relocated.message" ); //$NON-NLS-1$
608 if ( properties.isEmpty() )
610 v3Model.setProperties( null );
613 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
614 newArtifactId, newVersion, message, repository, transaction );
616 v3Model.setGroupId( newGroupId );
617 v3Model.setArtifactId( newArtifactId );
618 v3Model.setVersion( newVersion );
620 artifact.setGroupId( newGroupId );
621 artifact.setArtifactId( newArtifactId );
622 artifact.setVersion( newVersion );
632 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
633 String newArtifactId, String newVersion, String message,
634 ArtifactRepository repository, FileTransaction transaction )
637 Model pom = new Model();
638 pom.setGroupId( groupId );
639 pom.setArtifactId( artifactId );
640 pom.setVersion( version );
642 DistributionManagement dMngt = new DistributionManagement();
644 Relocation relocation = new Relocation();
645 relocation.setGroupId( newGroupId );
646 relocation.setArtifactId( newArtifactId );
647 relocation.setVersion( newVersion );
648 if ( message != null && message.length() > 0 )
650 relocation.setMessage( message );
653 dMngt.setRelocation( relocation );
655 pom.setDistributionManagement( dMngt );
657 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
658 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
660 StringWriter strWriter = new StringWriter();
661 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
662 pomWriter.write( strWriter, pom );
664 transaction.createFile( strWriter.toString(), pomFile, digesters );
667 private void addWarning( Artifact artifact, String message )
669 List<String> messages = warnings.get( artifact );
670 if ( messages == null )
672 messages = new ArrayList<String>();
674 messages.add( message );
675 warnings.put( artifact, messages );
678 public void clearWarnings()
683 public Map<Artifact, List<String>> getWarnings()