Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local repository location from Maven 3.0 plugin?

How to get local repository location (URI) from within Maven 3.x plugin?

like image 461
yegor256 Avatar asked Sep 15 '11 10:09

yegor256


People also ask

What is local repository in Maven?

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.


2 Answers

This one worked for me in Maven v3.6.0:

@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository localRepository;
like image 106
JodaStephen Avatar answered Sep 20 '22 15:09

JodaStephen


@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

like image 30
Austin Poole Avatar answered Sep 20 '22 15:09

Austin Poole