According to documentation, I can configure maven-compiler-plugin to show warnings using the <showWarnings> tag :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
I can also configure the plugin to pass arguments to javac, in particular -Xlint:all which "enables all recommended warnings" :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
</configuration>
</plugin>
Are these two options strictly equivalent ? If not, what is the difference ?
The compilerArgs controls the behaviour of the java compiler. These settings are passed on to the compiler, and not used directly by the maven-compiler-plugin. Using Xlint you can control which warnings the java compiler outputs from the compilation process. You can also add e.g. -Werror to abort compilation upon warnings (which in general is a good practice).
The showWarnings configuration on the other hand, is a setting for the maven-compiler-plugin (not passed on to the java compiler). It controls whether the plugin will output the warnings generated by the java compiler. So if you set it to false (I really don't understand why that is the default), you won't see the warnings generated by the java compiler. Even worse, the build will not fail even if you have set -Werror in the compilerArgs.
Related
We setup the compiler plugin in a dedicated parent pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
But the javadoc plugin (version 2.9.1) fails with error:
error: lambda expressions are not supported in -source 1.7
If i run with "-X" i only see that indeed source is set to 1.7: source = 1.7
So the question is why?
(the normal build works fine!)
I also tried to explicitly set source either by property maven.compiler.source or by plugin configuration - i put the compiler plugin configuration from above into the project.
So how to get it working?
I use maven-compiler-plugin to compile the project. With current configuration when project is building I get the WARNING in the log. My requirement is that how can I suppress the warnings when project build. Please let me know.
POM configuration
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<failOnError>false</failOnError>
</configuration>
</plugin>
See javac - Java programming language compiler, Options:
An additional set of non-standard options are specific to the current virtual machine and compiler implementations and are subject to change in the future. Non-standard options begin with -X.
...
-Xlint:none Disable all warnings.
See also How to get compiler warnings when building a Jenkins plugin:
<configuration>
<compilerArgument>-Xlint:none</compilerArgument>
I must increase java heap space to compile my project. The only way I found is modify mvn.bat and set:
set MAVEN_OPTS=-XX:PermSize=256m -XX:MaxPermSize=256m -Xms1300M -Xmx1300M
than all colleagues must change the file localy.
I want keep all inside my project, not localy. Can I insert in the POM.xml or other file?
Setting MAVEN_OPTS usually provides arguments to the JVM running the build, and these are passed down to the compiler because it runs inline. You have probably already noted that the maven-surefire-plugin used for test usually fork a separate process so passing options to the plugin is bound inside the pom.xml.
What if you fork the compilation process also and add the flags there, such as the below example.
Notice the fork and the compiler args
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
<compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
I just set up a new project using maven 3. I added the following statement to my code:
if(5==5){
}
Which results in a comparing identical expressions warning in eclipse.
I use the following configuration for the maven-compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArgument>-Xlint:unchecked</compilerArgument>
<optimize>true</optimize>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
But it refuses to show the warning.
What should I try next?
The Xlint argument you're specifying will cause the javac compiler to warn about unchecked generic operations but, even with the other -Xlint options, there's no way to get the warning you're after from javac.
You'll need to configure your build to use the Eclipse compiler. See the Maven documentation for how to use a non-javac compiler.
I want to make maven compile fail if somewhere in the code, raw types (missing generics) are used. I tried the following:
The javac does not recognize these errors (the eclipse compiler does); Starting from version 7, the -Xlint:rawtypes parameter to javac can be used to issue warnings if raw types are used. Unfortunately, I have to use java 6 which does not support this flag.
CheckStyle has no rules for such things.
PMD also has no abilities to check for raw types.
Are there any other maven-modules or tricks which generate an error if raw types are used?
I don't know if it would help, but you can use the eclipse compiler in maven
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>eclipse</compilerId>
<source>1.6</source>
<target>1.6</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
</plugin>