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

查看:43
本文介绍了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 检查,第二遍编译您的项目源代码(这可能取决于生成的类).同样,Compiler Plugin 没有提供指定编译源路径的方法:它编译当前 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 个解决方案:使用包含/排除的 Compiler Plugin 的 2 个执行或将其拆分为 2 个模块.

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

这个想法是有 2 个执行:一个将 exclude 您的主要类(并编译生成的类),而另一个执行将 include 它们.请注意,包含/排除机制适用于类的完全限定名称,不是目录结构;所以你不能排除 src/main/java.

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项目并有一个父my-parent 有 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>

你可以添加一个 块在这里为所有使用它的模块定义默认配置,比如 .

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

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

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天全站免登陆