从Maven插件访问项目类 [英] Access project classes from a maven plugin

查看:362
本文介绍了从Maven插件访问项目类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个在测试阶段运行的Maven插件,在使用我的插件的项目的pom.xml配置中,我设置了一个类规范名称,我想使用该类规范名称从我的插件中运行该类,基本上,我正在通过插件从项目内部动态加载类.

I'm making a maven plugin that run in test phase, in pom.xml configuration of a project that uses my plugin, I'm setting a class canonical name that I want to use to run that class from my plugin, basically I'm making a way to have a dynamic class loading of classes inside the project from my plugin.

Class clazz = Class.forName("... class from pom.xml ...")

当我运行它时,会收到预期的"ClassNotFoundException",似乎类加载器不相同或未共享.

When I run it I receive the expectable "ClassNotFoundException", seems the class loader are not the same or not shared.

有办法做到吗?像是从项目中捕获类加载器,还是通过将依赖项注入到我的插件中来接收它?最好的方法是什么?

There is a way to do it? Like capture the class loader from the project or receive it by dependency injection into my plugin? What is the best way?

推荐答案

我们可以在mojo中使用Hibernate实现,作为实现它的参考:

We can use the Hibernate implementation in mojo can be used as a reference to make it:

在此处签出源代码:

要捕获项目"对象,我们可以使用mojo依赖项注入:

To capture the "project" object, we can use the mojo dependency injection:

/**
 * Dependency injected
 */
 @Parameter(defaultValue = "${project}")
 public MavenProject project;

并使用它在项目类加载器中加载某些类:

And use it to load some class in project class loader:

getClassLoader(this.project).loadClass("com.somepackage.SomeClass")

这篇关于从Maven插件访问项目类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆