tom.everett
tom.everett

Reputation: 21

Trouble binding Maven plugin to compile phase

I've been trying to write a maven plugin using annotations. My plugin declaration looks like:

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

And I have this in the pom file which compiles the plugin:

<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 appears to confirm that the plugin is bound to the compile phase:

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

When I invoke the mojo explicitly it works:

mvn jvmbasic:compile

and if I use an executions section in a pom file it also works. However, I had expected the mojo to be automatically bound to the compile phase, so that if I typed

mvn clean compile

it would automagically run. Am I missing something obvious?

The actual source code is here:

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

Upvotes: 2

Views: 949

Answers (3)

Tarek
Tarek

Reputation: 3160

I think you are missing the @Execute annotation, as describe in the 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

Upvotes: 1

khmarbaise
khmarbaise

Reputation: 97359

You seemed having a problem with the dependency in your pom for your mojo. You should use the following:

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

instead of

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

Furthermore it would be cleaner to use the maven-plugin-plugin as follows:

    <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>

An other point is to define the maven-compiler-plugin version in the mojo area, cause you don't use a global parent pom for your project.

Upvotes: 1

Sumit
Sumit

Reputation: 1669

Where is the pom.xml of the project you would automagically like to bind to the compile phase? Your project pom should have something like this in the build section.

<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>

Upvotes: 0

Related Questions