简体   繁体   English

在 Maven 插件中,如何从依赖项访问文件?

[英]In a Maven plugin, how can I access files from a dependency?

Background: I am trying to hook the compiler for a domain-specific language into Maven.背景:我正在尝试将特定领域语言的编译器挂接到 Maven 中。 The DSL is compiled to Java source code, so my idea was to write a Mojo that gets executed in the generate-sources phase. DSL 被编译为 Java 源代码,所以我的想法是编写一个在生成源阶段执行的 Mojo。 The compiler is currently implemented as a Gradle plugin and cannot handle multiple projects with dependencies, and while trying to support projects, I hit problems in Gradle.编译器当前实现为 Gradle 插件,无法处理具有依赖关系的多个项目,并且在尝试支持项目时,我在 Gradle 中遇到了问题。 I'm now trying to understand if Maven handles that situation in a better way.我现在试图了解 Maven 是否以更好的方式处理这种情况。 The problems with Gradle are irrelevant to this question. Gradle 的问题与这个问题无关。

My DSL has packages like Java that get mapped to identical Java packages.我的 DSL 有像 Java 这样的包,它们映射到相同的 Java 包。 The same should be true for projects (called "modules", AFAIK, if they are part of a reactor build).对于项目(称为“模块”,AFAIK,如果它们是反应堆构建的一部分)也应该如此。 In that case, for each project, the DSL sources get compiled to Java source code, as well as meta-data (a JSON file per compiled class).在这种情况下,对于每个项目,DSL 源都被编译为 Java 源代码以及元数据(每个编译类的 JSON 文件)。 When project A depends on B, the DSL compilation process for A needs the meta-data files from B. That meta-data should be packaged as resources into the JAR file together with the generated and compiled Java code, as well as possibly hand-written and compiled Java code.当项目 A 依赖于 B 时,A 的 DSL 编译过程需要来自 B 的元数据文件。该元数据应作为资源打包到 JAR 文件中,连同生成和编译的 Java 代码,以及可能的手编写并编译Java代码。

In Maven, I can get the current MavenProject injected into the Mojo.在 Maven 中,我可以将当前的 MavenProject 注入到 Mojo 中。 I can ask the MavenProject for its dependencies, but I only seem to get meta-data like the Maven coordinates for the dependencies.我可以向 MavenProject 询问它的依赖关系,但我似乎只获得像 Maven 坐标这样的依赖关系的元数据。 I do not understand how to access files from the dependencies.我不明白如何从依赖项中访问文件。 What is necessary to do that?这样做有什么必要?

I also do not understand yet if I have to make a distinction whether the dependency is a JAR from an artifact repository or another module in a reactor build.我也不明白是否必须区分依赖项是来自工件存储库的 JAR 还是反应器构建中的另一个模块。 The reason for this is that, apparently, in a reactor build, it is not required for the dependency to go through the full Maven lifecycle up to "install": Suppose that project A depends on project B, and I run "mvn generate-sources" in the parent.原因是,显然,在反应堆构建中,通过完整的 Maven 生命周期直到“安装”,对 go 的依赖不是必需的:假设项目 A 依赖于项目 B,我运行“mvn generate-来源”在父级。 Then the DSL compiler runs in project B, and later it runs in project A. However, the build process for B has stopped after generate-sources, so to the DSL compiler for A, B looks very different from an artifact from a repository -- starting with the fact that there is no JAR file.然后 DSL 编译器在项目 B 中运行,然后在项目 A 中运行。但是,在 generate-sources 之后,B 的构建过程已经停止,因此对于 A 的 DSL 编译器,B 看起来与存储库中的工件非常不同 - - 从没有 JAR 文件的事实开始。 Is it correct that I have to make this distinction (between reactor dependencies and repository dependencies)?我必须做出这种区分(反应器依赖项和存储库依赖项之间)是否正确? If so, how do I get the necessary information?如果是这样,我如何获得必要的信息? Are there any best practices on how to do that?有没有关于如何做到这一点的最佳实践?

I managed to access dependencies by having a "dependency resolver" injected into my Mojo (which is a thin wrapper around RepositorySystem), as well as a session for the latter:我设法通过将“依赖解析器”注入到我的 Mojo(它是 RepositorySystem 的薄包装器)以及后者的 session 来访问依赖关系:

@Component
private ProjectDependenciesResolver projectDependenciesResolver;

@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repoSession;

I can use those to obtain dependencies:我可以使用它们来获取依赖项:

DefaultDependencyResolutionRequest request = new DefaultDependencyResolutionRequest(project, repoSession);
DependencyResolutionResult result = projectDependenciesResolver.resolve(request);

The result contains the dependencies as File objects which can point to a JAR in the.m2 folder (for repository dependencies), a JAR file that was just built (for reactor dependencies when building towards a sufficiently "high" phase), a folder full of class files (for reactor dependencies when building towards a "lower" phase where JAR packaging did not yet happen), or not exist at all (for reactor dependencies when building towards a "really low" phase).结果包含作为文件对象的依赖项,它可以指向 .m2 文件夹中的 JAR(用于存储库依赖项),一个刚刚构建的 JAR 文件(用于构建足够“高”阶段时的反应堆依赖项),一个文件夹已满class 文件的数量(用于构建“较低”阶段时的反应器依赖关系,其中 JAR 打包尚未发生),或根本不存在(用于构建“非常低”阶段时的反应器依赖关系)。

This is sufficient for me.这对我来说已经足够了。 I'll have to make a distinction based on whether I get a JAR file, folder, or nothing at all (should be pretty simple), and I have to deal with the fact that when building towards phase X, reactor dependencies will also build only up to X, so files that I need might be missing.我必须根据我是否获得 JAR 文件、文件夹或什么都没有(应该很简单)来区分,并且我必须处理这样一个事实,即在向阶段 X 构建时,反应堆依赖项也会构建最多只有 X,所以我需要的文件可能会丢失。

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

相关问题 如何从GWT中的外部Maven依赖项访问类? - How can I access a class from a external maven dependency in GWT? 如何使用Maven插件向嵌入式tomcat添加依赖项? - How can I add a dependency to embedded tomcat using the maven plugin? 如何使用Maven依赖插件下载ZIP文件? - How can I download a ZIP file with the Maven dependency plugin? 如何从maven-dependency-plugin:unpack的输出目录中剥离版本? - How can I strip the version from the output directory of maven-dependency-plugin:unpack? 如何在maven-antrun-plugin中使用插件的依赖项范围中的依赖项? - How can you use a dependency from the plugin's dependency scope in the maven-antrun-plugin? 如何从 Maven 依赖项中获取 class 文件到 target\classes 文件夹 - How can I get class files from Maven dependency into the target\classes folder Maven依赖插件-使用依赖解压缩时如何确保工件存在 - Maven dependency plugin - How can I ensure that an artifact is present when using dependency-unpack 如何从Maven插件更改默认依赖项 - how to change default dependency from maven plugin 如何从Maven插件访问Maven占位符? - How to access Maven placeholders from a Maven Plugin? 如何排除来自Maven插件的Maven可传递依赖项? - How do I exclude a Maven transitive dependency that comes from a Maven plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM