10

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

4

4 回答 4

6

按照此博客文章中的说明使用 Aether 。

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

现在通过以下方式获取本地回购RepositorySystemSession.getLocalRepository()

LocalRepository localRepo = repoSession.getLocalRepository();

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

于 2011-09-15T11:01:16.140 回答
1

@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.XX 中测试

于 2018-03-29T14:08:44.453 回答
1

这个在 Maven v3.6.0 中为我工作:

@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository localRepository;
于 2019-02-27T17:53:57.843 回答
0

您可以简单地从设置中获取本地存储库位置:

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

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

  ...
}

它适用于 maven-3x。

于 2019-02-14T17:47:31.297 回答