Rom*_*man 33 maven-2 maven-plugin
我正在写一些代码生成maven-plugin.
我需要将我的项目类路径注入到我的插件执行类路径中.
我找到了这篇文章.解决方案有效,但很长.也许你们中的某个人知道开箱即用的解决方案.
Rom*_*man 19
找到答案了!
好吧,Pascal是对的,这里是基础!!
所以这是最简洁的方法(据我所知)将编译类路径添加到你的插件的执行.
以下是我的code-gen插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码.所以我首先需要编译代码,然后分析,生成一些代码,然后再次编译.
使用@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....
}
}
Run Code Online (Sandbox Code Playgroud)
请注意@configurator
javadoc标题中的行,它对于plexus IOC容器是必不可少的,而不仅仅是另一个注释行.
include-project-dependencies
配置器的实现.我从一些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()]);
}
}
Run Code Online (Sandbox Code Playgroud)这是我必须添加的插件的构建部分.
<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>
Run Code Online (Sandbox Code Playgroud)
这是需要它的人的插件的pom.xml.现在应该编译没有问题.(标题出错了,所以忽略它)
mhs*_*ams 17
我采用了这种方法,显然它正在起作用:
1 - Mojo类中需要MavenProject参数:
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
Run Code Online (Sandbox Code Playgroud)
2 - 然后你可以从项目实例中获取classpath元素:
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14821 次 |
最近记录: |