【问题标题】:Trouble with -Xlint:all and maven-Xlint:all 和 maven 的问题
【发布时间】:2013-02-28 10:54:43
【问题描述】:

我正在尝试让 maven 输出 lint 级别警告。我创建了一个小型测试程序,它应该生成一个关于在非静态上下文中使用静态方法的警告,但是尽管有许多不同的插件配置选项,构建总是成功而没有任何警告!

在做了一些谷歌搜索后,我发现了使用编译器插件的“compilerArgument(s)”属性的建议,但这似乎对我也不起作用。

这是我应该生成警告的示例程序:

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"));
        }
    }

此程序应发出警告,因为 Java 6 String::format 的 javadoc 表明此方法仅存在静态版本。我想特别抓住这个案例,因为它过去曾咬过我,编译器应该检测到它:-)

这是我的 pom 文件:

<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>

我尝试了这两种形式的 compilerArgument(s) 属性都无济于事。

运行 mvn clean compile 会产生以下输出:

[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] ------------------------------------------------------------------------

附加版本信息:

$ 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

【问题讨论】:

    标签: java maven javac lint


    【解决方案1】:

    这“对我有用”与您的来源。

    <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>
    

    我想你注释掉尝试的原因

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

    因为“all”标记将落入的 XML 命名空间“Xlint”而失败,这意味着 maven 配置解析器甚至可能看不到整个标记“Xlint:all”(位于不同的命名空间和所有)。

    顺便说一下输出的相关行

    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
    

    和我的环境

    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"
    

    也许您遇到了特定于平台的错误?

    【讨论】:

    • 使用您发布的方法,编译器插件似乎将嵌套的 xml 标签解释为实际的编译器参数。 Maven 输出以下内容:“无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project JavaScratchSpace: Fatal error compile: invalid flag: -compilerArgument” - 这是当使用“ -Xlint:all” - 也许我使用的版本有不同的语义?我指定了 maven-compiler-plugin 版本 3.0
    • @RonDahlgren 对不起,我意识到我真的把最初的答案搞砸了,希望编辑后的答案对你有用。
    • 好像一直都是我的javac!看起来 openjdk 没有正确捕捉到这种静态方法的使用。您发布的 pom 按预期工作,我的 javac 只是选择忽略它。通过直接使用“javac -Xlint:all App.java”进行验证。感谢您的帮助!
    • @RonDahlgren 很高兴帮助您找到问题的根源。 1.6.0_24 有点旧。今天到了 1.6.0_43,我记得在 1.6.0_18 和 1.6.0_26 之间有很多打嗝,因为它们整合到我现在记忆中的东西中。那时还没有什么大的阻碍演出的错误,但有很多小东西从盘子里掉了下来。
    • 使用 maven-compiler-plugin 3.2 版(可能之前),compilerArguments 已被弃用。使用 compilerArgs (maven.apache.org/plugins/maven-compiler-plugin/…)。
    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 2013-10-15
    • 1970-01-01
    • 2023-03-29
    • 2018-04-17
    • 2010-10-27
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多