Tim Barclay
Tim Barclay

Reputation: 855

Maven mojo not picking up configuration from pom

I've written a Maven plugin and incorporated it as a goal in the package phase of another project with configuration given in that project's pom.xml. However, none of the fields set using the @parameter notation end up being populated so they just throw NullPointerExceptions when they get used.

My mojo:

/**
 * @goal wrap
 * @phase package
 */
public class MyMojo extends AbstractMojo {

    /**
     * @parameter expression="${project.build.directory}"
     */
    private String outputDirectory;

    /**
     * @parameter
     */
    private String dbDataName;

    private File dbFile;

    public MyMojo(){
        dbFile = new File(outputDirectory, dbDataName) // throws nullpointerexception
    }

    public void execute() throws MojoExecutionException{
        // Do stuff
    }
}

Some of the mojo pom:

<groupId>com.mycompany.wrapper</groupId>
<artifactId>something-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

...

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.2</version>
        <configuration>
            <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
        <executions>
            <execution>
                <id>mojo-descriptor</id>
                <goals>
                    <goal>descriptor</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

The relevant bit of my project pom:

<plugin>
    <groupId>com.mycompany.wrapper</groupId>
    <artifactId>something-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>wrap</goal>
            </goals>
            <configuration>
                <dbDataName>dcbTestData.sql</dbDataName>
            </configuration>
        </execution>
    </executions>
</plugin>

Can anyone see what I'm doing wrong here? Most likely it's some silly mistake that I'm just not seeing.

Upvotes: 2

Views: 4144

Answers (2)

Tim Barclay
Tim Barclay

Reputation: 855

I've solved the problem. I had done a few things wrong but after changing and tinkering, I managed to work it out.

Incidentally, this process was made rather harder by the fact that the Maven docs and user guides are a bit inconsistent about whether they prefer the annotations or the javadocs styles, and in some places they recommend deprecated methods, such as the expression="${stuff}" form.

Anyway to get it working:

  1. I corrected the mistakes pointed out by khmarbaise in his first comment where I had slightly mangled the maven-plugin-plugin definition in my pom.
  2. I updated to the Java annotations based way of denoting Mojos and parameters (it might have worked if I had done step 3 without doing this step, but it still seemed a good idea to update)
  3. The main problem was that I was trying to access the parameter variables in the constructor to assign values to other variables but the mojo doesn't pick up the configuration details until the execute() method is run. So I just moved any variable assignments that used the parameters to the start of the execute() method and then they worked.

So here's what my mojo looks like now:

@Mojo(name = "wrap", defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {

    @Parameter(property="project.build.directory")
    private File outputDirectory;

    @Parameter(property="dbDataName")
    private String dbDataName;

    private File dbFile;

    public void execute()
        throws MojoExecutionException {
        dbFile = new File(outputDirectory, dbDataName);

        // Do other stuff
    }
}

Upvotes: 5

khmarbaise
khmarbaise

Reputation: 97359

You should change your code like the following:

/**
 * @parameter default-value="${project.build.directory}"
 */
private String outputDirectory;

Upvotes: 2

Related Questions