gpt4 book ai didi

maven-2 - 将 maven-build-classpath 添加到插件执行类路径

转载 作者:行者123 更新时间:2023-12-03 00:33:05 25 4
gpt4 key购买 nike

我正在编写一些代码生成 maven-plugin。

我需要将我的项目类路径注入(inject)到我的插件执行类路径中。

我找到了这个article 。那里的解决方案有效,但相当长。也许你们中的某个人知道开箱即用的解决方案。

最佳答案

找到answer !

好吧,帕斯卡是对的,这是基础!!

所以这是将编译类路径添加到插件执行中的最干净的方法(据我所知)。

这里是我的代码生成插件中的一些代码示例,它实际上是根据编译的代码生成一些模板代码。所以我需要首先编译代码,然后分析,生成一些代码,然后再次编译。

  1. 在 Mojo 类中使用 @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....
    }
    }

    请注意javadoc头中的@configurator行,它对于plexus IOC容器来说是必不可少的,而不仅仅是另一个注释行。

  2. include-project-dependencies 配置器的实现。这是我从 Brian Jackson 那里学到的一个非常好的类(class),将其添加到您的插件的源代码中。

    /**
    * 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()]);
    }

    }
  3. 这是我的插件的构建部分,您必须添加它。

    <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,供需要的人使用。现在编译应该没有问题了。 (标题有问题,所以忽略它)

关于maven-2 - 将 maven-build-classpath 添加到插件执行类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com