简体   繁体   English

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

[英]How to get local repository location from Maven 3.0 plugin?

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

Use Aether as described in this blog post . 使用此博客文章中描述的Aether。

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

now get the local Repo through RepositorySystemSession.getLocalRepository() : 现在通过RepositorySystemSession.getLocalRepository()获取本地Repo:

LocalRepository localRepo = repoSession.getLocalRepository();

LocalRepository has a getBasedir() method, which is probably what you want. LocalRepository有一个getBasedir()方法,这可能就是你想要的。

@Sean Patrick Floyd provided a solid answer. @Sean Patrick Floyd提供了一个可靠的答案。

This solution doesn't require the injection of the Properties into your instance fields. 此解决方案不需要将Properties注入您的实例字段。

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

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. 它适用于maven-3x。

This one worked for me in Maven v3.6.0: 这个在Maven v3.6.0中为我工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM