How to get local repository location (URI) from within Maven 3.x plugin?
Asked
Active
Viewed 4,228 times
4 Answers
6
Use Aether as described in this blog post.
/**
* The current repository/network configuration of Maven.
*
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession repoSession;
now get the local Repo through RepositorySystemSession.getLocalRepository()
:
LocalRepository localRepo = repoSession.getLocalRepository();
LocalRepository
has a getBasedir()
method, which is probably what you want.

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
-
@DmytroChyzhykov no idea, you should ask a separate question – Sean Patrick Floyd Jun 17 '13 at 14:36
-
at least with Maven 3.6.* it does not work with those JavaDoc annotations anymore, but with the `@Parameter` annotation from this answer: https://stackoverflow.com/a/54911656/1915920 – Andreas Covidiot Mar 07 '22 at 23:19
2
This one worked for me in Maven v3.6.0:
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository localRepository;

JodaStephen
- 60,927
- 15
- 95
- 117
1
@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

Austin Poole
- 652
- 6
- 11
1
You can simply get local repository location from settings:
@Parameter( defaultValue = "${settings}", readonly = true )
private Settings settings;
public void execute() throws MojoExecutionException {
final File localRepository = new File(settings.getLocalRepository());
...
}
It works in maven-3x.

svaor
- 2,205
- 2
- 19
- 41