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.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksumValidationException;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.archiva.transaction.FileTransaction;
28 import org.apache.archiva.transaction.TransactionException;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.factory.ArtifactFactory;
32 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
35 import org.apache.maven.artifact.repository.metadata.Metadata;
36 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
37 import org.apache.maven.artifact.repository.metadata.Snapshot;
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.util.xml.pull.XmlPullParserException;
48 import org.springframework.stereotype.Service;
50 import javax.annotation.PostConstruct;
51 import javax.inject.Inject;
52 import java.io.IOException;
53 import java.io.Reader;
54 import java.io.StringReader;
55 import java.io.StringWriter;
56 import java.nio.charset.Charset;
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
60 import java.util.ArrayList;
61 import java.util.Arrays;
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
78 private List<ChecksumAlgorithm> digesters;
81 private PlexusSisuBridge plexusSisuBridge;
83 private ModelConverter translator;
85 private ArtifactFactory artifactFactory;
87 private ArtifactHandlerManager artifactHandlerManager;
89 private boolean force;
91 private boolean dryrun;
93 private Map<Artifact, List<String>> warnings = new HashMap<>();
96 public void initialize()
97 throws PlexusSisuBridgeException
99 // TODO: Should be configurable!
100 this.digesters = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5);
101 translator = plexusSisuBridge.lookup( ModelConverter.class );
102 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
103 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
107 public void convert( Artifact artifact, ArtifactRepository targetRepository )
108 throws ArtifactConversionException
110 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
112 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
115 if ( !validateMetadata( artifact ) )
117 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
121 FileTransaction transaction = new FileTransaction();
123 if ( !copyPom( artifact, targetRepository, transaction ) )
125 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
129 if ( !copyArtifact( artifact, targetRepository, transaction ) )
131 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
135 Metadata metadata = createBaseMetadata( artifact );
136 Versioning versioning = new Versioning();
137 versioning.addVersion( artifact.getBaseVersion() );
138 metadata.setVersioning( versioning );
139 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
141 metadata = createBaseMetadata( artifact );
142 metadata.setVersion( artifact.getBaseVersion() );
143 versioning = new Versioning();
145 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
146 if ( matcher.matches() )
148 Snapshot snapshot = new Snapshot();
149 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
150 snapshot.setTimestamp( matcher.group( 2 ) );
151 versioning.setSnapshot( snapshot );
154 // TODO: merge latest/release/snapshot from source instead
155 metadata.setVersioning( versioning );
156 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
162 transaction.commit();
164 catch ( TransactionException e )
166 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
172 @SuppressWarnings("unchecked")
173 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
174 throws ArtifactConversionException
176 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
177 artifact.getVersion() );
178 pom.setBaseVersion( artifact.getBaseVersion() );
179 ArtifactRepository repository = artifact.getRepository();
180 Path file = Paths.get( repository.getBasedir(), repository.pathOf( pom ) );
182 boolean result = true;
183 if ( Files.exists(file) )
185 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
187 String contents = null;
188 boolean checksumsValid = false;
191 if ( testChecksums( artifact, file ) )
193 checksumsValid = true;
196 // Even if the checksums for the POM are invalid we should still convert the POM
197 contents = org.apache.archiva.common.utils.FileUtils.readFileToString( file, Charset.defaultCharset() );
199 catch ( IOException e )
201 throw new ArtifactConversionException(
202 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
205 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
208 boolean matching = false;
209 if ( !force && Files.exists( targetFile ) )
211 String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString( targetFile, Charset.defaultCharset( ) );
212 matching = targetContents.equals( contents );
214 if ( force || !matching )
216 transaction.createFile( contents, targetFile, digesters );
222 try (StringReader stringReader = new StringReader( contents ))
225 try (StringWriter writer = new StringWriter())
227 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
228 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
229 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
231 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
233 Artifact relocatedPom =
234 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
235 artifact.getVersion() );
237 Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
240 Model v4Model = translator.translate( v3Model );
242 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
243 v3Model.getVersion(), v3Model.getPackage() );
245 MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
246 xpp3Writer.write( writer, v4Model );
248 transaction.createFile( writer.toString(), targetFile, digesters );
250 List<String> warnings = translator.getWarnings();
252 for ( String message : warnings )
254 addWarning( artifact, message );
257 catch ( XmlPullParserException e )
259 addWarning( artifact,
260 Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
263 catch ( IOException e )
265 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
268 catch ( PomTranslationException e )
270 addWarning( artifact,
271 Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
279 addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
284 private boolean testChecksums( Artifact artifact, Path file )
287 boolean result = true;
288 for ( ChecksumAlgorithm digester : digesters )
290 result &= verifyChecksum( file, file.getFileName() + "." + getDigesterFileExtension( digester ), digester,
293 "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
298 private boolean verifyChecksum( Path file, String fileName, ChecksumAlgorithm digester, Artifact artifact, String key )
302 Path checksumFile = file.resolveSibling( fileName );
303 // We ignore the check, if the checksum file does not exist
304 if (!Files.exists(checksumFile)) {
307 ChecksummedFile csFile = new ChecksummedFile( file );
310 result = csFile.isValidChecksum( digester, true );
311 } catch (ChecksumValidationException e ) {
312 addWarning( artifact, Messages.getString( key ) );
319 * File extension for checksums
320 * TODO should be moved to plexus-digester ?
322 private String getDigesterFileExtension( ChecksumAlgorithm checksumAlgorithm )
324 return checksumAlgorithm.getExt().get(0);
327 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
328 throws ArtifactConversionException
330 Path sourceFile = artifact.getFile().toPath();
332 if ( sourceFile.toAbsolutePath().toString().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
334 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
337 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
339 boolean result = true;
342 boolean matching = false;
343 if ( !force && Files.exists(targetFile) )
345 matching = FileUtils.contentEquals( sourceFile.toFile(), targetFile.toFile() );
348 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
354 if ( force || !matching )
356 if ( testChecksums( artifact, sourceFile ) )
358 transaction.copyFile( sourceFile, targetFile, digesters );
367 catch ( IOException e )
369 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
374 private Metadata createBaseMetadata( Artifact artifact )
376 Metadata metadata = new Metadata();
377 metadata.setArtifactId( artifact.getArtifactId() );
378 metadata.setGroupId( artifact.getGroupId() );
382 private Metadata readMetadata( Path file )
383 throws ArtifactConversionException
385 MetadataXpp3Reader reader = new MetadataXpp3Reader();
387 try (Reader fileReader = Files.newBufferedReader( file, Charset.defaultCharset() ))
389 return reader.read( fileReader );
391 catch ( IOException | XmlPullParserException e )
393 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
398 private boolean validateMetadata( Artifact artifact )
399 throws ArtifactConversionException
401 ArtifactRepository repository = artifact.getRepository();
403 boolean result = true;
405 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
406 Path file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
407 if ( Files.exists(file) )
409 Metadata metadata = readMetadata( file );
410 result = validateMetadata( metadata, repositoryMetadata, artifact );
413 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
414 file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
415 if ( Files.exists(file) )
417 Metadata metadata = readMetadata( file );
418 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
424 @SuppressWarnings("unchecked")
425 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
428 String artifactIdKey = null;
429 String snapshotKey = null;
430 String versionKey = null;
431 String versionsKey = null;
433 if ( repositoryMetadata.storedInGroupDirectory() )
435 groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
437 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
439 groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
440 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
441 versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
442 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
446 groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
447 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
448 versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
451 boolean result = true;
453 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
455 addWarning( artifact, Messages.getString( groupIdKey ) );
458 if ( !repositoryMetadata.storedInGroupDirectory() )
460 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
462 addWarning( artifact, Messages.getString( artifactIdKey ) );
465 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
469 boolean foundVersion = false;
470 if ( metadata.getVersioning() != null )
472 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
474 if ( version.equals( artifact.getBaseVersion() ) )
484 addWarning( artifact, Messages.getString( versionsKey ) );
491 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
493 addWarning( artifact, Messages.getString( versionKey ) );
497 if ( artifact.isSnapshot() )
499 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
500 if ( matcher.matches() )
502 boolean correct = false;
503 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
505 Snapshot snapshot = metadata.getVersioning().getSnapshot();
506 int build = Integer.parseInt( matcher.group( 3 ) );
507 String ts = matcher.group( 2 );
508 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
516 addWarning( artifact, Messages.getString( snapshotKey ) );
526 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
527 Metadata newMetadata, FileTransaction transaction )
528 throws ArtifactConversionException
530 Path file = Paths.get( targetRepository.getBasedir(),
531 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
536 if ( Files.exists(file) )
538 metadata = readMetadata( file );
539 changed = metadata.merge( newMetadata );
544 metadata = newMetadata;
550 try (StringWriter writer = new StringWriter())
552 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
554 mappingWriter.write( writer, metadata );
556 transaction.createFile( writer.toString(), file, digesters );
558 catch ( IOException e )
560 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
566 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
567 ArtifactRepository repository, FileTransaction transaction )
570 Properties properties = v3Model.getProperties();
571 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
572 //$NON-NLS-1$ //$NON-NLS-2$
573 || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
575 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
576 properties.remove( "relocated.groupId" ); //$NON-NLS-1$
578 String newArtifactId =
579 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
580 properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
582 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
583 properties.remove( "relocated.version" ); //$NON-NLS-1$
585 String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
586 properties.remove( "relocated.message" ); //$NON-NLS-1$
588 if ( properties.isEmpty() )
590 v3Model.setProperties( null );
593 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
594 newArtifactId, newVersion, message, repository, transaction );
596 v3Model.setGroupId( newGroupId );
597 v3Model.setArtifactId( newArtifactId );
598 v3Model.setVersion( newVersion );
600 artifact.setGroupId( newGroupId );
601 artifact.setArtifactId( newArtifactId );
602 artifact.setVersion( newVersion );
612 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
613 String newArtifactId, String newVersion, String message,
614 ArtifactRepository repository, FileTransaction transaction )
617 Model pom = new Model();
618 pom.setGroupId( groupId );
619 pom.setArtifactId( artifactId );
620 pom.setVersion( version );
622 DistributionManagement dMngt = new DistributionManagement();
624 Relocation relocation = new Relocation();
625 relocation.setGroupId( newGroupId );
626 relocation.setArtifactId( newArtifactId );
627 relocation.setVersion( newVersion );
628 if ( message != null && message.length() > 0 )
630 relocation.setMessage( message );
633 dMngt.setRelocation( relocation );
635 pom.setDistributionManagement( dMngt );
637 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
638 Path pomFile = Paths.get( repository.getBasedir(), repository.pathOf( artifact ) );
640 StringWriter strWriter = new StringWriter();
641 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
642 pomWriter.write( strWriter, pom );
644 transaction.createFile( strWriter.toString(), pomFile, digesters );
647 private void addWarning( Artifact artifact, String message )
649 List<String> messages = warnings.get( artifact );
650 if ( messages == null )
652 messages = new ArrayList<>( 1 );
654 messages.add( message );
655 warnings.put( artifact, messages );
659 public void clearWarnings()
665 public Map<Artifact, List<String>> getWarnings()
671 public List<ChecksumAlgorithm> getDigesters()
676 public void setDigesters( List<ChecksumAlgorithm> digesters )
678 this.digesters = digesters;
681 public ModelConverter getTranslator()
686 public void setTranslator( ModelConverter translator )
688 this.translator = translator;
691 public ArtifactFactory getArtifactFactory()
693 return artifactFactory;
696 public void setArtifactFactory( ArtifactFactory artifactFactory )
698 this.artifactFactory = artifactFactory;
701 public ArtifactHandlerManager getArtifactHandlerManager()
703 return artifactHandlerManager;
706 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
708 this.artifactHandlerManager = artifactHandlerManager;
711 public boolean isForce()
716 public void setForce( boolean force )
721 public boolean isDryrun()
726 public void setDryrun( boolean dryrun )
728 this.dryrun = dryrun;