Maven 插件加载类

发布于 2024-12-10 17:39:20 字数 261 浏览 0 评论 0原文

我有一个应用程序,它具有扩展 org.apache.struts.StrutsActions 的遗留 Struts 操作。我想确保扩展 StrutsActions 的所有类都有自定义注释。

为了提供这一点,我编写了一个小的 Maven 执行器规则来验证我的要求。但是我不知道如何在我的魔力中加载我的类来验证它们。

实际上,我做了一些并不奇特的事情,那就是注入outputDirectory,并且使用自定义类加载器,我已经递归地加载了构建文件夹中的所有类。

谢谢

I have an application which has legacy Struts actions extending org.apache.struts.StrutsActions. I would like to be sure that all my classes which are extending StrutsActions have a custom annotation.

In order to provide this I have written a small maven enforcer rule to validate my requirement. However I dont know how to load my classes at my mojo to validate them.

Actually I have done something not fancy which is injection outputDirectory and with a custom class loader I have recursively loaded all classes at my build folder.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-12-17 17:39:20

所有课程?你这是什么意思?也许您的意思是 target/classes/** (这是类的默认输出位置)或者您的意思是多个目录树位置的列表?

您能否更好地解释一下您的 Mojo 的作用以及您希望它与哪个阶段和目标绑定。

我想也许您正在考虑如何错误地将 Maven 的构建周期应用到您的项目中。您能否更好地解释一下您的插件的作用,也许它是“打包”工作?

但是如果我理解正确的话,您希望插件的执行为 target/classes/** 拾取额外的类路径条目?那么它可以从项目本身加载代码和资源来更改 maven-plugin 内的某些动态行为吗?

执行此操作的默认方法是,但是当然这需要固定的单位。

其他允许这种行为的插件(如 maven-antrun-plugin)提供了一种机制来更改 Mojo 内的类路径,并使用 pom.xml 的 部分中的内容来完成此操作。不清楚您使用的插件是 Maven 插件还是您自己编写的插件?

验证和打包是一个有效的用例。但我质疑为什么在“类路径”上?我猜你正在绑定进程类阶段。

即类路径的目的是向运行时提供代码/资源来执行。但在您的情况下,您有一个输入目录而不是类路径要求。

在 Mojo 中,可以在输入目录 */.class 上设置目录扫描器,然后可以(使用某些库)打开每个文件并检查注释而不加载它。

这也是不可靠的输入数据和插件代码本身的一致行为之间的一种很好的分离。如果项目决定要实现与插件本身实现中使用的相同的包和/或类,会发生什么。

更新:如果您确实要从 Mojo 加载要签入 JVM 的类,那么至少要实现您自己的 ClassLoader 来执行此操作。这不一定是一个容易解决的问题。您使该类加载器在输入目录中查找配置中指定的内容。

All classes ? What do you mean by that ? Maybe you mean target/classes/** (which is the default output place for classes) or maybe you mean a list of multiple directory tree locations ?

Can you better explain what your Mojo does and which phase and goals you want it to bind with.

I think maybe you are thinking about how to apply Maven's build cycle to your project incorrectly. Could you explain better what your plugin does, maybe it is does "packaging" work ?

But if I understand you correctly you want the plugin's execution to pickup the additional classpath entry for target/classes/** ? So it can load code and resources from the project itself to change some dynamic behaviour inside the maven-plugin ?

The default way to do this is <dependency> however of course this requires a fixed unit.

Other plugins that allow for this behavior (like maven-antrun-plugin) provide a mechansism to change classpath inside the Mojo and use something from <configuration> section of their pom.xml to do it. It is not clear if the plugin you are using is a Maven one or one you have written ?

.

Validation and packaging that is a valid use case. But I question why on the "classpath" ? I would guess you are binding on process-classes phase.

i.e. the classpath is for the purpose of providing code/resources to the runtime to execute. But in your case you have an input directory rather than a class path requirement.

It is possible in a Mojo to setup a directory scanner on an input directory */.class and then it is possible to (using some library) open each file and inspect the annotation without loading it.

This is also a good kind of separation between unreliable input data and the consistent behaviour of the plugin code itself. What happens if a project decides it wants to implement the same package and/or class as used in the implementation of the plugin itself.

UPDATE: If you really are loading the classes you are checkig into the JVM from your Mojo, then at least implement your own ClassLoader to do it. This is not necessarily a simple problem to solve. You make this ClassLoader find things specified from configuration in the input directory.

人海汹涌 2024-12-17 17:39:20

我在 reflections 的帮助下完成了它,

  <dependency>
     <groupId>org.reflections</groupId>
     <artifactId>reflections</artifactId>
     <version>0.9.5</version>
  </dependency>

我的实现如下:

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {

    URL url = getURL(helper.evaluate("${project.build.outputDirectory}"));

    Predicate<String> filter = new FilterBuilder().include(getTargetSuperTypeForSubtypes());
    Predicate<String> filter2 = new FilterBuilder().include(getMustHaveAnnotation());

    Reflections reflections = new Reflections(new ConfigurationBuilder()
     .setScanners(
        new TypeAnnotationsScanner().filterResultsBy(filter2),
        new SubTypesScanner().filterResultsBy(filter))
     .setUrls(url));

    validate(reflections);
}

I have done it with the help of reflections

  <dependency>
     <groupId>org.reflections</groupId>
     <artifactId>reflections</artifactId>
     <version>0.9.5</version>
  </dependency>

my implementation is like this:

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {

    URL url = getURL(helper.evaluate("${project.build.outputDirectory}"));

    Predicate<String> filter = new FilterBuilder().include(getTargetSuperTypeForSubtypes());
    Predicate<String> filter2 = new FilterBuilder().include(getMustHaveAnnotation());

    Reflections reflections = new Reflections(new ConfigurationBuilder()
     .setScanners(
        new TypeAnnotationsScanner().filterResultsBy(filter2),
        new SubTypesScanner().filterResultsBy(filter))
     .setUrls(url));

    validate(reflections);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文