77

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?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • 1
    If you need to pass multiple arguments, you may recieve an error with ``. See this answer for alternative `...`: https://stackoverflow.com/a/23743186/257299 – kevinarpe Feb 20 '15 at 10:11

4 Answers4

96

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>
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 1
    I've created a small test program that should generate a warning about the usage of static methods, but I can't seem to get maven to generate any warnings about it. Gist with sample program and pom file posted here -> https://gist.github.com/influenza/5145598 –  Mar 12 '13 at 18:32
  • @RonDahlgren: why would you expect this to throw a warning? – haylem Oct 01 '13 at 16:51
  • @haylem - Accessing a static field or method should generate a warning with that enabled. This is a details of the javac being used, but it's common: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcompiler%2Fref-preferences-errors-warnings.htm and http://pic.dhe.ibm.com/infocenter/dstudio/v3r1/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcompiler%2Fref-preferences-errors-warnings.htm –  Oct 01 '13 at 18:33
  • 1
    @RonDahlgren: right, it's a common warning, but it's not one that's reported by javac. It's reported by the Eclipse compiler and a few others, and by many additional code style checkers, but it won't be reported by javac. You could have maven use the eclipse compiler instead if you prefer. – haylem Oct 01 '13 at 19:18
  • 2
    The linked page suggests that the entry should go in the group. Which seemed logical. As per the link below it actually goes in the group. https://stackoverflow.com/questions/8215781/how-do-i-compile-with-xlintunchecked – Teto Dec 05 '17 at 07:55
64

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>
ADTC
  • 8,999
  • 5
  • 68
  • 93
RajV
  • 6,860
  • 8
  • 44
  • 62
  • 3
    Giving the context parameters need to go into is an important part of an helpful answer. Thank you! – foo Feb 24 '19 at 22:02
1

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>
Rasheed
  • 991
  • 1
  • 12
  • 18
-2

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
ddb
  • 2,423
  • 7
  • 28
  • 38