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

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