Maven 3:如何从Xlint检查中排除生成的源? [英] Maven 3: How to exclude generated sources from Xlint check?

查看:133
本文介绍了Maven 3:如何从Xlint检查中排除生成的源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们只允许没有Xlint错误的Java源代码.但是,当源由第三方工具生成时,这是不切实际的.在我们的用例中,生成的源示例包括:JFlex,JavaCC,JAXB和注释处理器.

We only allow java source code without Xlint errors. However, when sources are generated by third party tools, this is not practical. Examples of generated sources in our use-case are: JFlex, JavaCC, JAXB and annotation processors.

所以问题是:如何从Xlint检查中排除生成的源? (请参阅下面的当前配置)

So the question is: how to exclude the generated sources from the Xlint checks? (see current configuration below)

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration combine.self="override">
        <source>${java.version}</source>
        <target>${java.version}</target>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
        <!-- Since JDK1.3 javac ignores any optimization flags -->
        <optimize>true</optimize>
        <debug>false</debug>
    </configuration>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
        <configuration>
          <!-- everything in target/generated-sources/** should be excluded from this check -->
          <compilerArgs>
            <arg>-Xlint:all,-rawtypes</arg>
          </compilerArgs>                     
        </configuration>
      </execution>
    </executions>
</plugin>

推荐答案

maven-compiler-plugin 可以做到这一点.传递的参数用于整个执行,因此传递-Xlint:all将适用于所有要编译的源.

There is no direct configuration in the maven-compiler-plugin to do that. The parameters passed are for the whole execution, so passing -Xlint:all would apply to all sources to compile.

这里的解决方案是分两步进行编译:第一遍将编译生成的源代码,而无需进行任何lint检查;第二遍将编译您的项目源代码(可能取决于生成的类).再次,编译器插件没有提供一种指定要编译的源的路径的方法:它编译了当前Maven项目的所有源.

The solution here is to compile it in two pass: first pass would compile the generated sources without any lint check, and the second pass would compile your project sources (that might depend on the generated classes). Again, the Compiler Plugin doesn't offer a way to specify a path to sources to compile: it compiles all of the sources of the current Maven project.

您有2个解决方案:使用包含/排除项的2个编译器插件执行程序,或将其拆分为2个模块.

You have 2 solutions: use 2 executions of the Compiler Plugin with includes/excludes or split this in 2 modules.

这个想法是要执行两次:一次执行 exclude 您的主类(并编译生成的主类),而其他执行将

The idea is to have 2 executions: one that would exclude your main classes (and compile the generated ones), while the other execution would include them. Note that the inclusion/exclusion mechanism works on the fully qualified name of the classes, not the directory structure; so you can't exclude src/main/java.

假设所有主要的Java源文件都在my.package包下,则可以:

Assuming all of your main java source files are under the my.package package, you can have:

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.5.1</version>
  <configuration>
    <source>${java.version}</source>
    <target>${java.version}</target>
  </configuration>
  <executions>
    <execution> <!-- this execution excludes my main sources under my.package -->
      <id>default-compile</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration> <!-- no lint check -->
        <excludes>
          <exclude>my/package/**/*.java</exclude>
        </excludes>
      </configuration>
    </execution>
    <execution> <!-- this execution includes my main sources under my.package -->
      <id>compile-main</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration> <!-- adds lint check -->
        <includes>
          <include>my/package/**/*.java</include>
        </includes>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
        <compilerArgs>
          <arg>-Xlint:all,-rawtypes</arg>
        </compilerArgs>
      </configuration>
    </execution>
  </executions>
</plugin>

之所以可行,是因为第一次执行会覆盖Maven在compile阶段自动启动的default-compile执行,因此可以确保首先编译生成的类.

This works because the first execution overrides the default-compile execution that Maven launches automatically on the compile phase, so it makes sure that the generated classes are compiled first.

因此,您需要将其分成2个模块,第一个模块将生成源代码并对其进行编译,而第二个模块将依赖于第一个模块.创建一个多模块Maven项目,并有一个父级 带有2个模块:

As such, you need to split this in 2 modules, where the first module would generate the sources and compile them, while the second module would depend on the first one. Create a multi-module Maven project and have a parent my-parent with 2 modules:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.groupId</groupId>
  <artifactId>my-parent</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>my-generating-module</module>
    <module>my-module</module>
  </modules>
</project>

您可以在此处添加 <pluginManagement> 块,以定义所有使用它的模块,例如<source><target>.

You could add a <pluginManagement> block here to define default configuration for all the modules using it, like <source> and <target>.

第一个模块my-generating-module负责生成和编译源代码,而无需进行任何检查.默认情况下, showWarnings ,因此您可以保留默认配置.

The first module, my-generating-module, is responsible for generating and compiling the sources without any lint check. By default, showWarnings is false so you can keep the default configuration.

然后,在第二个模块中,您可以依赖第一个模块,并添加了lint检查.由于这只会编译您的项目源代码(生成的类已经在另一个模块中进行了编译和打包),因此您不会对此发出任何警告.

Then, in the second module, you can have a dependency on this first one, adding the lint check. Since this will only compile your project sources (the classes generated were already compiled and packaged in the other module), you won't have any warnings for those.

这篇关于Maven 3:如何从Xlint检查中排除生成的源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆