6

I'm trying to get maven to output lint level warnings. I've created a small test program that should generate a warning about using a static method from a non-static context, but despite a number of different plugin configuration options, the build always succeeds without any warnings!

After doing some googling, I found suggestions to use the 'compilerArgument(s)' attribute of the compiler plugin, but this doesn't seem to be working for me either.

Here's my sample program that should generate the warning:

package com.dahlgren;

    /**
     * Test space
     *
     */
    public class App {
        public static void main( String[] args ) {
            String foo = "foo";
            // I want this to generate a compilation warning
            System.out.println(foo.format("blah"));
        }
    }

This program should issue a warning, as the javadoc for Java 6 String::format indicates that only static versions of this method exist. I want to catch this case specifically, as it has bitten me in the past and the compiler should detect it :-)

Here is my pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dahlgren</groupId>
  <artifactId>JavaScratchSpace</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JavaScratchSpace</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <build>
        <plugins>
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.0</version>
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <!--
                    <compilerArguments>
                        <Xlint:all />
                    </compilerArguments>
                    -->
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>  
            </plugin>  
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.dahlgren.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I've tried both forms of the compilerArgument(s) attributes to no avail.

Running mvn clean compile yields the following output:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building JavaScratchSpace 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.3:clean (default-clean) @ JavaScratchSpace ---
[INFO] Deleting file set: /work/fun/JavaScratchSpace/target (included: [**], excluded: [])
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ JavaScratchSpace ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /work/fun/JavaScratchSpace/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ JavaScratchSpace ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /work/fun/JavaScratchSpace/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.716s
[INFO] Finished at: Tue Mar 12 11:39:21 PDT 2013
[INFO] Final Memory: 8M/150M
[INFO] ------------------------------------------------------------------------

Additional version information:

$ mvn --version && javac -version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-29-generic", arch: "amd64", family: "unix"
javac 1.6.0_24

1 Answers1

7

This "works for me" with your sources.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.buck</groupId>
  <artifactId>mavenproject3</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mavenproject3</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <compilerArgument>-Xlint:all</compilerArgument>
          <showWarnings>true</showWarnings>
          <showDeprecation>true</showDeprecation>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

I imagine that the reason your commented out attempt with

                <compilerArguments>
                    <Xlint:all />
                </compilerArguments>

is failing because of the XML namespace "Xlint" that the "all" tag would fall into, which means that the entire tag "Xlint:all" probably wasn't even seen by the maven configuration parser (being in a different namespace and all).

By the way, the relevant lines of output

Compiling 1 source file to C:\Users\edwbuck\Documents\NetBeansProjects\mavenproject3\target\classes
bootstrap class path not set in conjunction with -source 1.6
com/buck/mavenproject3/App.java:[12,35] static method should be qualified by type name, java.lang.String, instead of by an expression

and my environment

Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
Maven home: C:\Program Files\NetBeans 7.2.1\java\maven
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:\Program Files (x86)\Java\jdk1.7.0_07\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

Perhaps you stepped on a platform specific bug?

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 1
    Using the method you posted, it seems that the compiler plugin interprets the nested xml tags as actual compiler params. Maven outputs the following: "Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project JavaScratchSpace: Fatal error compiling: invalid flag: -compilerArgument" - this is when using " -Xlint:all " - maybe the version I am using has different semantics? I've specified maven-compiler-plugin version 3.0 –  Mar 12 '13 at 19:48
  • @RonDahlgren Sorry, I realized that I really messed up the initial answer, hopefully the edited one will be of some use to you. – Edwin Buck Mar 12 '13 at 19:55
  • 1
    Seems like it was my javac all along! Looks like the openjdk isn't correctly catching this static method usage. The pom you posted works as intended, my javac just chooses to ignore it. Verified by using 'javac -Xlint:all App.java' directly. Thanks for the help! –  Mar 12 '13 at 19:57
  • @RonDahlgren glad to help you get to the root of the problem. 1.6.0_24 was a bit old. It's up to 1.6.0_43 today, and I recall that between 1.6.0_18 and 1.6.0_26 there were a lot of hiccups as they integrated in something that slips my memory at the moment. No major show-stopping bugs back then, but a lot of little things that fell off the plate. – Edwin Buck Mar 12 '13 at 20:07
  • With maven-compiler-plugin version 3.2 (maybe before), compilerArguments is deprecated. Use compilerArgs (http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html). – mcoolive Feb 25 '15 at 17:38