Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
StormeHawke Avatar asked Nov 04 '14 14:11

StormeHawke


People also ask

Are lambdas faster in Java?

I've seen a lot of questions here about Java lambdas performance, but most of them go like "Lambdas are slightly faster, but become slower when using closures" or "Warm-up vs execution times are different" or other such things.

Why it is important to enable the usage of lambda expressions?

Easy-to-Use APIs and Libraries: An API designed using lambda expressions can be easier to use and support other API. Enables support for parallel processing: A lambda expression can also enable us to write parallel processing because every processor is a multi-core processor nowadays.

Does lambda expression improve performance?

Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.

Does Java 7 have lambdas?

From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .


1 Answers

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.

like image 194
user944849 Avatar answered Sep 23 '22 18:09

user944849