【发布时间】:2019-12-19 10:44:36
【问题描述】:
我有一个在进程类阶段运行的自定义 maven 插件。它需要加载和反映构建生成的类文件,但这意味着在插件设置中显式地重新声明所有依赖项。
插件是否可以继承构建依赖项?
一些进一步的背景:
有问题的插件处理编译阶段生成的类,用不同的语言为每个类输出一个等效的表示形式(即它基本上是一个 java 转译器)。
为了达到这个目的,插件需要对进程类进行反射。只要插件与类本身具有相同的依赖关系,这就可以正常工作。否则,当类或类方法加载并引用插件类路径中未包含的类型时,您会遇到反射错误(请注意,由于某些第三方问题,仅使用 ASM 之类的工具检查类文件二进制文件是不够的)。
示例 POM:
<project>
<!-- ...boilerplate... -->
<dependencies>
<dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>myplugins</groupId>
<artifactId>my-custom-plugin-that-processes-classes</artifactId>
<!-- To process classes generated by the main build, I need to
duplicate dependencies or I'll get reflection issues.
For example if a class in my code 'X' uses a static
SLF4J logger, I will get a clinit error when I load
X for processing unless I include this. -->
<dependencies>
<dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
请详细说明您要解决什么样的问题...最好是 POM 文件和类等示例。
-
如果你得到反射错误意味着你没有走正确的路,因为与项目依赖分开的插件依赖之间存在差异......没有代码插件我无能为力这里..
标签: maven maven-plugin