You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyToDefaultConverter.java 28KB

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