2012-02-08 26 views
12

我想:我如何得到一个java maven构建失败的编译器警告?

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
       <compilerArgument>-Werror</compilerArgument> 
       <fork>true</fork> 
      </configuration> 
     </plugin> 

,但没有喜悦。现在有什么想法能够在this blog post上建议的这样的错误中得到中世纪?

+0

有一个错误,如下所述,不再是一个问题。这个与compilerArgument指定的表单很好。 – 2015-03-26 18:48:27

回答

1

编辑的一端S:这个答案是过时的,但是我不能删除它,因为它是当时一个公认的答案。

这是Maven的一个错误,请参阅:http://jira.codehaus.org/browse/MCOMPILER-120它已经在Maven-compiler-plugin的2.4版本中修复了,但我不相信它已经发布了。标签不幸的工作。

+0

maven-compiler-plugin 2.4已经发布到Maven Central – yegor256 2012-05-20 16:40:33

+0

是的,但不是当我做出答案的时候。 – 2012-05-21 08:40:44

+1

为了清楚起见,这个bug不再是问题,并且问题中指定的表单很有用。 – 2015-03-26 18:47:29

0

有一种替代形式也许试试看?注意在<compilerArguments>

<configuration> 
    <compilerArguments> 
     <Werror /> 
    </compilerArguments> 
</configuration> 
0

对于maven编译器插件,通过在this commentopen jira issue中使用解决方法,编译器警告可能会失败。

这个工作对我来说:

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>2.3.2</version> 
     <configuration> 
      <compilerId>javac</compilerId> 
      <source>1.6</source> 
      <target>1.6</target> 
      <compilerArgument>-Werror</compilerArgument> 
      <showDeprecation>true</showDeprecation> 
     </configuration> 

     <dependencies> 
      <dependency> 
       <groupId>org.codehaus.plexus</groupId> 
       <artifactId>plexus-compiler-api</artifactId> 
       <version>1.8.2</version> 
       <exclusions> 
        <exclusion> 
        <groupId>org.codehaus.plexus</groupId> 
        <artifactId>plexus-component-api</artifactId> 
        </exclusion> 
       </exclusions> 
      </dependency> 
      <dependency> 
       <groupId>org.codehaus.plexus</groupId> 
       <artifactId>plexus-compiler-manager</artifactId> 
       <version>1.8.2</version> 
       <exclusions> 
        <exclusion> 
        <groupId>org.codehaus.plexus</groupId> 
        <artifactId>plexus-component-api</artifactId> 
        </exclusion> 
       </exclusions> 
      </dependency> 
      <dependency> 
       <groupId>org.codehaus.plexus</groupId> 
       <artifactId>plexus-compiler-javac</artifactId> 
       <version>1.8.2</version> 
       <scope>runtime</scope> 
       <exclusions> 
        <exclusion> 
         <groupId>org.codehaus.plexus</groupId> 
         <artifactId>plexus-component-api</artifactId> 
        </exclusion> 
       </exclusions> 
      </dependency> 
     </dependencies> 
    </plugin> 
20

更新到2015年,使用Maven 3.3和Java 8

这里有一个最小的编译器配置,使所有的警告,使 构建失败时警告发生。值得注意的

<plugins> 
    <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.3</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
      <showWarnings>true</showWarnings> 
      <compilerArgs> 
       <arg>-Xlint:all</arg> 
       <arg>-Werror</arg> 
      </compilerArgs> 
     </configuration> 
    </plugin> 
</plugins> 

位:

  • <showWarnings>true</showWarnings>是必需的。出于未知原因,Maven默认会主动禁止-nowarn标志的警告,因此-Xlint-Werror标志将被忽略。
  • showDeprecation不需要启用,因为-Xlint:all已经发出弃用警告。
  • 实验显示fork不需要启用,即使 文档另有说明。
+0

我喜欢忽略内置Maven插件的'groupId'以强调它们作为内置插件的特殊地位。内置的Maven插件都有默认的组标识'org.apache.maven.plugins',所以不需要在POM中指定它。 (尝试'mvn help:effective-pom'来验证这一点。) – glts 2016-02-28 14:40:05

2

maven-compiler-plugin新增功能3.6.0:failOnWarning标志。这为我工作:

<plugin> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>3.6.0</version> 
    <executions> 
     <execution> 
     <id>compile</id> 
     <phase>process-sources</phase> 
     <goals> 
      <goal>compile</goal> 
     </goals> 
     <configuration> 
      <compilerArgument>-Xlint:-processing</compilerArgument> 
      <failOnWarning>true</failOnWarning> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

注意,我必须排除processing棉绒或以其他方式自动此事的注释将与神秘的“符号未找到”的错误打破建设。

+1

上面的答案很有效,尽管我试着让它在没有'-Xlint'设置的情况下工作,并且没有成功。我发现' true'与'-错误'相同。本身'-Werror'不会做任何事情,因为它需要与'-Xlint:all'配对。所以自己' true'也不会做任何事情。 – 2017-08-24 15:37:15