StormeHawke
StormeHawke

Reputation: 6207

Maven Plugin Build fails when Lambdas are used

I wrote a maven plugin/mojo to generate some code. The plugin works well, and is built in Java JDK 1.8.

I am seeing an odd bit of behavior though: It builds, installs, etc just fine if I use pre-1.8 syntax, but as soon as I use a Java 8 Lambda expression, I get the following error when executing mvn clean install:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (default-descriptor) on project my-maven-plugin: Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor failed: 13557 -> [Help 1]

Here's my pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.my.group.id</groupId>
    <artifactId>my-maven-plugin</artifactId>
    <packaging>maven-plugin</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>My Maven Mojo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <org.springframework.version>4.0.1.RELEASE</org.springframework.version>
        <joda-time.version>2.3</joda-time.version>
    </properties>

    <dependencies>
       <!-- several dependencies -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- The following manual call to the maven-plugin plugin is necessary to get around a bug in maven 3.1.1.
             If the build server gets updated to 3.2.2+ we can remove this -->
            <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>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>inin-release</id>
            <name>ININ Release Repository</name>
            <url>http://nexus.infrastructure.inintca.com/nexus/content/repositories/inin-release</url>
        </repository>
        <snapshotRepository>
            <id>inin-snapshot</id>
            <name>ININ Snapshot Repository</name>
            <url>http://nexus.infrastructure.inintca.com/nexus/content/repositories/inin-snapshot</url>
        </snapshotRepository>
    </distributionManagement>
</project>

The issue appears in this case when I attempt to use a lambda to define a FileFilter rather than the traditional anonymous inner class.

This works:

List<File> controllerFiles = Arrays.asList(packageDirFile.listFiles(new FileFilter()
{
    @Override
    public boolean accept(File file)
    {
        return file.isFile() && file.getName().endsWith(".java");
    }
}));

While this produces the aforementioned error:

List<File>
        controllerFiles =
        Arrays.asList(packageDirFile.listFiles(file -> file.isFile() &&
                file.getName().endsWith(".java")));

Obviously given that I have a workaround this isn't critical, but the lambda is much cleaner than the anonymous inner class IMO and given that I'm working in Java 8 I'd much prefer to use it. Anybody have any tips, particularly given that I've already set skipErrorNoDescriptorsFound to true?

Upvotes: 16

Views: 4546

Answers (2)

user944849
user944849

Reputation: 14951

The note on MPLUGIN-273 says lambdas work if the build is using maven-plugin-plugin version 3.3. The error message shown references maven-plugin-plugin version 3.2 . Make sure you're using the correct version of the plugin and see if that fixes the issue.

Upvotes: 15

StormeHawke
StormeHawke

Reputation: 6207

I just realized I answered my own question. In my second comment there's a link to a Maven Jira issue, in which somebody commented that upping the maven-plugin-plugin version to 3.3 from 3.2 fixes the issue, and after trying it I discovered the same. So this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.2</version>
    ...    
</plugin>

becomes

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.3</version>
    ...    
</plugin>

And the problem goes away.

Upvotes: 7

Related Questions