我正在编写一些代码生成 maven-plugin。
我需要将我的项目类路径注入到我的插件执行类路径中。
我找到了这个article https://webtide.com/extending-the-maven-plugin-classpath-at-runtime/。那里的解决方案有效,但相当长。也许你们中有人知道开箱即用的解决方案。
找到了answer http://old.nabble.com/Adding-project-dependencies-and-generated-classes-to-classpath-of-my-plugin-td18624435.html!
好吧,帕斯卡是对的,这是基础!!
所以这是将编译类路径添加到插件执行中的最干净的方法(据我所知)。
以下是我的 code-gen 插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码。所以我需要首先编译代码,然后分析,生成一些代码,然后再次编译。
-
Use @configurator
在 Mojo 类中:
/**
* @goal generate
* @phase process-classes
* @configurator include-project-dependencies
* @requiresDependencyResolution compile+runtime
*/
public class CodeGenMojo
extends AbstractMojo
{
public void execute()
throws MojoExecutionException
{
// do work....
}
}
请注意@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()]);
}
}
-
这是我的插件的构建部分,您必须添加它。
<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>
这是插件的 pom.xml,供需要的人使用。现在编译应该没有问题了。 (标题有问题,所以忽略它)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)