1 package org.apache.maven.repository.converter;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
23 import org.apache.maven.artifact.repository.metadata.Metadata;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Snapshot;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
30 import org.apache.maven.model.DistributionManagement;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Relocation;
33 import org.apache.maven.model.converter.ArtifactPomRewriter;
34 import org.apache.maven.model.converter.ModelConverter;
35 import org.apache.maven.model.converter.PomTranslationException;
36 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
37 import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
38 import org.apache.maven.repository.converter.transaction.FileTransaction;
39 import org.apache.maven.repository.digest.Digester;
40 import org.apache.maven.repository.reporting.ArtifactReporter;
41 import org.codehaus.plexus.i18n.I18N;
42 import org.codehaus.plexus.util.FileUtils;
43 import org.codehaus.plexus.util.IOUtil;
44 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
47 import java.io.FileNotFoundException;
48 import java.io.FileReader;
49 import java.io.IOException;
50 import java.io.StringReader;
51 import java.io.StringWriter;
52 import java.security.NoSuchAlgorithmException;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Properties;
57 import java.util.regex.Matcher;
60 * Implementation of repository conversion class.
62 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
63 * @plexus.component role="org.apache.maven.repository.converter.RepositoryConverter" role-hint="default"
65 public class DefaultRepositoryConverter
66 implements RepositoryConverter
71 private Digester digester;
76 private ArtifactFactory artifactFactory;
81 private ArtifactPomRewriter rewriter;
86 private ModelConverter translator;
89 * @plexus.configuration default-value="false"
91 private boolean force;
94 * @plexus.configuration default-value="false"
96 private boolean dryrun;
103 public void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
104 throws RepositoryConversionException
106 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
108 throw new RepositoryConversionException( getI18NString( "exception.repositories.match" ) );
111 if ( validateMetadata( artifact, reporter ) )
113 FileTransaction transaction = new FileTransaction();
115 if ( copyPom( artifact, targetRepository, reporter, transaction ) )
117 if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
119 Metadata metadata = createBaseMetadata( artifact );
120 Versioning versioning = new Versioning();
121 versioning.addVersion( artifact.getBaseVersion() );
122 metadata.setVersioning( versioning );
123 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
126 metadata = createBaseMetadata( artifact );
127 metadata.setVersion( artifact.getBaseVersion() );
128 versioning = new Versioning();
130 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
131 if ( matcher.matches() )
133 Snapshot snapshot = new Snapshot();
134 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
135 snapshot.setTimestamp( matcher.group( 2 ) );
136 versioning.setSnapshot( snapshot );
139 // TODO: merge latest/release/snapshot from source instead
140 metadata.setVersioning( versioning );
141 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
146 transaction.commit();
148 reporter.addSuccess( artifact );
154 private static Metadata createBaseMetadata( Artifact artifact )
156 Metadata metadata = new Metadata();
157 metadata.setArtifactId( artifact.getArtifactId() );
158 metadata.setGroupId( artifact.getGroupId() );
162 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
163 Metadata newMetadata, FileTransaction transaction )
164 throws RepositoryConversionException
166 File file = new File( targetRepository.getBasedir(),
167 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
174 metadata = readMetadata( file );
175 changed = metadata.merge( newMetadata );
180 metadata = newMetadata;
185 StringWriter writer = null;
188 writer = new StringWriter();
190 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
192 mappingWriter.write( writer, metadata );
194 transaction.createFile( writer.toString(), file );
196 catch ( IOException e )
198 throw new RepositoryConversionException( "Error writing target metadata", e );
202 IOUtil.close( writer );
207 private Metadata readMetadata( File file )
208 throws RepositoryConversionException
211 MetadataXpp3Reader reader = new MetadataXpp3Reader();
212 FileReader fileReader = null;
215 fileReader = new FileReader( file );
216 metadata = reader.read( fileReader );
218 catch ( FileNotFoundException e )
220 throw new RepositoryConversionException( "Error reading target metadata", e );
222 catch ( IOException e )
224 throw new RepositoryConversionException( "Error reading target metadata", e );
226 catch ( XmlPullParserException e )
228 throw new RepositoryConversionException( "Error reading target metadata", e );
232 IOUtil.close( fileReader );
237 private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter )
238 throws RepositoryConversionException
240 ArtifactRepository repository = artifact.getRepository();
242 boolean result = true;
244 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
246 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
249 Metadata metadata = readMetadata( file );
250 result = validateMetadata( metadata, repositoryMetadata, artifact, reporter );
253 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
254 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
257 Metadata metadata = readMetadata( file );
258 result = result && validateMetadata( metadata, repositoryMetadata, artifact, reporter );
264 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact,
265 ArtifactReporter reporter )
268 String artifactIdKey = null;
269 String snapshotKey = null;
270 String versionKey = null;
271 String versionsKey = null;
272 if ( repositoryMetadata.storedInGroupDirectory() )
274 groupIdKey = "failure.incorrect.groupMetadata.groupId";
276 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
278 groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
279 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
280 versionKey = "failure.incorrect.snapshotMetadata.version";
281 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
285 groupIdKey = "failure.incorrect.artifactMetadata.groupId";
286 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
287 versionsKey = "failure.incorrect.artifactMetadata.versions";
290 boolean result = true;
292 if ( !metadata.getGroupId().equals( artifact.getGroupId() ) )
294 reporter.addFailure( artifact, getI18NString( groupIdKey ) );
297 if ( !repositoryMetadata.storedInGroupDirectory() )
299 if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
301 reporter.addFailure( artifact, getI18NString( artifactIdKey ) );
304 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
308 boolean foundVersion = false;
309 if ( metadata.getVersioning() != null )
311 for ( Iterator i = metadata.getVersioning().getVersions().iterator();
312 i.hasNext() && !foundVersion; )
314 String version = (String) i.next();
315 if ( version.equals( artifact.getBaseVersion() ) )
324 reporter.addFailure( artifact, getI18NString( versionsKey ) );
331 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
333 reporter.addFailure( artifact, getI18NString( versionKey ) );
337 if ( artifact.isSnapshot() )
339 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
340 if ( matcher.matches() )
342 boolean correct = false;
343 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
345 Snapshot snapshot = metadata.getVersioning().getSnapshot();
346 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
347 String ts = matcher.group( 2 );
348 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
356 reporter.addFailure( artifact, getI18NString( snapshotKey ) );
366 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
367 FileTransaction transaction )
368 throws RepositoryConversionException
370 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
371 artifact.getVersion() );
372 pom.setBaseVersion( artifact.getBaseVersion() );
373 ArtifactRepository repository = artifact.getRepository();
374 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
376 boolean result = true;
379 // TODO: utility methods in the model converter
380 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
382 String contents = null;
383 boolean checksumsValid = false;
386 if ( testChecksums( artifact, file, reporter ) )
388 checksumsValid = true;
389 contents = FileUtils.fileRead( file );
392 catch ( IOException e )
394 throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
397 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
402 boolean matching = false;
403 if ( !force && targetFile.exists() )
405 String targetContents = FileUtils.fileRead( targetFile );
406 matching = targetContents.equals( contents );
408 if ( force || !matching )
410 transaction.createFile( contents, targetFile );
413 catch ( IOException e )
415 throw new RepositoryConversionException( "Unable to write target POM: " + e.getMessage(), e );
421 StringReader stringReader = new StringReader( contents );
422 StringWriter writer = null;
425 MavenXpp3Reader v3Reader = new MavenXpp3Reader();
426 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
428 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
430 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
431 artifact.getArtifactId(),
432 artifact.getVersion() );
433 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
436 Model v4Model = translator.translate( v3Model );
438 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
439 v3Model.getVersion(), v3Model.getPackage() );
441 writer = new StringWriter();
442 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
443 Xpp3Writer.write( writer, v4Model );
445 transaction.createFile( writer.toString(), targetFile );
447 List warnings = translator.getWarnings();
449 for ( Iterator i = warnings.iterator(); i.hasNext(); )
451 String message = (String) i.next();
452 reporter.addWarning( artifact, message );
455 catch ( XmlPullParserException e )
457 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
460 catch ( IOException e )
462 throw new RepositoryConversionException( "Unable to write converted POM", e );
464 catch ( PomTranslationException e )
466 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
471 IOUtil.close( writer );
477 reporter.addWarning( artifact, getI18NString( "warning.missing.pom" ) );
482 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
483 ArtifactRepository repository, FileTransaction transaction )
486 Properties properties = v3Model.getProperties();
487 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
488 properties.containsKey( "relocated.version" ) )
490 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
491 properties.remove( "relocated.groupId" );
493 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
494 properties.remove( "relocated.artifactId" );
496 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
497 properties.remove( "relocated.version" );
499 String message = properties.getProperty( "relocated.message", "" );
500 properties.remove( "relocated.message" );
502 if ( properties.isEmpty() )
504 v3Model.setProperties( null );
507 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
508 newArtifactId, newVersion, message, repository, transaction );
510 v3Model.setGroupId( newGroupId );
511 v3Model.setArtifactId( newArtifactId );
512 v3Model.setVersion( newVersion );
514 artifact.setGroupId( newGroupId );
515 artifact.setArtifactId( newArtifactId );
516 artifact.setVersion( newVersion );
526 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
527 String newArtifactId, String newVersion, String message,
528 ArtifactRepository repository, FileTransaction transaction )
531 Model pom = new Model();
532 pom.setGroupId( groupId );
533 pom.setArtifactId( artifactId );
534 pom.setVersion( version );
536 DistributionManagement dMngt = new DistributionManagement();
538 Relocation relocation = new Relocation();
539 relocation.setGroupId( newGroupId );
540 relocation.setArtifactId( newArtifactId );
541 relocation.setVersion( newVersion );
542 if ( message != null && message.length() > 0 )
544 relocation.setMessage( message );
547 dMngt.setRelocation( relocation );
549 pom.setDistributionManagement( dMngt );
551 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
552 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
554 StringWriter strWriter = new StringWriter();
555 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
556 pomWriter.write( strWriter, pom );
558 transaction.createFile( strWriter.toString(), pomFile );
561 private String getI18NString( String key, String arg0 )
563 return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
566 private String getI18NString( String key )
568 return i18n.getString( getClass().getName(), Locale.getDefault(), key );
571 private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
572 throws IOException, RepositoryConversionException
578 result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
579 "failure.incorrect.md5" );
580 result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
581 "failure.incorrect.sha1" );
583 catch ( NoSuchAlgorithmException e )
585 throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
590 private boolean verifyChecksum( File file, String fileName, String algorithm, ArtifactReporter reporter,
591 Artifact artifact, String key )
592 throws IOException, NoSuchAlgorithmException
594 boolean result = true;
596 File md5 = new File( file.getParentFile(), fileName );
599 String checksum = FileUtils.fileRead( md5 );
600 if ( !digester.verifyChecksum( file, checksum, algorithm ) )
602 reporter.addFailure( artifact, getI18NString( key ) );
609 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
610 FileTransaction transaction )
611 throws RepositoryConversionException
613 File sourceFile = artifact.getFile();
615 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
617 boolean result = true;
620 boolean matching = false;
621 if ( !force && targetFile.exists() )
623 matching = FileUtils.contentEquals( sourceFile, targetFile );
626 reporter.addFailure( artifact, getI18NString( "failure.target.already.exists" ) );
632 if ( force || !matching )
634 if ( testChecksums( artifact, sourceFile, reporter ) )
636 transaction.copyFile( sourceFile, targetFile );
645 catch ( IOException e )
647 throw new RepositoryConversionException( "Error copying artifact", e );
652 public void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
653 throws RepositoryConversionException
655 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
657 Artifact artifact = (Artifact) i.next();
658 convert( artifact, targetRepository, reporter );