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.maven.artifact.Artifact;
29 import org.apache.maven.artifact.factory.ArtifactFactory;
30 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.Metadata;
34 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
35 import org.apache.maven.artifact.repository.metadata.Snapshot;
36 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
37 import org.apache.maven.artifact.repository.metadata.Versioning;
38 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
39 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
40 import org.apache.maven.model.DistributionManagement;
41 import org.apache.maven.model.Model;
42 import org.apache.maven.model.Relocation;
43 import org.apache.maven.model.converter.ModelConverter;
44 import org.apache.maven.model.converter.PomTranslationException;
45 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
46 import org.codehaus.plexus.digest.Digester;
47 import org.codehaus.plexus.digest.DigesterException;
48 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
49 import org.springframework.stereotype.Service;
51 import javax.annotation.PostConstruct;
52 import javax.inject.Inject;
53 import java.io.IOException;
54 import java.io.Reader;
55 import java.io.StringReader;
56 import java.io.StringWriter;
57 import java.nio.charset.Charset;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.nio.file.Paths;
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;
86 private ModelConverter translator;
88 private ArtifactFactory artifactFactory;
90 private ArtifactHandlerManager artifactHandlerManager;
92 private boolean force;
94 private boolean dryrun;
96 private Map<Artifact, List<String>> warnings = new HashMap<>();
99 public void initialize()
100 throws PlexusSisuBridgeException
102 this.digesters = digesterUtils.getAllDigesters();
103 translator = plexusSisuBridge.lookup( ModelConverter.class );
104 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
105 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
109 public void convert( Artifact artifact, ArtifactRepository targetRepository )
110 throws ArtifactConversionException
112 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
114 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
117 if ( !validateMetadata( artifact ) )
119 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
123 FileTransaction transaction = new FileTransaction();
125 if ( !copyPom( artifact, targetRepository, transaction ) )
127 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
131 if ( !copyArtifact( artifact, targetRepository, transaction ) )
133 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
137 Metadata metadata = createBaseMetadata( artifact );
138 Versioning versioning = new Versioning();
139 versioning.addVersion( artifact.getBaseVersion() );
140 metadata.setVersioning( versioning );
141 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
143 metadata = createBaseMetadata( artifact );
144 metadata.setVersion( artifact.getBaseVersion() );
145 versioning = new Versioning();
147 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
148 if ( matcher.matches() )
150 Snapshot snapshot = new Snapshot();
151 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
152 snapshot.setTimestamp( matcher.group( 2 ) );
153 versioning.setSnapshot( snapshot );
156 // TODO: merge latest/release/snapshot from source instead
157 metadata.setVersioning( versioning );
158 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
164 transaction.commit();
166 catch ( TransactionException e )
168 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
174 @SuppressWarnings("unchecked")
175 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
176 throws ArtifactConversionException
178 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
179 artifact.getVersion() );
180 pom.setBaseVersion( artifact.getBaseVersion() );
181 ArtifactRepository repository = artifact.getRepository();
182 Path file = Paths.get( repository.getBasedir(), repository.pathOf( pom ) );
184 boolean result = true;
185 if ( Files.exists(file) )
187 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
189 String contents = null;
190 boolean checksumsValid = false;
193 if ( testChecksums( artifact, file ) )
195 checksumsValid = true;
198 // Even if the checksums for the POM are invalid we should still convert the POM
199 contents = org.apache.archiva.common.utils.FileUtils.readFileToString( file, Charset.defaultCharset() );
201 catch ( IOException e )
203 throw new ArtifactConversionException(
204 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
207 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
210 boolean matching = false;
211 if ( !force && Files.exists( targetFile ) )
213 String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString( targetFile, Charset.defaultCharset( ) );
214 matching = targetContents.equals( contents );
216 if ( force || !matching )
218 transaction.createFile( contents, targetFile, digesters );
224 try (StringReader stringReader = new StringReader( contents ))
227 try (StringWriter writer = new StringWriter())
229 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
230 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
231 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
233 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
235 Artifact relocatedPom =
236 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
237 artifact.getVersion() );
239 Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
242 Model v4Model = translator.translate( v3Model );
244 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
245 v3Model.getVersion(), v3Model.getPackage() );
247 MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
248 xpp3Writer.write( writer, v4Model );
250 transaction.createFile( writer.toString(), targetFile, digesters );
252 List<String> warnings = translator.getWarnings();
254 for ( String message : warnings )
256 addWarning( artifact, message );
259 catch ( XmlPullParserException e )
261 addWarning( artifact,
262 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" ),
270 catch ( PomTranslationException e )
272 addWarning( artifact,
273 Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
281 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
286 private boolean testChecksums( Artifact artifact, Path file )
289 boolean result = true;
290 for ( Digester digester : digesters )
292 result &= verifyChecksum( file, file.getFileName() + "." + getDigesterFileExtension( digester ), digester,
295 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
300 private boolean verifyChecksum( Path file, String fileName, Digester digester, Artifact artifact, String key )
303 boolean result = true;
305 Path checksumFile = file.resolveSibling( fileName );
306 if ( Files.exists(checksumFile) )
308 String checksum = org.apache.archiva.common.utils.FileUtils.readFileToString( checksumFile, Charset.defaultCharset() );
311 digester.verify( file.toFile(), checksum );
313 catch ( DigesterException e )
315 addWarning( artifact, Messages.getString( key ) );
323 * File extension for checksums
324 * TODO should be moved to plexus-digester ?
326 private String getDigesterFileExtension( Digester digester )
328 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
331 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
332 throws ArtifactConversionException
334 Path sourceFile = artifact.getFile().toPath();
336 if ( sourceFile.toAbsolutePath().toString().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
338 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
341 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
343 boolean result = true;
346 boolean matching = false;
347 if ( !force && Files.exists(targetFile) )
349 matching = FileUtils.contentEquals( sourceFile.toFile(), targetFile.toFile() );
352 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
358 if ( force || !matching )
360 if ( testChecksums( artifact, sourceFile ) )
362 transaction.copyFile( sourceFile, targetFile, digesters );
371 catch ( IOException e )
373 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
378 private Metadata createBaseMetadata( Artifact artifact )
380 Metadata metadata = new Metadata();
381 metadata.setArtifactId( artifact.getArtifactId() );
382 metadata.setGroupId( artifact.getGroupId() );
386 private Metadata readMetadata( Path file )
387 throws ArtifactConversionException
389 MetadataXpp3Reader reader = new MetadataXpp3Reader();
391 try (Reader fileReader = Files.newBufferedReader( file, Charset.defaultCharset() ))
393 return reader.read( fileReader );
395 catch ( IOException | XmlPullParserException e )
397 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
402 private boolean validateMetadata( Artifact artifact )
403 throws ArtifactConversionException
405 ArtifactRepository repository = artifact.getRepository();
407 boolean result = true;
409 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
410 Path file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
411 if ( Files.exists(file) )
413 Metadata metadata = readMetadata( file );
414 result = validateMetadata( metadata, repositoryMetadata, artifact );
417 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
418 file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
419 if ( Files.exists(file) )
421 Metadata metadata = readMetadata( file );
422 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
428 @SuppressWarnings("unchecked")
429 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
432 String artifactIdKey = null;
433 String snapshotKey = null;
434 String versionKey = null;
435 String versionsKey = null;
437 if ( repositoryMetadata.storedInGroupDirectory() )
439 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
441 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
443 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
444 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
445 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
446 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
450 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
451 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
452 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
455 boolean result = true;
457 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
459 addWarning( artifact, Messages.getString( groupIdKey ) );
462 if ( !repositoryMetadata.storedInGroupDirectory() )
464 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
466 addWarning( artifact, Messages.getString( artifactIdKey ) );
469 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
473 boolean foundVersion = false;
474 if ( metadata.getVersioning() != null )
476 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
478 if ( version.equals( artifact.getBaseVersion() ) )
488 addWarning( artifact, Messages.getString( versionsKey ) );
495 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
497 addWarning( artifact, Messages.getString( versionKey ) );
501 if ( artifact.isSnapshot() )
503 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
504 if ( matcher.matches() )
506 boolean correct = false;
507 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
509 Snapshot snapshot = metadata.getVersioning().getSnapshot();
510 int build = Integer.parseInt( matcher.group( 3 ) );
511 String ts = matcher.group( 2 );
512 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
520 addWarning( artifact, Messages.getString( snapshotKey ) );
530 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
531 Metadata newMetadata, FileTransaction transaction )
532 throws ArtifactConversionException
534 Path file = Paths.get( targetRepository.getBasedir(),
535 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
540 if ( Files.exists(file) )
542 metadata = readMetadata( file );
543 changed = metadata.merge( newMetadata );
548 metadata = newMetadata;
554 try (StringWriter writer = new StringWriter())
556 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
558 mappingWriter.write( writer, metadata );
560 transaction.createFile( writer.toString(), file, digesters );
562 catch ( IOException e )
564 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
570 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
571 ArtifactRepository repository, FileTransaction transaction )
574 Properties properties = v3Model.getProperties();
575 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
576 //$NON-NLS-1$ //$NON-NLS-2$
577 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
579 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
580 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
582 String newArtifactId =
583 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
584 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
586 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
587 properties.remove( "relocated.version" ); //$NON-NLS-1$
589 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
590 properties.remove( "relocated.message" ); //$NON-NLS-1$
592 if ( properties.isEmpty() )
594 v3Model.setProperties( null );
597 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
598 newArtifactId, newVersion, message, repository, transaction );
600 v3Model.setGroupId( newGroupId );
601 v3Model.setArtifactId( newArtifactId );
602 v3Model.setVersion( newVersion );
604 artifact.setGroupId( newGroupId );
605 artifact.setArtifactId( newArtifactId );
606 artifact.setVersion( newVersion );
616 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
617 String newArtifactId, String newVersion, String message,
618 ArtifactRepository repository, FileTransaction transaction )
621 Model pom = new Model();
622 pom.setGroupId( groupId );
623 pom.setArtifactId( artifactId );
624 pom.setVersion( version );
626 DistributionManagement dMngt = new DistributionManagement();
628 Relocation relocation = new Relocation();
629 relocation.setGroupId( newGroupId );
630 relocation.setArtifactId( newArtifactId );
631 relocation.setVersion( newVersion );
632 if ( message != null && message.length() > 0 )
634 relocation.setMessage( message );
637 dMngt.setRelocation( relocation );
639 pom.setDistributionManagement( dMngt );
641 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
642 Path pomFile = Paths.get( repository.getBasedir(), repository.pathOf( artifact ) );
644 StringWriter strWriter = new StringWriter();
645 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
646 pomWriter.write( strWriter, pom );
648 transaction.createFile( strWriter.toString(), pomFile, digesters );
651 private void addWarning( Artifact artifact, String message )
653 List<String> messages = warnings.get( artifact );
654 if ( messages == null )
656 messages = new ArrayList<>( 1 );
658 messages.add( message );
659 warnings.put( artifact, messages );
663 public void clearWarnings()
669 public Map<Artifact, List<String>> getWarnings()
675 public List<? extends Digester> getDigesters()
680 public void setDigesters( List<Digester> digesters )
682 this.digesters = digesters;
685 public ModelConverter getTranslator()
690 public void setTranslator( ModelConverter translator )
692 this.translator = translator;
695 public ArtifactFactory getArtifactFactory()
697 return artifactFactory;
700 public void setArtifactFactory( ArtifactFactory artifactFactory )
702 this.artifactFactory = artifactFactory;
705 public ArtifactHandlerManager getArtifactHandlerManager()
707 return artifactHandlerManager;
710 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
712 this.artifactHandlerManager = artifactHandlerManager;
715 public boolean isForce()
720 public void setForce( boolean force )
725 public boolean isDryrun()
730 public void setDryrun( boolean dryrun )
732 this.dryrun = dryrun;