无法将Maven插件绑定到编译阶段

时间:2013-07-02 15:02:24

标签: java maven annotations maven-plugin

我一直在尝试使用注释编写maven插件。我的插件声明如下:

@Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE, requiresProject = true,     threadSafe = false)
public class CompileMojo extends AbstractMojo

我在编译插件的pom文件中有这个:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.2</version>
    <configuration>
    <!-- see http://jira.codehaus.org/browse/MNG-5346 -->                          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
    </configuration>
    <executions>
        <execution>
        <id>mojo-descriptor</id>
        <goals>
            <goal>descriptor</goal>
        </goals>
        </execution>
    </executions>
</plugin>

maven似乎确认插件已绑定到编译阶段:

mvn help:describe -DartifactId=jvmbasic-maven-plugin -DgroupId=com.khubla.jvmbasic -Dgoal=compile -Ddetail

[INFO] Mojo: 'jvmbasic:compile'
jvmbasic:compile
    Description: jvmBASIC compiler
    Implementation: com.khubla.jvmbasic.jvmbasicmojo.CompileMojo
    Language: java
    Bound to phase: compile

    Available parameters:

    sourceDir
         where to find the sources
    targetDir
         target dir
    verbose
       verbose

当我明确地调用mojo时,它可以工作:

mvn jvmbasic:compile

如果我在pom文件中使用执行部分,它也可以。但是,我曾期望mojo自动绑定到编译阶段,因此如果我输入

mvn clean compile

它会自动运行。我错过了一些明显的东西吗?

实际的源代码在这里:

https://github.com/teverett/jvmBASIC/tree/master/jvmbasicmojo

3 个答案:

答案 0 :(得分:1)

您似乎遇到了dependency in your pom for your mojo的问题。您应该使用以下内容:

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

而不是

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

此外,使用maven-plugin-plugin会更清晰,如下所示:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <configuration>
        <goalPrefix>jvmbasic</goalPrefix>
    </configuration>
    <executions>
        <execution>
            <id>default-descriptor</id>
            <goals>
                <goal>descriptor</goal>
            </goals>
            <phase>process-classes</phase>
        </execution>
        <execution>
            <id>help-descriptor</id>
            <goals>
                <goal>helpmojo</goal>
            </goals>
            <phase>process-classes</phase>
        </execution>
    </executions>
   </plugin>

另一点是在mojo区域中定义maven-compiler-plugin版本,因为您没有为项目使用全局父pom。

答案 1 :(得分:1)

我认为您错过了@Execute注释,如Maven Plugin Tool for Annotations中所述:

@Mojo(name="compile", defaultPhase = LifecyclePhase.COMPILE, 
      requiresProject = true, threadSafe = false)
@Execute(goal = "compile", phase = LifecyclePhase.COMPILE)
public class CompileMojo extends AbstractMojo

答案 2 :(得分:0)

项目的pom.xml在哪里可以自动地绑定到编译阶段?您的项目pom应该在构建部分中具有类似的内容。

<build>
    <plugins>
        <plugin>
        <artifactId>jvmbasic-maven-plugin</artifactId>
        <version>${jvmbasic.version}</version>
        <configuration>
           <sourceDir>${maven.compiler.source}</sourceDir>
           <targetDir>${maven.compiler.target}</targetDir>
        </configuration>

    </plugin>