How to get local repository location (URI) from within Maven 3.x plugin?
A repository in Maven holds build artifacts and dependencies of varying types. There are exactly two types of repositories: local and remote: the local repository is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.
This one worked for me in Maven v3.6.0:
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository localRepository;
@Sean Patrick Floyd provided a solid answer.
This solution doesn't require the injection of the Properties into your instance fields.
@Override
public void execute() throws MojoExecutionException {
MavenProject project=(MavenProject)getPluginContext().get("project");
Set<Artifact> arts=project.getDependencyArtifacts();
Set<String> localRepoSet = new HashSet<>();
for (Artifact art : arts) {
if (art.getScope().equals(Artifact.SCOPE_COMPILE)) {
Path path = Paths.get(art.getFile().getAbsolutePath());
String removal = art.getGroupId().replace(".", "/") + "/" + art.getArtifactId() + "/"
+ art.getVersion();
String localRepo = path.getParent().toAbsolutePath().toString().replace(removal, "");
localRepoSet.add(localRepo);
}
}
}
You can get the possible locations of all of your direct dependencies.
Tested in Maven 3.X.X
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With