如何从Maven 3.0插件获取本地存储库位置?

时间:2011-09-15 10:49:32

标签: java maven maven-3 aether

如何从Maven 3.x插件中获取本地存储库位置(URI)?

4 个答案:

答案 0 :(得分:6)

this blog post

中所述使用Aether
/**
 * The current repository/network configuration of Maven.
 *
 * @parameter default-value="${repositorySystemSession}"
 * @readonly
 */
private RepositorySystemSession repoSession;

现在通过RepositorySystemSession.getLocalRepository()获取本地回购:

LocalRepository localRepo = repoSession.getLocalRepository();

LocalRepository有一个getBasedir()方法,可能就是你想要的。

答案 1 :(得分:0)

@Sean Patrick Floyd提供了一个可靠的答案。

此解决方案不需要将属性注入您的实例字段。

@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);
        }
    }
}

您可以获取所有直接依赖项的可能位置。

在Maven 3.X.X中测试

答案 2 :(得分:0)

您只需从设置中获取本地存储库位置即可:

@Parameter( defaultValue = "${settings}", readonly = true )
private Settings settings;

public void execute() throws MojoExecutionException {
  final File localRepository = new File(settings.getLocalRepository());

  ...
}

它在maven-3x中工作。

答案 3 :(得分:0)

此版本在Maven v3.6.0中为我工作:

pp.decimal