6

How do I resolve an artifact's path in the local repository in Maven 3?

In Maven 2 you could use ArtifactResolver to populate an Artifact object with the relevant information, but this class has been deprecated in Maven 3.

Community
  • 1
  • 1
Gili
  • 86,244
  • 97
  • 390
  • 689

2 Answers2

4

The best replacement of ArtifactFactory (also deprecated with M3) is RepositorySystem. createDependencyArtifact,... operations are available.

fdaugan
  • 747
  • 1
  • 5
  • 17
  • One method that's missing from `RepositorySystem` is `createParentArtifact`, but internally it simply calls `createProjectArtifact`, so use that instead. – Andrew Swan Nov 18 '20 at 04:32
2

Answering my own question:

/**
 * @component
 */
private ArtifactFactory artifactFactory;
/**
 * The local maven repository.
 *
 * @parameter expression="${localRepository}"
 * @required
 * @readonly
 */
private ArtifactRepository localRepository;
[...]
Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
artifact.setFile(new File(localRepository.getBasedir(), localRepository.pathOf(artifact)));
Gili
  • 86,244
  • 97
  • 390
  • 689
  • 1
    What about RepositorySystem and RepositorySystemSession? – Asaf Mesika Jul 05 '11 at 10:49
  • You're right. RepositorySystem looks like a drop-in replacement for ArtifactRepository. – Gili Sep 04 '11 at 04:43
  • 1
    Bear in mind that you can continue to use ArtifactResolver if you need your plugin to be compatible with Maven 2. – Brett Porter Sep 05 '11 at 01:14
  • Brett, how is it possible ? I have ald project created using maven-core-2.0.9 now I have migrate this plugin to maven3. So I use maven-core-3.0 – Andrew Niken Apr 19 '16 at 13:11
  • Brett, how is it possible ? I have ald project created using maven-core-2.0.9 now I have migrate this plugin to maven3. So I use maven-core-3.0 But ArtifactResolver located in the 2.0.9 version only. – Andrew Niken Apr 19 '16 at 13:23