Maven Plugin Build fails when Lambdas are used - java

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?

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.

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.

Related

Compiling mysql dependincy with maven Spigot

I personally prefer to keep my coding problem off of here unless I think I'm stumped. I'm trying to add the mysql java dependency to my project (I'm coding a Minecraft-Spigot 1.12.2 plugin).
I've been coding MySQL for years but I've never had to use it with Java until now so I'm following this tutorial: https://www.spigotmc.org/wiki/connecting-to-databases-mysql/
Which redirected me to this page: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.26
Naturally adding a Maven dep is easy enough you just copy and paste it! Beyond that, I'm not very in tune with the inner workings of the system.
Whenever I compile (it does in-fact compile without throwing any fits) and upload it to my servers I get this error: java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/MysqlDataSource. Now, I know that this has to do with the class being undefined or something. So obviously, I searched the error code. A lot of the problems I saw didn't seem to apply to me.
I double checked and I DO see mysql-connector-java inside of my "External Libraries" section in my IDE (InteliJ IDEA)
I also saw some people sending this link: How can I create an executable JAR with dependencies using Maven?
Which, the answer didn't seem to help me out either (unless I missed something).
My hope is that by sending my file someone can help me out here?
I've seen some people mention that it could be a problem with a classpath? How exactly can I go about fixing that (if it is the problem). I couldn't seem to find anything to help me with that problem (tho in this case classpath is a little over my head. Def want to read up on it more in the future).
Here's my pom.xml any helpful assistance is appreciated!
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.12.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>example</name>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
</project>
Did you use the right SNAPSHOT jar? Because that should work for you and
jar tf <the snapshot>.jar | find "com/mysql/cj/jdbc/MysqlDataSource"
on Windows or
jar tf <the snapshot>.jar | grep "com/mysql/cj/jdbc/MysqlDataSource"
on *nix should find that 'missing' class in the jar

Plugin 'org.apache.maven.plugins:maven-compiler-plugin:3.7.0' not found

I have just started with java dev. I am given a task of doing object serialization of avro format. I have broken my head trying different methods. The different IDEs and different tools, none of which have I been able to use to solve the issue, moreover they have made everything more complex and frustrating. I tried following a tutorial which didnt use an IDE per say but that was also unsuccessful as the Serialize.java class wasnt compiling. I'm presently trying to achieve the task using maven plugins and dependencies. I have just made a new Maven project on Intellij. This is my pom.xml where I have pasted the dependencies and plugins for avro format:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>avroTrial2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.10.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
</project>
The second plugin shows an error, I have done the maven syncing on intellij and thats how the error from first plugin was rectified. Now the error being shown is : "Plugin 'org.apache.maven.plugins:maven-compiler-plugin:' not found". I need to deal with this error first before I can go and get stuck on some avro error after this.
This might be the reason behind it. The configuration part in the maven compiler plugin section and the properties basically do the same thing. You have configured twice and with different versions. It should be either one of them and in general should match with the java version you are using (more details could be found here.
This is not matching
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
with
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
and only use one of them.
I have encountered same problem so what I did to resolve it is:
Navigated to File->Settings in IntelliJ
Select Plugin from left bar and then I unchecked Maven plugin
Restarted the IDE
Again clicked on Plugin and checked Maven
Restarted the IDE

Maven-compiler-plugin throws wanted compile errors but deny the compile progress

A few days ago i started with Maven. I have to put only a few of my dependencies in my generated jar file. This is needed because my code is only a plugin (Minecraft Plugin) executed by an api (Minecraft Server Software Spigot). Now the Problem is, that my Plugin depends on an other api (json-simple-1.1).
The last days i tried to edit the maven shade plugin to get the wished result. I failed, and now i did it in this way:
maven include the json-simple-1.1 api, i needing for my plugin
eclipse include the spigot api (Minecraft server software), which will executing my plugin
pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.falco.essentialsXXX</groupId>
<artifactId>EssentialsXXX-bungeecord</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Basic class for every Plugin
</description>
<build>
<sourceDirectory>src</sourceDirectory>
<!-- COMPILE -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- BUILD -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-json</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
When i now execute 'mvn clean install' (in the right directory) i get many many errors. That make completely sense. Maven can not find types or classes and everything else comeing from the spigot-api.
My Problem is, that this isnt a real error because when the spigot-api execute my plugin i have the classes and types i need. Maven dont know that and dont compile my Programm :(
At this point a have no idea what to do. I read so many articles but i couldnt find a solution. Every article say ohhh an error here try to use tags and the right api values. That isnt what i need.
I need something like a "bypass" attribute for the compiler so the compiler know "yes this is an error but the coder knows what he does"
If you need something for compilation, it needs to be a Maven dependency.
So take that artifact, install it in your local repository and add it as dependency.
Then your compilation process will probably work.
Note that using a dependency does not mean that you have to include the dependency into the resulting jar.

unable to satisfy dependency from com.lmax.disruptor 3.2.0 to package sun.misc 0.0.0

I am developing an eclipse plugin which needs an com.lmax.disruptor.It imports sun.misc. I have this in my p2 repository but when I maven build my plugin I am getting this error "unable to satisfy dependency from com.lmax.disruptor 3.2.0 to package sun.misc 0.0.0."
I have gone through the sites Resolve a dependency on package sun.misc with Tycho they are saying to create a plugin fragment but when I tried to create it and added export page as sun.misc, It is throwing an error like "package sun.misc doesnot exsist in the plugin".
How can solve this issue please help me with this.? Instead of creating new plugin fragment,is there is any possible way i can add in my plugin itself ?
Thanks,
As mentioned in oberlies' answer in the question you link to, you need to build a system bundle fragment, which exposes, i.e., exports, the sun.misc package. I don't know of any other way. However, this is easier than could be expected.
You do this by creating an OSGi MANIFEST.MF that exports sun.misc, and then bundle it into a fragment. This is done via Maven as follows.
<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>your.group</groupId>
<version>1.0.0</version>
<artifactId>your.group.fragment.sun.misc</artifactId>
<packaging>jar</packaging>
<name>System Bundle Fragment exporting sun.misc</name>
<description>This bundle extends the System Bundle export list with the sun.misc package such that OSGi bundles may refer to Sun's misc implementation without the OSGi framework itself to provide it in a non-portable way.</description>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<forceCreation>true</forceCreation>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Export-Package>sun.misc</Export-Package>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<instructions>
<Bundle-Category>your.group</Bundle-Category>
<Fragment-Host>system.bundle; extension:=framework</Fragment-Host>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run mvn clean install on this POM. Now you need to make the fragment consumable for Tycho, i.e., you need to make it available via a p2 Software Site.
Thankfully there is a great Maven plugin which can help with that: reficio's p2-maven-plugin. You can use it to basically wrap any mavenized JAR into an OSGi bundle and then provide it via a p2 site.
Set up the respective POM as follows.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sun-misc-p2</groupId>
<artifactId>site</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.reficio</groupId>
<artifactId>p2-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifacts>
<!-- specify your depencies here -->
<!-- groupId:artifactId:version -->
<artifact><id>com.lmax:disruptor:3.3.2</id></artifact>
<artifact><id>your.group:your.group.fragment.sun.misc:1.0.0</id></artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/target/repository/</webAppSourceDirectory>
<webApp>
<contextPath>/site</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>reficio</id>
<url>http://repo.reficio.org/maven/</url>
</pluginRepository>
</pluginRepositories>
</project>
Note that I use this plugin to provide the LMAX Disruptor (version 3.3.2, the latest one at the time of writing, is thankfully available from Maven Central).
Run mvn p2:site on the POM. This will create a p2 site containing the sun.misc fragment at {project-folder}/target/repository.
This p2 repository - and with it the sun.misc fragment - can now be added to your target platform, and hence used in your Tycho build.
This should fix it, and - to answer your question if "there is any possible way [you] can add in [your] plugin itself" - this is the only possible way to do it (I know of).
The sources are also available at https://github.com/newcodeontheblock/eclipse-rcp-with-async-logging. The whole procedure is also described in more detail in this - my - blog post about using async Log4j 2 loggers in an Eclipse RCP.

how to solve /bin/sh: protoc: command not found?

I have installed protobuf 2.5.0 in my CentOs,
when i execute the command protoc --version, it yields
libprotoc 2.5.0
as output.
but Once I have pulled code from git and when I try to compile it using Maven3, the proto module throws error saying,
protoc failed error: /bin/sh: protoc: command not found
I refereed many blogs and also did try to change my bashrc path as follows,
export JAVA_HOME=/opt/java/jdk1.7.0_67
export PATH=$PATH:/opt/java/jdk1.7.0_67/bin
export PATH=$PATH:/usr/local/lib
but if I execute,
sudo yum install protobuf-compiler
it installs protobuf2.3 compiler and this particular error gets solved. But since my pom file has protobuf 2.5.0, java abstract method error arises during the next compilation. I'm stuck on how to proceed. I've spend many hrs in it, so any help is much appreciated.
my pom file for the proto module,
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>GradPower</artifactId>
<groupId>org.screative.gardpower</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>proto</groupId>
<artifactId>proto</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<version>0.1.10</version>
<configuration>
<protocExecutable>protoc</protocExecutable>
<protoSourceRoot>${project.basedir}/src/main/proto/</protoSourceRoot>
<languageSpecifications>
<LanguageSpecification>
<language>JAVA</language>
<outputDirectory>
${project.basedir}/target/generated-sources/protoc
</outputDirectory>
</LanguageSpecification>
</languageSpecifications>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>dtrott</id>
<url>http://maven.davidtrott.com/repository</url>
</pluginRepository>
</pluginRepositories>
</project>
Thanks in advance.
EDIT:
I solved it. I copied protoc file from /usr/local/lib to /usr/bin
and it solved it. A silly mistake :P
You should run 'sudo ldconfig' to finish the installation.
I guessed you set LD_LIBRARY_PATH to add /usr/local/lib in your environment setting. And it doesn't take effect when building project with maven/sbt and other build tools.
Answer extracted from the Question (written by OP):
I solved it. I copied protoc file from /usr/local/lib to /usr/bin
and it solved it.

Categories

Resources