Coverage Report - org.apache.maven.archiva.converter.artifact.LegacyToDefaultConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
LegacyToDefaultConverter
0%
0/272
0%
0/118
0
 
 1  
 package org.apache.maven.archiva.converter.artifact;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 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;
 31  
 import java.util.Map;
 32  
 import java.util.Properties;
 33  
 import java.util.regex.Matcher;
 34  
 
 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;
 60  
 
 61  
 /**
 62  
  * LegacyToDefaultConverter 
 63  
  *
 64  
  * @version $Id: LegacyToDefaultConverter.java 755239 2009-03-17 13:40:10Z brett $
 65  
  * 
 66  
  * @plexus.component role="org.apache.maven.archiva.converter.artifact.ArtifactConverter" 
 67  
  *      role-hint="legacy-to-default"
 68  
  */
 69  0
 public class LegacyToDefaultConverter
 70  
     implements ArtifactConverter
 71  
 {
 72  
     /**
 73  
      * {@link List}<{@link Digester}>
 74  
      * 
 75  
      * @plexus.requirement role="org.codehaus.plexus.digest.Digester"
 76  
      */
 77  
     private List<Digester> digesters;
 78  
 
 79  
     /**
 80  
      * @plexus.requirement
 81  
      */
 82  
     private ModelConverter translator;
 83  
 
 84  
     /**
 85  
      * @plexus.requirement
 86  
      */
 87  
     private ArtifactFactory artifactFactory;
 88  
 
 89  
     /**
 90  
      * @plexus.requirement
 91  
      */
 92  
     private ArtifactHandlerManager artifactHandlerManager;
 93  
 
 94  
     /**
 95  
      * @plexus.configuration default-value="false"
 96  
      */
 97  
     private boolean force;
 98  
 
 99  
     /**
 100  
      * @plexus.configuration default-value="false"
 101  
      */
 102  
     private boolean dryrun;
 103  
 
 104  0
     private Map<Artifact,List<String>> warnings = new HashMap<Artifact,List<String>>();
 105  
 
 106  
     public void convert( Artifact artifact, ArtifactRepository targetRepository )
 107  
         throws ArtifactConversionException
 108  
     {
 109  0
         if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
 110  
         {
 111  0
             throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) ); //$NON-NLS-1$
 112  
         }
 113  
 
 114  0
         if ( !validateMetadata( artifact ) )
 115  
         {
 116  0
             addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) ); //$NON-NLS-1$
 117  0
             return;
 118  
         }
 119  
 
 120  0
         FileTransaction transaction = new FileTransaction();
 121  
 
 122  0
         if ( !copyPom( artifact, targetRepository, transaction ) )
 123  
         {
 124  0
             addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) ); //$NON-NLS-1$
 125  0
             return;
 126  
         }
 127  
 
 128  0
         if ( !copyArtifact( artifact, targetRepository, transaction ) )
 129  
         {
 130  0
             addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) ); //$NON-NLS-1$
 131  0
             return;
 132  
         }
 133  
 
 134  0
         Metadata metadata = createBaseMetadata( artifact );
 135  0
         Versioning versioning = new Versioning();
 136  0
         versioning.addVersion( artifact.getBaseVersion() );
 137  0
         metadata.setVersioning( versioning );
 138  0
         updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
 139  
 
 140  0
         metadata = createBaseMetadata( artifact );
 141  0
         metadata.setVersion( artifact.getBaseVersion() );
 142  0
         versioning = new Versioning();
 143  
 
 144  0
         Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
 145  0
         if ( matcher.matches() )
 146  
         {
 147  0
             Snapshot snapshot = new Snapshot();
 148  0
             snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() );
 149  0
             snapshot.setTimestamp( matcher.group( 2 ) );
 150  0
             versioning.setSnapshot( snapshot );
 151  
         }
 152  
 
 153  
         // TODO: merge latest/release/snapshot from source instead
 154  0
         metadata.setVersioning( versioning );
 155  0
         updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
 156  
 
 157  0
         if ( !dryrun )
 158  
         {
 159  
             try
 160  
             {
 161  0
                 transaction.commit();
 162  
             }
 163  0
             catch ( TransactionException e )
 164  
             {
 165  0
                 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ), e ); //$NON-NLS-1$
 166  0
             }
 167  
         }
 168  0
     }
 169  
 
 170  
     @SuppressWarnings("unchecked")
 171  
     private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
 172  
         throws ArtifactConversionException
 173  
     {
 174  0
         Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact
 175  
             .getVersion() );
 176  0
         pom.setBaseVersion( artifact.getBaseVersion() );
 177  0
         ArtifactRepository repository = artifact.getRepository();
 178  0
         File file = new File( repository.getBasedir(), repository.pathOf( pom ) );
 179  
 
 180  0
         boolean result = true;
 181  0
         if ( file.exists() )
 182  
         {
 183  0
             File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
 184  
 
 185  0
             String contents = null;
 186  0
             boolean checksumsValid = false;
 187  
             try
 188  
             {
 189  0
                 if ( testChecksums( artifact, file ) )
 190  
                 {
 191  0
                     checksumsValid = true;
 192  
                 }
 193  
 
 194  
                 // Even if the checksums for the POM are invalid we should still convert the POM
 195  0
                 contents = FileUtils.readFileToString( file, null );
 196  
             }
 197  0
             catch ( IOException e )
 198  
             {
 199  0
                 throw new ArtifactConversionException(
 200  
                                                        Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e ); //$NON-NLS-1$
 201  0
             }
 202  
 
 203  0
             if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$
 204  
             {
 205  
                 // v4 POM
 206  
                 try
 207  
                 {
 208  0
                     boolean matching = false;
 209  0
                     if ( !force && targetFile.exists() )
 210  
                     {
 211  0
                         String targetContents = FileUtils.readFileToString( targetFile, null );
 212  0
                         matching = targetContents.equals( contents );
 213  
                     }
 214  0
                     if ( force || !matching )
 215  
                     {
 216  0
                         transaction.createFile( contents, targetFile, digesters );
 217  
                     }
 218  
                 }
 219  0
                 catch ( IOException e )
 220  
                 {
 221  0
                     throw new ArtifactConversionException( Messages
 222  
                         .getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$
 223  0
                 }
 224  
             }
 225  
             else
 226  
             {
 227  
                 // v3 POM
 228  0
                 StringReader stringReader = new StringReader( contents );
 229  0
                 StringWriter writer = null;
 230  
                 try
 231  
                 {
 232  0
                     org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
 233  0
                     org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
 234  
 
 235  0
                     if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
 236  
                     {
 237  0
                         Artifact relocatedPom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact
 238  
                             .getArtifactId(), artifact.getVersion() );
 239  0
                         targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
 240  
                     }
 241  
 
 242  0
                     Model v4Model = translator.translate( v3Model );
 243  
 
 244  0
                     translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model
 245  
                         .getVersion(), v3Model.getPackage() );
 246  
 
 247  0
                     writer = new StringWriter();
 248  0
                     MavenXpp3Writer Xpp3Writer = new MavenXpp3Writer();
 249  0
                     Xpp3Writer.write( writer, v4Model );
 250  
 
 251  0
                     transaction.createFile( writer.toString(), targetFile, digesters );
 252  
 
 253  0
                     List<String> warnings = translator.getWarnings();
 254  
 
 255  0
                     for ( String message : warnings )
 256  
                     {
 257  0
                         addWarning( artifact, message );
 258  
                     }
 259  
                 }
 260  0
                 catch ( XmlPullParserException e )
 261  
                 {
 262  0
                     addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
 263  0
                     result = false;
 264  
                 }
 265  0
                 catch ( IOException e )
 266  
                 {
 267  0
                     throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), e ); //$NON-NLS-1$
 268  
                 }
 269  0
                 catch ( PomTranslationException e )
 270  
                 {
 271  0
                     addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$
 272  0
                     result = false;
 273  
                 }
 274  
                 finally
 275  
                 {
 276  0
                     IOUtils.closeQuietly( writer );
 277  0
                 }
 278  
             }
 279  0
         }
 280  
         else
 281  
         {
 282  0
             addWarning( artifact, Messages.getString( "warning.missing.pom" ) ); //$NON-NLS-1$
 283  
         }
 284  0
         return result;
 285  
     }
 286  
 
 287  
     private boolean testChecksums( Artifact artifact, File file )
 288  
         throws IOException
 289  
     {
 290  0
         boolean result = true;
 291  0
         for ( Digester digester : digesters )
 292  
         {
 293  0
             result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$
 294  
                                       artifact, "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$
 295  
         }
 296  0
         return result;
 297  
     }
 298  
 
 299  
     private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key )
 300  
         throws IOException
 301  
     {
 302  0
         boolean result = true;
 303  
 
 304  0
         File checksumFile = new File( file.getParentFile(), fileName );
 305  0
         if ( checksumFile.exists() )
 306  
         {
 307  0
             String checksum = FileUtils.readFileToString( checksumFile, null );
 308  
             try
 309  
             {
 310  0
                 digester.verify( file, checksum );
 311  
             }
 312  0
             catch ( DigesterException e )
 313  
             {
 314  0
                 addWarning( artifact, Messages.getString( key ) );
 315  0
                 result = false;
 316  0
             }
 317  
         }
 318  0
         return result;
 319  
     }
 320  
 
 321  
     /**
 322  
      * File extension for checksums
 323  
      * TODO should be moved to plexus-digester ?
 324  
      */
 325  
     private String getDigesterFileExtension( Digester digester )
 326  
     {
 327  0
         return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$
 328  
     }
 329  
 
 330  
     private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
 331  
         throws ArtifactConversionException
 332  
     {
 333  0
         File sourceFile = artifact.getFile();
 334  
 
 335  0
         if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$
 336  
         {
 337  0
             artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$
 338  
         }
 339  
 
 340  0
         File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
 341  
 
 342  0
         boolean result = true;
 343  
         try
 344  
         {
 345  0
             boolean matching = false;
 346  0
             if ( !force && targetFile.exists() )
 347  
             {
 348  0
                 matching = FileUtils.contentEquals( sourceFile, targetFile );
 349  0
                 if ( !matching )
 350  
                 {
 351  0
                     addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$
 352  0
                     result = false;
 353  
                 }
 354  
             }
 355  0
             if ( result )
 356  
             {
 357  0
                 if ( force || !matching )
 358  
                 {
 359  0
                     if ( testChecksums( artifact, sourceFile ) )
 360  
                     {
 361  0
                         transaction.copyFile( sourceFile, targetFile, digesters );
 362  
                     }
 363  
                     else
 364  
                     {
 365  0
                         result = false;
 366  
                     }
 367  
                 }
 368  
             }
 369  
         }
 370  0
         catch ( IOException e )
 371  
         {
 372  0
             throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e ); //$NON-NLS-1$
 373  0
         }
 374  0
         return result;
 375  
     }
 376  
 
 377  
     private Metadata createBaseMetadata( Artifact artifact )
 378  
     {
 379  0
         Metadata metadata = new Metadata();
 380  0
         metadata.setArtifactId( artifact.getArtifactId() );
 381  0
         metadata.setGroupId( artifact.getGroupId() );
 382  0
         return metadata;
 383  
     }
 384  
 
 385  
     private Metadata readMetadata( File file )
 386  
         throws ArtifactConversionException
 387  
     {
 388  
         Metadata metadata;
 389  0
         MetadataXpp3Reader reader = new MetadataXpp3Reader();
 390  0
         FileReader fileReader = null;
 391  
         try
 392  
         {
 393  0
             fileReader = new FileReader( file );
 394  0
             metadata = reader.read( fileReader );
 395  
         }
 396  0
         catch ( FileNotFoundException e )
 397  
         {
 398  0
             throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
 399  
         }
 400  0
         catch ( IOException e )
 401  
         {
 402  0
             throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
 403  
         }
 404  0
         catch ( XmlPullParserException e )
 405  
         {
 406  0
             throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$
 407  
         }
 408  
         finally
 409  
         {
 410  0
             IOUtils.closeQuietly( fileReader );
 411  0
         }
 412  0
         return metadata;
 413  
     }
 414  
 
 415  
     private boolean validateMetadata( Artifact artifact )
 416  
         throws ArtifactConversionException
 417  
     {
 418  0
         ArtifactRepository repository = artifact.getRepository();
 419  
 
 420  0
         boolean result = true;
 421  
 
 422  0
         RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
 423  0
         File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
 424  0
         if ( file.exists() )
 425  
         {
 426  0
             Metadata metadata = readMetadata( file );
 427  0
             result = validateMetadata( metadata, repositoryMetadata, artifact );
 428  
         }
 429  
 
 430  0
         repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
 431  0
         file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
 432  0
         if ( file.exists() )
 433  
         {
 434  0
             Metadata metadata = readMetadata( file );
 435  0
             result = result && validateMetadata( metadata, repositoryMetadata, artifact );
 436  
         }
 437  
 
 438  0
         return result;
 439  
     }
 440  
 
 441  
     @SuppressWarnings("unchecked")
 442  
     private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
 443  
     {
 444  
         String groupIdKey;
 445  0
         String artifactIdKey = null;
 446  0
         String snapshotKey = null;
 447  0
         String versionKey = null;
 448  0
         String versionsKey = null;
 449  
 
 450  0
         if ( repositoryMetadata.storedInGroupDirectory() )
 451  
         {
 452  0
             groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
 453  
         }
 454  0
         else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
 455  
         {
 456  0
             groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
 457  0
             artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
 458  0
             versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
 459  0
             snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
 460  
         }
 461  
         else
 462  
         {
 463  0
             groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
 464  0
             artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
 465  0
             versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
 466  
         }
 467  
 
 468  0
         boolean result = true;
 469  
 
 470  0
         if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
 471  
         {
 472  0
             addWarning( artifact, Messages.getString( groupIdKey ) );
 473  0
             result = false;
 474  
         }
 475  0
         if ( !repositoryMetadata.storedInGroupDirectory() )
 476  
         {
 477  0
             if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
 478  
             {
 479  0
                 addWarning( artifact, Messages.getString( artifactIdKey ) );
 480  0
                 result = false;
 481  
             }
 482  0
             if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
 483  
             {
 484  
                 // artifact metadata
 485  
 
 486  0
                 boolean foundVersion = false;
 487  0
                 if ( metadata.getVersioning() != null )
 488  
                 {
 489  0
                     for ( String version : (List<String>) metadata.getVersioning().getVersions() )
 490  
                     {
 491  0
                         if ( version.equals( artifact.getBaseVersion() ) )
 492  
                         {
 493  0
                             foundVersion = true;
 494  0
                             break;
 495  
                         }
 496  
                     }
 497  
                 }
 498  
 
 499  0
                 if ( !foundVersion )
 500  
                 {
 501  0
                     addWarning( artifact, Messages.getString( versionsKey ) );
 502  0
                     result = false;
 503  
                 }
 504  0
             }
 505  
             else
 506  
             {
 507  
                 // snapshot metadata
 508  0
                 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
 509  
                 {
 510  0
                     addWarning( artifact, Messages.getString( versionKey ) );
 511  0
                     result = false;
 512  
                 }
 513  
 
 514  0
                 if ( artifact.isSnapshot() )
 515  
                 {
 516  0
                     Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
 517  0
                     if ( matcher.matches() )
 518  
                     {
 519  0
                         boolean correct = false;
 520  0
                         if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
 521  
                         {
 522  0
                             Snapshot snapshot = metadata.getVersioning().getSnapshot();
 523  0
                             int build = Integer.valueOf( matcher.group( 3 ) ).intValue();
 524  0
                             String ts = matcher.group( 2 );
 525  0
                             if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
 526  
                             {
 527  0
                                 correct = true;
 528  
                             }
 529  
                         }
 530  
 
 531  0
                         if ( !correct )
 532  
                         {
 533  0
                             addWarning( artifact, Messages.getString( snapshotKey ) );
 534  0
                             result = false;
 535  
                         }
 536  
                     }
 537  
                 }
 538  
             }
 539  
         }
 540  0
         return result;
 541  
     }
 542  
 
 543  
     private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
 544  
                                  Metadata newMetadata, FileTransaction transaction )
 545  
         throws ArtifactConversionException
 546  
     {
 547  0
         File file = new File( targetRepository.getBasedir(), targetRepository
 548  
             .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
 549  
 
 550  
         Metadata metadata;
 551  
         boolean changed;
 552  
 
 553  0
         if ( file.exists() )
 554  
         {
 555  0
             metadata = readMetadata( file );
 556  0
             changed = metadata.merge( newMetadata );
 557  
         }
 558  
         else
 559  
         {
 560  0
             changed = true;
 561  0
             metadata = newMetadata;
 562  
         }
 563  
 
 564  0
         if ( changed )
 565  
         {
 566  0
             StringWriter writer = null;
 567  
             try
 568  
             {
 569  0
                 writer = new StringWriter();
 570  
 
 571  0
                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
 572  
 
 573  0
                 mappingWriter.write( writer, metadata );
 574  
 
 575  0
                 transaction.createFile( writer.toString(), file, digesters );
 576  
             }
 577  0
             catch ( IOException e )
 578  
             {
 579  0
                 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ), e ); //$NON-NLS-1$
 580  
             }
 581  
             finally
 582  
             {
 583  0
                 IOUtils.closeQuietly( writer );
 584  0
             }
 585  
         }
 586  0
     }
 587  
 
 588  
     private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
 589  
                                   ArtifactRepository repository, FileTransaction transaction )
 590  
         throws IOException
 591  
     {
 592  0
         Properties properties = v3Model.getProperties();
 593  0
         if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" ) //$NON-NLS-1$ //$NON-NLS-2$
 594  
             || properties.containsKey( "relocated.version" ) ) //$NON-NLS-1$
 595  
         {
 596  0
             String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() ); //$NON-NLS-1$
 597  0
             properties.remove( "relocated.groupId" ); //$NON-NLS-1$
 598  
 
 599  0
             String newArtifactId = properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() ); //$NON-NLS-1$
 600  0
             properties.remove( "relocated.artifactId" ); //$NON-NLS-1$
 601  
 
 602  0
             String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() ); //$NON-NLS-1$
 603  0
             properties.remove( "relocated.version" ); //$NON-NLS-1$
 604  
 
 605  0
             String message = properties.getProperty( "relocated.message", "" ); //$NON-NLS-1$ //$NON-NLS-2$
 606  0
             properties.remove( "relocated.message" ); //$NON-NLS-1$
 607  
 
 608  0
             if ( properties.isEmpty() )
 609  
             {
 610  0
                 v3Model.setProperties( null );
 611  
             }
 612  
 
 613  0
             writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
 614  
                                 newArtifactId, newVersion, message, repository, transaction );
 615  
 
 616  0
             v3Model.setGroupId( newGroupId );
 617  0
             v3Model.setArtifactId( newArtifactId );
 618  0
             v3Model.setVersion( newVersion );
 619  
 
 620  0
             artifact.setGroupId( newGroupId );
 621  0
             artifact.setArtifactId( newArtifactId );
 622  0
             artifact.setVersion( newVersion );
 623  
 
 624  0
             return true;
 625  
         }
 626  
         else
 627  
         {
 628  0
             return false;
 629  
         }
 630  
     }
 631  
 
 632  
     private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
 633  
                                      String newArtifactId, String newVersion, String message,
 634  
                                      ArtifactRepository repository, FileTransaction transaction )
 635  
         throws IOException
 636  
     {
 637  0
         Model pom = new Model();
 638  0
         pom.setGroupId( groupId );
 639  0
         pom.setArtifactId( artifactId );
 640  0
         pom.setVersion( version );
 641  
 
 642  0
         DistributionManagement dMngt = new DistributionManagement();
 643  
 
 644  0
         Relocation relocation = new Relocation();
 645  0
         relocation.setGroupId( newGroupId );
 646  0
         relocation.setArtifactId( newArtifactId );
 647  0
         relocation.setVersion( newVersion );
 648  0
         if ( message != null && message.length() > 0 )
 649  
         {
 650  0
             relocation.setMessage( message );
 651  
         }
 652  
 
 653  0
         dMngt.setRelocation( relocation );
 654  
 
 655  0
         pom.setDistributionManagement( dMngt );
 656  
 
 657  0
         Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$
 658  0
         File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) );
 659  
 
 660  0
         StringWriter strWriter = new StringWriter();
 661  0
         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
 662  0
         pomWriter.write( strWriter, pom );
 663  
 
 664  0
         transaction.createFile( strWriter.toString(), pomFile, digesters );
 665  0
     }
 666  
 
 667  
     private void addWarning( Artifact artifact, String message )
 668  
     {
 669  0
         List<String> messages = warnings.get( artifact );
 670  0
         if ( messages == null )
 671  
         {
 672  0
             messages = new ArrayList<String>();
 673  
         }
 674  0
         messages.add( message );
 675  0
         warnings.put( artifact, messages );
 676  0
     }
 677  
 
 678  
     public void clearWarnings()
 679  
     {
 680  0
         warnings.clear();
 681  0
     }
 682  
 
 683  
     public Map<Artifact, List<String>> getWarnings()
 684  
     {
 685  0
         return warnings;
 686  
     }
 687  
 }