Maven 2 将maven构建类路径添加到插件执行类路径
我正在写一些代码GenMaven插件 我需要将我的项目类路径注入插件执行类路径 我找到了这个。那里的解决方案可行,但相当长。也许你们中有人知道一个现成的解决方案。找到了 好的,Pascal是对的,这是基础!! 因此,这里是将编译类路径添加到插件执行中的最干净的方法(据我所知) 下面是我的CodeGen插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码。所以我需要先编译代码,然后分析,生成一些代码,然后再编译Maven 2 将maven构建类路径添加到插件执行类路径,maven-2,maven-plugin,Maven 2,Maven Plugin,我正在写一些代码GenMaven插件 我需要将我的项目类路径注入插件执行类路径 我找到了这个。那里的解决方案可行,但相当长。也许你们中有人知道一个现成的解决方案。找到了 好的,Pascal是对的,这是基础!! 因此,这里是将编译类路径添加到插件执行中的最干净的方法(据我所知) 下面是我的CodeGen插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码。所以我需要先编译代码,然后分析,生成一些代码,然后再编译 在Mojo类中使用@configurator: /** * @goal g
@configurator
:
/**
* @goal generate
* @phase process-classes
* @configurator include-project-dependencies
* @requiresDependencyResolution compile+runtime
*/
public class CodeGenMojo
extends AbstractMojo
{
public void execute()
throws MojoExecutionException
{
// do work....
}
}
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
请注意javadoc标题中的@configurator
行,它对于plexus IOC容器非常重要,而不仅仅是另一个注释行的实现包括项目依赖关系
配置程序。我从一些Brian Jackson那里得到了一个非常好的类,将它添加到插件的源代码中
/**
* A custom ComponentConfigurator which adds the project's runtime classpath elements
* to the
*
* @author Brian Jackson
* @since Aug 1, 2008 3:04:17 PM
*
* @plexus.component role="org.codehaus.plexus.component.configurator.ComponentConfigurator"
* role-hint="include-project-dependencies"
* @plexus.requirement role="org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup"
* role-hint="default"
*/
public class IncludeProjectDependenciesComponentConfigurator extends AbstractComponentConfigurator {
private static final Logger LOGGER = Logger.getLogger(IncludeProjectDependenciesComponentConfigurator.class);
public void configureComponent( Object component, PlexusConfiguration configuration,
ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm,
ConfigurationListener listener )
throws ComponentConfigurationException {
addProjectDependenciesToClassRealm(expressionEvaluator, containerRealm);
converterLookup.registerConverter( new ClassRealmConverter( containerRealm ) );
ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
converter.processConfiguration( converterLookup, component, containerRealm.getClassLoader(), configuration,
expressionEvaluator, listener );
}
private void addProjectDependenciesToClassRealm(ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm) throws ComponentConfigurationException {
List<String> runtimeClasspathElements;
try {
//noinspection unchecked
runtimeClasspathElements = (List<String>) expressionEvaluator.evaluate("${project.runtimeClasspathElements}");
} catch (ExpressionEvaluationException e) {
throw new ComponentConfigurationException("There was a problem evaluating: ${project.runtimeClasspathElements}", e);
}
// Add the project dependencies to the ClassRealm
final URL[] urls = buildURLs(runtimeClasspathElements);
for (URL url : urls) {
containerRealm.addConstituent(url);
}
}
private URL[] buildURLs(List<String> runtimeClasspathElements) throws ComponentConfigurationException {
// Add the projects classes and dependencies
List<URL> urls = new ArrayList<URL>(runtimeClasspathElements.size());
for (String element : runtimeClasspathElements) {
try {
final URL url = new File(element).toURI().toURL();
urls.add(url);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Added to project class loader: " + url);
}
} catch (MalformedURLException e) {
throw new ComponentConfigurationException("Unable to access project dependency: " + element, e);
}
}
// Add the plugin's dependencies (so Trove stuff works if Trove isn't on
return urls.toArray(new URL[urls.size()]);
}
}
/**
*添加项目运行时类路径元素的自定义ComponentConfigurator
*到
*
*@作者布莱恩·杰克逊
*@自2008年8月1日起下午3:04:17
*
*@plexus.component role=“org.codehaus.plexus.component.configurator.ComponentConfigurator”
*role hint=“包括项目依赖项”
*@plexus.requirement role=“org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup”
*role hint=“默认值”
*/
公共类IncludeProjectDependenciesComponentConfiguration扩展了AbstractComponentConfigurator{
私有静态最终记录器Logger=Logger.getLogger(包括projectdependenciescomponentconfigurator.class);
public void配置组件(对象组件、丛配置配置、,
ExpressionEvaluator ExpressionEvaluator,ClassRealm containerRealm,
配置侦听器(侦听器)
抛出ComponentConfigurationException{
addProjectDependenciesToClassRealm(expressionEvaluator,containerRealm);
registerConverter(新类realmconverter(containerRealm));
ObjectWithFieldsConverter converter=新建ObjectWithFieldsConverter();
processConfiguration(converterLookup,组件,containerRealm.getClassLoader(),配置,
表达评价者、听众);
}
私有void addProjectDependenciesToClassRealm(ExpressionEvaluator ExpressionEvaluator,ClassRealm containerRealm)引发ComponentConfigurationException{
列出runtimeClasspathElements;
试一试{
//未检查
runtimeClasspathElements=(List)expressionEvaluator.evaluate(${project.runtimeClasspathElements});
}捕获(表达式评估异常e){
抛出新的ComponentConfigurationException(“计算:${project.runtimeClasspathElements}时出现问题”,e);
}
//将项目依赖项添加到类领域
最终URL[]URL=buildURL(runtimeClasspathElements);
用于(URL:URL){
containerRealm.addComponent(url);
}
}
私有URL[]构建URL(列出runtimeClasspathElements)引发ComponentConfigurationException{
//添加项目类和依赖项
列表URL=newArrayList(runtimeClasspathElements.size());
for(字符串元素:runtimeClasspathElements){
试一试{
最终URL=新文件(元素).toURI().toul();
添加(url);
if(LOGGER.isDebugEnabled()){
debug(“添加到项目类加载器:”+url);
}
}捕获(格式错误){
抛出新的ComponentConfigurationException(“无法访问项目依赖项:”+元素,e);
}
}
//添加插件的依赖项(因此,如果Trove未打开,Trove的东西就可以工作)
返回URL.toArray(新URL[URL.size()]);
}
}
<modelVersion>4.0.0</modelVersion>
<groupId>com.delver</groupId>
<artifactId>reference-gen-plugin</artifactId>
<name>Reference Code Genration Maven Plugin</name>
<packaging>maven-plugin</packaging>
<version>1.2</version>
<url>http://maven.apache.org</url>
<properties>
<maven.version>2.2.1</maven.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
4.0.0
com.delver
参考gen插件
参考代码genrationmaven插件
maven插件
1.2
http://maven.apache.org
2.2.1
org.codehaus.plexus
丛maven插件
描述符
org.apache.maven
马文伪影
${maven.version}
org.apache.maven
maven插件api
${maven.version}
org.apache.maven
马文项目
${maven.version}
org.apache.maven
maven模型
${maven.version}
org.apache.maven
马文堆芯
2.0.9
这是插件的pom.xml,适合那些需要它的人。现在应该编译出来,但没有问题。(标题有问题,所以忽略它)遵循这个链接…对我的代码做了非常类似的事情…我创建了一个maven fit插件 我的插件使用了完全相同的pom.xml,重用了IncludeProjectDependenciesComponentConfiguration 我正在使用maven 2.2.0 如果有帮助的话,再来一次pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.worldcorpservices.plugins</groupId>
<artifactId>maven-fit-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<name>maven-fit-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>
<properties>
<fitlibrary.version>2.0</fitlibrary.version>
<maven.version>2.2.0</maven.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.fitnesse</groupId>
<artifactId>fitlibrary</artifactId>
<version>${fitlibrary.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>poi</groupId>
<artifactId>poi</artifactId>
<version>3.7-20101029</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
<build>
4.0.0
插件
maven-fit插件
maven插件
1.0-快照
maven fit插件maven Mojo
http://maven.apache.org
2
2.2.0
org.apache.maven
maven插件api
2
朱尼特
朱尼特
4.7
测试
公用记录
公用记录
1.1.1
org.fitnesse
菲特图书馆
${fitlibrary.version}
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
try {
Set<URL> urls = new HashSet<>();
List<String> elements = project.getTestClasspathElements();
//getRuntimeClasspathElements()
//getCompileClasspathElements()
//getSystemClasspathElements()
for (String element : elements) {
urls.add(new File(element).toURI().toURL());
}
ClassLoader contextClassLoader = URLClassLoader.newInstance(
urls.toArray(new URL[0]),
Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(contextClassLoader);
} catch (DependencyResolutionRequiredException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}