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 27KB

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