将 maven-build-classpath 添加到插件执行类路径
I am writing some code-gen maven-plugin.
I need my project classpath be injected in to my plugin execution classpath.
I found this article. The solution there works but is quite long. Maybe someone of you know an out of the box solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了 答案!
好吧,帕斯卡是对的,这是基础!!
所以这是将编译类路径添加到插件执行中的最干净的方法(据我所知)。
以下是我的 code-gen 插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码。所以我需要首先编译代码,然后分析,生成一些代码,然后再次编译。
在Mojo类中使用
@configurator
:<前><代码>/**
* @目标生成
* @phase过程类
* @configurator 包含项目依赖项
* @requiresDependencyResolution 编译+运行时
*/
公共类 CodeGenMojo
扩展 AbstractMojo
{
公共无效执行()
抛出 MojoExecutionException
{
// 做工作....
}
}
请注意javadoc头中的
@configurator
行,它对于plexus IOC容器来说是必需的,而不仅仅是另一个注释行。include-project-dependencies
配置器的实现。我从 Brian Jackson 那里得到了一个非常好的课程,将其添加到您的插件的源代码中。<前><代码>/**
* 自定义 ComponentConfigurator 添加项目的运行时类路径元素
* 到
*
* @作者布莱恩·杰克逊
* @自 2008 年 8 月 1 日下午 3:04:17 起
*
* @plexus.component角色=“org.codehaus.plexus.component.configurator.ComponentConfigurator”
* 角色提示=“包含项目依赖项”
* @plexus.requirement角色=“org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup”
* 角色提示=“默认”
*/
公共类 IncludeProjectDependencyComponentConfigurator 扩展 AbstractComponentConfigurator {
私有静态最终 Logger LOGGER = Logger.getLogger(InincludeProjectDependencyComponentConfigurator.class);
public void configureComponent( 对象组件, PlexusConfiguration 配置,
ExpressionEvaluator 表达式Evaluator、ClassRealm 容器Realm、
ConfigurationListener监听器)
抛出 ComponentConfigurationException {
addProjectDependencyToClassRealm(表达式Evaluator,containerRealm);
converterLookup.registerConverter(新的ClassRealmConverter(containerRealm));
ObjectWithFieldsConverter 转换器 = new ObjectWithFieldsConverter();
converter.processConfiguration(converterLookup,组件,containerRealm.getClassLoader(),配置,
表达式评估器、监听器);
}
私人无效addProjectDependencyToClassRealm(ExpressionEvaluator表达式Evaluator,ClassRealm容器Realm)抛出ComponentConfigurationException {) expressionEvaluator.evaluate("${project.runtimeClasspathElements}");
列表<字符串>运行时类路径元素;
尝试 {
//noinspection 未选中
runtimeClasspathElements = (List
} catch (ExpressionEvaluationException e) {
throw new ComponentConfigurationException("评估时出现问题:${project.runtimeClasspathElements}", e);
}
// 将项目依赖添加到ClassRealm中
最终 URL[] urls = buildURLs(runtimeClasspathElements);
for (URL url : urls) {
containerRealm.addConstituent(url);
}
}
私有 URL[] buildURLs(ListruntimeClasspathElements) 抛出 ComponentConfigurationException {urls = new ArrayList(runtimeClasspathElements.size());
// 添加项目类和依赖项
列表
for (字符串元素:runtimeClasspathElements) {
尝试 {
最终 URL url = new File(element).toURI().toURL();
urls.add(url);
如果(LOGGER.isDebugEnabled()){
LOGGER.debug("已添加到项目类加载器:" + url);
}
} catch (MalformedURLException e) {
throw new ComponentConfigurationException("无法访问项目依赖项:" + element, e);
}
}
// 添加插件的依赖项(这样如果 Trove 没有打开,Trove 的东西就可以工作)
return urls.toArray(new URL[urls.size()]);
}
}
这是我的插件的构建部分,您必须添加它。
这里是插件的pom.xml,供需要的人使用。现在编译应该没有问题了。 (标题有问题,所以忽略它)
Found the answer!
OK , Pascal is right , here it is for the foundation!!
So here is the cleanest way ( as far as i know ) to add the compile classpath to the execution of you plugin.
Here are some code samples from my code-gen plugin, that is actually generating some template code based on the code compiled. So I needed first the code compiled, then analyzed, generate some code, and then compiled again.
Use
@configurator
in the Mojo class:Please pay attention to the
@configurator
line in the javadoc header, it is essetial for the plexus IOC container and is not just another comment line.The implementation of the
include-project-dependencies
configurator. There is this very nice class that I took from some Brian Jackson, add it to the source of your plugin.Here is the build part of my plugin that you will have to add.
Here is the pom.xml of the plugin for these who need it. Should compile wihtout a problem now. ( something wrong with the header, so ignore it )
我采用了这种方法,显然它有效:
1 - Mojo 类中需要 MavenProject 参数:
2 - 然后您可以从项目实例中获取类路径元素:
I took this approach and apparently it's working:
1 - a MavenProject parameter is needed within your Mojo class:
2 - and then you can get the classpath elements from project instance:
按照此链接...对我的代码做了非常相似的事情...我创建了一个 maven-fit-plugin
我为我的插件使用了完全相同的 pom.xml,重用了 IncludeProjectDependencyComponentConfigurator
我正在使用 maven 2.2.0
如果有帮助的话,这里是 pom 再次
希望这对
rgds 有帮助
马可
followed this link.... did very similar thing for my code... i have created a maven-fit-plugin
I have used exactly the same pom.xml for my plugin, reusing the IncludeProjectDependenciesComponentConfigurator
I am using maven 2.2.0
if it can help, here's pom again
hope this helps
rgds
marco