Maven How to show warnings of codes when compiling - java

I'm using maven to build a new project. There are some warnings in my codes which are underlined by yellow line. I wish maven could report these warnings in console. How can I accomplish that? Can I just add some parameters in the command such as mvn -XXX clean compile?

<showDeprecation>
Sets whether to show source locations where deprecated APIs are used.
Default value is: false
User property is: maven.compiler.showDeprecation
<showWarnings>
Set to true to show compilation warnings.
Default value is: false
User property is: maven.compiler.showWarnings
-- Maven Doku
As simple as mvn clean install -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.showWarnings=true all in one line.
You could even remove the =true part because, when you add a maven parameter with -D it automatically sets its value to true, if not set to something else.

With maven 3.3.9, using the maven-compiler-plugin and Java 1.8, you need a <configuration> section like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
I got this answer from here when none of the above worked for me: http://frequal.com/java/EnableWarningsInMaven.html

I am guessing you are using the compiler plugin. Have you tried this?

Use the maven compiler plugin in your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>

To get to see detailed logs in maven you can use the command line option
-X, --debug Produce execution debug output
e.g this can be used as
mvn clean install -X
Also if what you want to get in the logs is the compiler warnings (The value of the local variable a is not used). You can try modifying your project's pom.xml with the following -
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
... other plugins
</plugins>
</build>

Related

Maven command runs fine on local machine, but not in Jenkins

So I have a maven project that has a profile
When I run the following in the terminal for my project, the tests will run just fine
mvn clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod
When I run the following in a Jenkins project it will not run the tests that I need it too
clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod
Jenkins settings
I have setup a Jenkins Maven project and told Jenkins where my Pom is, and I send the command below for the goal and options "clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod"
Maven Settings
serial-integration-test
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<junitPlatformArtifactName>com.github.junit-team.junit5:junit-platform-engine</junitPlatformArtifactName>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/Parallel*IT.class</include>
</includes>
<systemPropertyVariables>
<test.environment></test.environment>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>validate</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<plugins>
<plugin>
<name>com.gm.onstar.rita.cucumber.CucumberReporter</name>
</plugin>
</plugins>
<glue>
<package>com.gm.onstar.rita.stepdefinitions</package>
</glue>
<featuresDirectory>src/main/resources/features</featuresDirectory>
<tags>
<tag>${cucumber.tag1}</tag>
<tag>${cucumber.tag2}</tag>
<tag>${cucumber.tag3}</tag>
<tag>${cucumber.tag4}</tag>
<tag>${cucumber.tag5}</tag>
<tag>~#Ignore</tag>
</tags>
</configuration>
</execution>
</executions>
</plugin>
What I have tried
I have tried putting in like this in Jenkins -Dcucumber.options="--tags #QR_Scan" and like this -Dcucumber.options='--tags #QR_Scan'
I have tried putting the profile -Pserial-integration-test with a space and without a space -P serial-integration-test
I have tried to see if I a missing some sort of plugin
I have tried to see if Mvn clean install or Mvn clear verify would run it on Jenkins
What it might be.
Jenkins does not like that my cucumber runner is in the main and not in java test folder?
Am I missing a plugin for Jenkins?
Am I typing the command in wrong in jenkins, and maybe missing some parentheses or something?
Any help would be appreciated. Thank you!

How to run java program using Maven

I am trying to run a Java project using Maven, need help on how to run
I tried with various options, Run As > Maven clean, Run As > Maven Install, Run As > Maven test, etc.
But the output is not showing in the console though build is successful
I am using eclipse and able to run java file using Run As > Java Application
My Build tag in pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>mypackage.classnamehavingmain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
You should add the goal for execution
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>your_goal_name</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
and then to execute you can perform :
mvn exec:your_goal_name
If you would like to run it from the terminal you can do mvn compile exec:java -Dexec.mainClass=mypackage.classnamehavingmain.

How to set custom version of jdk in maven?

I am new in Maven.
I work in Windows and when I try to do the next instructions mvn clean install in folder with pom.xml file it throw me errors like this:
CLASS_NAME.java error: diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
As I can understand from the message it occurs because maven use jdk version 1.5 (Actually I even didn't install it). In maven settings folder I found toolchains.xml file. If I understood right, it is possible to set custom version of jdk for user using this file. So I add this code to my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<configuration>
<toolchains>
<jdk>
<version>[1.8]</version>
</jdk>
</toolchains>
</configuration>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
and this to toolchains.xml
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>C:/Program Files/Java/jdk1.8.0_45</jdkHome>
</configuration>
</toolchain>
If somebody know can you tell me how to fix this? I will appreciate any help, idea or explanation.
P.S. JAVA_HOME is C:\Program Files\Java\jdk1.8.0_45
Try configuring the maven-compiler-plugin instead of the maven-toolchains-plugin in your pom.xml.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Source

Could not find dependencies for aspectj-maven-plugin

I have a problem with CTW aspects using aspectj-maven-plugin. I get the following error (execution entry is being highlighted):
Multiple annotations found at this line:
- Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.5:compile failed: Plugin
org.codehaus.mojo:aspectj-maven-plugin:1.5 or one of its dependencies could not be resolved: Could not find artifact
com.sun:tools:jar:1.7.0_21 at specified path C:\Program Files\Java\jre7/../lib/tools.jar (org.codehaus.mojo:aspectj-maven-
plugin:1.5:compile:default:compile)
- Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.5:test-compile failed: Plugin
org.codehaus.mojo:aspectj-maven-plugin:1.5 or one of its dependencies could not be resolved: Could not find artifact
com.sun:tools:jar:1.7.0_21 at specified path C:\Program Files\Java\jre7/../lib/tools.jar (org.codehaus.mojo:aspectj-maven-
plugin:1.5:test-compile:default:test-compile)
On the configuration:
<build>
<plugins>
<!-- http://mojo.codehaus.org/aspectj-maven-plugin/usage.html -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>
<source>1.7</source>
<target>1.7</target>
<sources>
<source>
<basedir>src/main/java</basedir>
<includes>
<include>**/*Aspect.java</include>
</includes>
</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
What am I doing wrong? It looks like as if this plugin was unable to find jdk? But why?
Is your JAVA_HOME set properly? Please check that. It worked perfectly for me. So I think you should add below mentioned plugin and try:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Run mvn compile after that.
Please check the JAVA_HOME env variable. This happened to me when JAVA_HOME is pointed to JRE folder rather than jdk folder.
I had this problem running with java 11, seems like it is only compatible with java 8.
Looking into the project, aspectj-maven-plugin it looks like the update was committed but never actually merged.

Different maven compiler versions for test and main

How can I configure the maven compiler to use java 5 for my test code and java 1.4 for my main code?
If you want to set compliance to the relevant Java version, you can configure the compiler plugin for each execution. Assuming Maven is using a JDK at least as current as the highest version you specify. By using properties you can override that configuration on the commandline or in a child if needed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>${testCompileSource}</source>
<target>${testCompileSource}</target>
</configuration>
</execution>
</executions>
</plugin>
...
<properties>
<compileSource>1.4</compileSource>
<testCompileSource>1.5</testCompileSource>
</properties>
If you mean using different compilers, that's a bit more involved. as you need to specify the path to the JDK and what compiler version you're using. Again these can be defined in properties. Though you may want to define them in your settings.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
<executable>${compileJdkPath}/bin/javac</executable>
<compilerVersion>${compileSource}</compilerVersion>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>${testCompileSource}</source>
<target>${testCompileSource}</target>
<executable>${testCompileJdkPath}/bin/javac</executable>
<compilerVersion>${testCompileSource}</compilerVersion>
</configuration>
</execution>
</executions>
</plugin>
...
<properties>
<compileSource>1.4</compileSource>
<testCompileSource>1.5</testCompileSource>
<compileJdkPath>path/to/jdk</compileJdkPath>
<testCompileJdkPath>path/to/test/jdk<testCompileJdkPath>
</properties>
Note it might make sense to define the compiler configurations in profiles, one for each JDK you support, so that your normal builds don't rely on properties being set.
Also, in Maven 3.x, you need to include the fork parameter when specifying the executable, e.g.:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<fork>true</fork>
<executable>${testCompileJdkPath}/bin/javac</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
I had no luck with the accepted answer compiling java 7 source and java 8 test sources using the maven-compiler-plugin, version 3.5.1. Because the compile plugin used the source / target parameter for both, main and test sources.
But I found out, there are separate configuration parameters for the test source and target.
So for me the solution that worked was
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
</plugins>
</build>
I found a solution at least for my case.
I am writing a library that needs to be Java 1.7 compatible but relies on the nashorn engine in JDK 8 to test some of its features, which is a perfect reason for using different compiler/jdk version for main and test.
When talking about using different compiler/JRE version for main and test, say version A for main and version B for test and A is strictly smaller than B, we really want to achieve the following:
Make the IDE (in my case, the Eclipse) use the JDK version B during development for not introducing compilation error (red cross on your project icon).
Make JUnit tests runnable from within your IDE.
Make JUnit tests runnable from command line through mvn clean test which is required for releasing.
Compile sources in main using JRE A, which enables automatic detection of using new features from JDK B.
Free use of new features / new APIs from JDK B in test cases codes.
And the following pom declaration works (for example let A = 1.7 and B = 1.8):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArguments>
<source>1.7</source>
<target>1.7</target>
</compilerArguments>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<compilerArguments>
<source>1.8</source>
<target>1.8</target>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
Firstly, <source>1.8</source> <target>1.8</target> is a must to make Eclipse happy and using 1.8 for the project itself, as others pointed out that Eclipse has a still pending bug and does not support different JDK version in a single project natively.
Secondly, use <execution> to override the source and target arguments during source compilation and test source compilation. default-compile and default-testCompile are special names for compiler plugin.
Thirdly, be sure to use compilerArguments but not compilerArgs. As far as I know, only compilerArguments can be used to override argument settings during runtime.

Categories