enabling the lambda expression - java

In the program like
entities.stream().filter(m->m.getId()==id).findAny().get();
where entities is a List. After setting all the libraries and other SDKs to Java 8. we are getting the error as:
use -source 8 or higher to enable lambda expressions

This is how solved my problem by adding the below plugin settings in my parent POM file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Related

How to add VM args using pom.xml plugin

I have tried below ways but nothing work...
i am trying to access jmx remotely from server.
<jvmArgs>
<jvmArg>-Dcom.sun.management.jmxremote.port=9999</jvmArg>
<jvmArg>-Dcom.sun.management.jmxremote.authenticate=false</jvmArg>
<jvmArg>-Dcom.sun.management.jmxremote.ssl=false</jvmArg>
</jvmArgs>
<!-- <systemPropertyVariables>
<com.sun.management.jmxremote.port>9999</com.sun.management.jmxremote.port>
<com.sun.management.jmxremote.authenticate>false</com.sun.management.jmxremote.a uthenticate>
<com.sun.management.jmxremote.ssl>false</com.sun.management.jmxremote.ssl>
</systemPropertyVariables> -->
<!-- <jvmArguments>
<jvmArgument>- Dcom.sun.management.jmxremote.port=9999</jvmArgument>
<jvmArgument>- Dcom.sun.management.jmxremote.authenticate=false</jvmArgument>
<jvmArgument>- Dcom.sun.management.jmxremote.ssl=false</jvmArgument>
</jvmArguments> -->
I also tried
<options>
<option>-Dcom.sun.management.jmxremote.port=9999</option>
<option>-Dcom.sun.management.jmxremote.authenticate=false</option>
<option>-Dcom.sun.management.jmxremote.ssl=false</option>
</options>
You can do this in few different ways.
Using the Maven Compiler Plugin
You can use the compileArgs property of the plugin to specify the Xmx, Xms, Xss VM arguments.
Below is an example
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<compilerArgs>
<arg>-Xmx512M</arg> // You can provide comma separated values if you have more than one
</compilerArgs>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Refer to the Maven Compiler Plugin for more details.
Using the Maven Surefire Plugin
You can use the argLine property of the plugin to specify the Xmx, Xms, Xss VM arguments.
Below is an example
</project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<argLine>-Xmx512M</argLine> // You can provide comma separated values if you have more than one
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Refer to the Maven Surefire Plugin for more details.

Different target in pom.xml files

I am building an android app and I am trying to use external libraries (jar).
When I build project it throws
bad class file magic or version
I checked pom.xml files and I noticed that in the one jar it has the following
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
while in the other
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
Could this justify the error?
I think that's because You can't use Java8 code in java7 project.

Why to specify java version in maven build?

When I was building my project like :-
mvn clean install -DskipTests
, then it was giving some error.
After that, I just added, -Djdk.version=1.8, then it works fine.
Can someone tell what is the reason for this?
Try something like this in your pom:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
....
</build>
It may be because you might be missing the maven-compiler-plugin.
Try the following:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
....
</build>
From Maven 3, it defaults to JDK 1.5. So if you do not include the version, it will take JDK 1.5 as default compiler version.
Since, you mentioned that you used JDK 1.8, and the error disappeared. So if you had maven-compiler-version defined in the pom.xml, the error might be because the version would had not been defined.
So by default, it pointed to JDK 1.5 and it was trying to compile the code which would be defined for JDK 1.8 and not for JDK 1.5.
So, it is better to define the correct java version in the pom.xml
Why to specify java version in maven build?
Because Maven won't try to guess the Java version your project was created with (the one you probably only configured in your IDE).
You need to specify the earliest version supported in your pom.xml with:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

How to create a new classes for each compilation using maven plugin

In case of my project I need to create new classes after each compilation. For compilation I'm using maven compiler plugin 3.1. I tried to use compilerReuseStrategy = alwaysNew option but it didn't make any affect, it always compile only changed classes. Here is plugin declaration in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerReuseStrategy>alwaysNew</compilerReuseStrategy>
</configuration>
</plugin>
An I doing something wrong or that's a bug and this option really doesn't work?
If you are talking about the incremental feature fo the maven-compiler-plugin you can change this behaviour by the following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
The compileReuseStrategy in contradiction is intended to define the behaviour in relationship with multi-threaded running of the compiler.
What about using mvn clean install?

maven: (use -source 5 or higher to enable static import declarations)

How can I use source 5?
I tried
mvn -source 5 test
but it didn't work :-)
When I compile the file by javac, everything works.
You need to configure the maven-compiler-plugin:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
...
</plugins>
...
</build>
</project>
EDIT: Changed example to use latest version of the plugin.

Categories

Resources