简体   繁体   English

如何从Maven中的另一个插件继承Mojo

[英]How to inherit a Mojo from another plugin in Maven

I'm trying to inherit from the default CompilerMojo from Maven and execute this new Mojo as newcompile goal. 我试图从Maven继承默认的CompilerMojo并执行这个新的Mojo作为新的newcompile目标。

My setup: 我的设置:

pom.xml of new-compiler-plugin new-compiler-plugin的pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.plugins</groupId>
    <artifactId>new-compiler-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0.5</version>
        </dependency>
    </dependencies>
</project>

NewCompilerMojo.java

import org.apache.maven.plugin.compiler.CompilerMojo;

/**
 * Goal which touches a timestamp file.
 *
 * @goal newcompile
 * 
 * @phase compile
 */
public class NewCompilerMojo extends CompilerMojo {

}

So I created a second Maven project to test the new-compiler-plugin . 所以我创建了第二个Maven项目来测试new-compiler-plugin Its pom.xml looks like this: 它的pom.xml看起来像这样:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.plugins</groupId>
    <artifactId>test-new-compiler-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>test.plugins</groupId>
                <artifactId>new-compiler-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>newcompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Now I run mvn clean package -e with the test-new-compiler-plugin : 现在我使用test-new-compiler-plugin运行mvn clean package -e

Caused by: java.lang.NullPointerException
        at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:481)
        at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)

Apparently Maven didnt initialized the @Component or @Parameter variables in the original CompilerMojo from Maven. 显然,Maven没有从Maven初始化原始CompilerMojo@Component@Parameter变量。

Somebody knows how to fix this or if its even possible to inherit in such a way from a Mojo of another plugin? 有人知道如何解决这个问题,或者它是否可以从另一个插件的Mojo中以这种方式继承?

I added the complete POMs and I'm getting the NullPointer with this exact code (using Maven 3.0.5). 我添加了完整的POM,我正在使用这个确切的代码获得NullPointer(使用Maven 3.0.5)。 Whole idea is to override the execute method to start a logger and log all output (especially the warnings) during the compile goal by calling the super execute method. 整个想法是覆盖execute方法以启动记录器并通过调用super execute方法记录编译目标期间的所有输出(尤其是警告)。 After that generating a report during the site phase. 之后在站点阶段生成报告。

The issue is that you're declaring your MOJO with the @goal and @phase tag inside the Javadoc instead of using annotations . 问题是你在Javadoc中使用 @goal@phase标签声明你的MOJO而不是使用注释 The maven-compiler-plugin is using annotations to configure itself, so you need to use them also: maven-compiler-plugin使用注释来配置自身,因此您还需要使用它们:

With annotations, your Mojo super class does not any more require to be in the same project. 使用注释,您的Mojo超类不再需要在同一个项目中。 Provided that the super class also uses annotations, it can now come from reactor projects or external dependencies. 如果超类也使用注释,它现在可以来自反应器项目或外部依赖项。

Add the following dependency to the POM of your Maven plugin: 将以下依赖项添加到Maven插件的POM:

<dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.4</version>
    <scope>provided</scope>
</dependency>

and configure it with 并配置它

import org.apache.maven.plugin.compiler.CompilerMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name = "newcompile", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE)
public class NewCompilerMojo extends CompilerMojo {

}

Reinstalling the Maven plugin and testing it inside a sample project, the logs shows that it is correctly invoked: 重新安装Maven插件并在示例项目中对其进行测试,日志显示它已被正确调用:

[INFO] --- new-compiler-plugin:1.0-SNAPSHOT:newcompile (default) @ test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 3 source files to ...\target\classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM