How to compile using -Xlint:unchecked in a Maven project?

JavaMavenNetbeans

Java Problem Overview


In NetBeans 7.2, I'm having trouble finding how to compile using -Xlint:unchecked in a Maven project. Under an Ant project, you can change compiler flags by going to Project Properties -> Compiling, but Maven projects don't seem to have any such option.

Is there any way to configure the IDE to compile with such flags using Maven?

Java Solutions


Solution 1 - Java

I guess you can set compiler arguments in your pom.xml. Please refer this http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html

 <compilerArgument>-Xlint:unchecked</compilerArgument>

Solution 2 - Java

I want to elaborate on @Nishant's answer. The compilerArgument tag needs to go inside plugin/configuration tag. Here is a full example:

<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>
      <testSource>1.8</testSource>
      <testTarget>1.8</testTarget>
      <compilerArgument>-Xlint:unchecked</compilerArgument>
    </configuration>
  </plugin>
</plugins>

Solution 3 - Java

This works for me...

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>11</source>
            <target>11</target>
            <compilerArguments>
                <endorseddirs>${endorsed.dir}</endorseddirs>
            </compilerArguments>
            <compilerArgs>
                <arg>-Xlint:unchecked</arg>   <-------this right here ---->
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>

Solution 4 - Java

The pom file information is spot on. I had the additional challenge of building someone else's Maven project in Jenkins and not having access to the pom file repository.

I created a pre-build step to insert the compiler parameter into the pom file after downloading it from git, for example

sed -i 's|/target> *$|/target>\n<compilerArgument>\n-Xlint:deprecation\n</compilerArgument>|' $WORKSPACE/pom.xml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexis KingView Question on Stackoverflow
Solution 1 - JavaNishantView Answer on Stackoverflow
Solution 2 - JavaRajVView Answer on Stackoverflow
Solution 3 - JavaRasheedView Answer on Stackoverflow
Solution 4 - JavaWMayrhoferView Answer on Stackoverflow