How to get local repository location from Maven 3.0 plugin?

痴心易碎 提交于 2019-12-06 23:55:22

问题


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


回答1:


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.




回答2:


This one worked for me in Maven v3.6.0:

@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository localRepository;



回答3:


@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




回答4:


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.



来源:https://stackoverflow.com/questions/7429617/how-to-get-local-repository-location-from-maven-3-0-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!