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.digest.DigesterException;
41 import org.apache.maven.repository.reporting.ArtifactReporter;
42 import org.codehaus.plexus.i18n.I18N;
43 import org.codehaus.plexus.util.FileUtils;
44 import org.codehaus.plexus.util.IOUtil;
45 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
48 import java.io.FileNotFoundException;
49 import java.io.FileReader;
50 import java.io.IOException;
51 import java.io.StringReader;
52 import java.io.StringWriter;
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
69 * @plexus.requirement role-hint="sha1"
71 private Digester sha1Digester;
74 * @plexus.requirement role-hint="md5"
76 private Digester md5Digester;
81 private ArtifactFactory artifactFactory;
86 private ArtifactPomRewriter rewriter;
91 private ModelConverter translator;
94 * @plexus.configuration default-value="false"
96 private boolean force;
99 * @plexus.configuration default-value="false"
101 private boolean dryrun;
104 * @plexus.requirement
108 public void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
109 throws RepositoryConversionException
111 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
113 throw new RepositoryConversionException( getI18NString( "exception.repositories.match" ) );
116 if ( validateMetadata( artifact, reporter ) )
118 FileTransaction transaction = new FileTransaction();
120 if ( copyPom( artifact, targetRepository, reporter, transaction ) )
122 if ( copyArtifact( artifact, targetRepository, reporter, transaction ) )
124 Metadata metadata = createBaseMetadata( artifact );
125 Versioning versioning = new Versioning();
126 versioning.addVersion( artifact.getBaseVersion() );
127 metadata.setVersioning( versioning );
128 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
131 metadata = createBaseMetadata( artifact );
132 metadata.setVersion( artifact.getBaseVersion() );
133 versioning = new Versioning();
135 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
136 if ( matcher.matches() )
138 Snapshot snapshot = new Snapshot();
139 snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
140 snapshot.setTimestamp( matcher.group( 2 ) );
141 versioning.setSnapshot( snapshot );
144 // TODO: merge latest/release/snapshot from source instead
145 metadata.setVersioning( versioning );
146 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata,
151 transaction.commit();
153 reporter.addSuccess( artifact );
159 private static Metadata createBaseMetadata( Artifact artifact )
161 Metadata metadata = new Metadata();
162 metadata.setArtifactId( artifact.getArtifactId() );
163 metadata.setGroupId( artifact.getGroupId() );
167 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
168 Metadata newMetadata, FileTransaction transaction )
169 throws RepositoryConversionException
171 File file = new File( targetRepository.getBasedir(),
172 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
179 metadata = readMetadata( file );
180 changed = metadata.merge( newMetadata );
185 metadata = newMetadata;
190 StringWriter writer = null;
193 writer = new StringWriter();
195 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
197 mappingWriter.write( writer, metadata );
199 transaction.createFile( writer.toString(), file );
201 catch ( IOException e )
203 throw new RepositoryConversionException( "Error writing target metadata", e );
207 IOUtil.close( writer );
212 private Metadata readMetadata( File file )
213 throws RepositoryConversionException
216 MetadataXpp3Reader reader = new MetadataXpp3Reader();
217 FileReader fileReader = null;
220 fileReader = new FileReader( file );
221 metadata = reader.read( fileReader );
223 catch ( FileNotFoundException e )
225 throw new RepositoryConversionException( "Error reading target metadata", e );
227 catch ( IOException e )
229 throw new RepositoryConversionException( "Error reading target metadata", e );
231 catch ( XmlPullParserException e )
233 throw new RepositoryConversionException( "Error reading target metadata", e );
237 IOUtil.close( fileReader );
242 private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter )
243 throws RepositoryConversionException
245 ArtifactRepository repository = artifact.getRepository();
247 boolean result = true;
249 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
251 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
254 Metadata metadata = readMetadata( file );
255 result = validateMetadata( metadata, repositoryMetadata, artifact, reporter );
258 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
259 file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
262 Metadata metadata = readMetadata( file );
263 result = result && validateMetadata( metadata, repositoryMetadata, artifact, reporter );
269 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact,
270 ArtifactReporter reporter )
273 String artifactIdKey = null;
274 String snapshotKey = null;
275 String versionKey = null;
276 String versionsKey = null;
277 if ( repositoryMetadata.storedInGroupDirectory() )
279 groupIdKey = "failure.incorrect.groupMetadata.groupId";
281 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
283 groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
284 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
285 versionKey = "failure.incorrect.snapshotMetadata.version";
286 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
290 groupIdKey = "failure.incorrect.artifactMetadata.groupId";
291 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
292 versionsKey = "failure.incorrect.artifactMetadata.versions";
295 boolean result = true;
297 if ( !metadata.getGroupId().equals( artifact.getGroupId() ) )
299 reporter.addFailure( artifact, getI18NString( groupIdKey ) );
302 if ( !repositoryMetadata.storedInGroupDirectory() )
304 if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
306 reporter.addFailure( artifact, getI18NString( artifactIdKey ) );
309 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
313 boolean foundVersion = false;
314 if ( metadata.getVersioning() != null )
316 for ( Iterator i = metadata.getVersioning().getVersions().iterator();
317 i.hasNext() && !foundVersion; )
319 String version = (String) i.next();
320 if ( version.equals( artifact.getBaseVersion() ) )
329 reporter.addFailure( artifact, getI18NString( versionsKey ) );
336 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
338 reporter.addFailure( artifact, getI18NString( versionKey ) );
342 if ( artifact.isSnapshot() )
344 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
345 if ( matcher.matches() )
347 boolean correct = false;
348 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
350 Snapshot snapshot = metadata.getVersioning().getSnapshot();
351 int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
352 String ts = matcher.group( 2 );
353 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
361 reporter.addFailure( artifact, getI18NString( snapshotKey ) );
371 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter,
372 FileTransaction transaction )
373 throws RepositoryConversionException
375 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
376 artifact.getVersion() );
377 pom.setBaseVersion( artifact.getBaseVersion() );
378 ArtifactRepository repository = artifact.getRepository();
379 File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
381 boolean result = true;
384 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
386 String contents = null;
387 boolean checksumsValid = false;
390 if ( testChecksums( artifact, file, reporter ) )
392 checksumsValid = true;
393 contents = FileUtils.fileRead( file );
396 catch ( IOException e )
398 throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
401 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
406 boolean matching = false;
407 if ( !force && targetFile.exists() )
409 String targetContents = FileUtils.fileRead( targetFile );
410 matching = targetContents.equals( contents );
412 if ( force || !matching )
414 transaction.createFile( contents, targetFile );
417 catch ( IOException e )
419 throw new RepositoryConversionException( "Unable to write target POM: " + e.getMessage(), e );
425 StringReader stringReader = new StringReader( contents );
426 StringWriter writer = null;
429 MavenXpp3Reader v3Reader = new MavenXpp3Reader();
430 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
432 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
434 Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(),
435 artifact.getArtifactId(),
436 artifact.getVersion() );
437 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
440 Model v4Model = translator.translate( v3Model );
442 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
443 v3Model.getVersion(), v3Model.getPackage() );
445 writer = new StringWriter();
446 MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
447 Xpp3Writer.write( writer, v4Model );
449 transaction.createFile( writer.toString(), targetFile );
451 List warnings = translator.getWarnings();
453 for ( Iterator i = warnings.iterator(); i.hasNext(); )
455 String message = (String) i.next();
456 reporter.addWarning( artifact, message );
459 catch ( XmlPullParserException e )
461 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
464 catch ( IOException e )
466 throw new RepositoryConversionException( "Unable to write converted POM", e );
468 catch ( PomTranslationException e )
470 reporter.addFailure( artifact, getI18NString( "failure.invalid.source.pom", e.getMessage() ) );
475 IOUtil.close( writer );
481 reporter.addWarning( artifact, getI18NString( "warning.missing.pom" ) );
486 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
487 ArtifactRepository repository, FileTransaction transaction )
490 Properties properties = v3Model.getProperties();
491 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) ||
492 properties.containsKey( "relocated.version" ) )
494 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
495 properties.remove( "relocated.groupId" );
497 String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
498 properties.remove( "relocated.artifactId" );
500 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
501 properties.remove( "relocated.version" );
503 String message = properties.getProperty( "relocated.message", "" );
504 properties.remove( "relocated.message" );
506 if ( properties.isEmpty() )
508 v3Model.setProperties( null );
511 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
512 newArtifactId, newVersion, message, repository, transaction );
514 v3Model.setGroupId( newGroupId );
515 v3Model.setArtifactId( newArtifactId );
516 v3Model.setVersion( newVersion );
518 artifact.setGroupId( newGroupId );
519 artifact.setArtifactId( newArtifactId );
520 artifact.setVersion( newVersion );
530 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
531 String newArtifactId, String newVersion, String message,
532 ArtifactRepository repository, FileTransaction transaction )
535 Model pom = new Model();
536 pom.setGroupId( groupId );
537 pom.setArtifactId( artifactId );
538 pom.setVersion( version );
540 DistributionManagement dMngt = new DistributionManagement();
542 Relocation relocation = new Relocation();
543 relocation.setGroupId( newGroupId );
544 relocation.setArtifactId( newArtifactId );
545 relocation.setVersion( newVersion );
546 if ( message != null && message.length() > 0 )
548 relocation.setMessage( message );
551 dMngt.setRelocation( relocation );
553 pom.setDistributionManagement( dMngt );
555 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
556 File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
558 StringWriter strWriter = new StringWriter();
559 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
560 pomWriter.write( strWriter, pom );
562 transaction.createFile( strWriter.toString(), pomFile );
565 private String getI18NString( String key, String arg0 )
567 return i18n.format( getClass().getName(), Locale.getDefault(), key, arg0 );
570 private String getI18NString( String key )
572 return i18n.getString( getClass().getName(), Locale.getDefault(), key );
575 private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
580 verifyChecksum( file, file.getName() + ".md5", md5Digester, reporter, artifact, "failure.incorrect.md5" );
581 result = result && verifyChecksum( file, file.getName() + ".sha1", sha1Digester, reporter, artifact,
582 "failure.incorrect.sha1" );
586 private boolean verifyChecksum( File file, String fileName, Digester digester, ArtifactReporter reporter,
587 Artifact artifact, String key )
590 boolean result = true;
592 File checksumFile = new File( file.getParentFile(), fileName );
593 if ( checksumFile.exists() )
595 String checksum = FileUtils.fileRead( checksumFile );
598 digester.verify( file, checksum );
600 catch ( DigesterException e )
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 );