Maven 3:如何从 Xlint 检查中排除生成的源?
Maven 3: How to exclude generated sources from Xlint check?
我们只允许 java 没有 Xlint 错误的源代码。但是,当源由第三方工具生成时,这是不切实际的。我们的用例中生成源的示例是:JFlex、JavaCC、JAXB 和注释处理器。
所以问题是:如何从 Xlint 检查中排除生成的源? (请参阅下面的当前配置)
<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
将应用于所有要编译的源。
这里的解决方案是分两次编译它:第一次编译生成的源代码而不进行任何 lint 检查,第二次编译你的项目源代码(这可能取决于生成的 类) .同样,Compiler Plugin 不提供指定编译源路径的方法:它编译当前 Maven 项目的所有源。
您有 2 个解决方案:使用 includes/excludes 编译器插件的 2 次执行或将其拆分为 2 个模块。
Include/Exclude
这个想法是执行 2 次:一次 exclude
your main classes (and compile the generated ones), while the other execution would include
他们。请注意,inclusion/exclusion 机制适用于 类 的完全限定名称, 而不是 目录结构;所以你不能排除 src/main/java
.
假设所有主要 java 源文件都在 my.package
包下,您可以:
<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
执行,因此它确保首先编译生成的 类。
使用两个模块
因此,您需要将它分成两个模块,第一个模块将生成源代码并编译它们,而第二个模块将依赖于第一个模块。创建一个 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>
.
第一个模块 my-generating-module
负责生成和编译源代码,无需任何 lint 检查。默认情况下,showWarnings
是 false
所以你可以保持默认配置。
然后,在第二个模块中,您可以依赖第一个模块,添加 lint 检查。因为这只会编译您的项目源代码(生成的 类 已经编译并打包到另一个模块中),您不会收到任何警告。
我们只允许 java 没有 Xlint 错误的源代码。但是,当源由第三方工具生成时,这是不切实际的。我们的用例中生成源的示例是:JFlex、JavaCC、JAXB 和注释处理器。
所以问题是:如何从 Xlint 检查中排除生成的源? (请参阅下面的当前配置)
<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
将应用于所有要编译的源。
这里的解决方案是分两次编译它:第一次编译生成的源代码而不进行任何 lint 检查,第二次编译你的项目源代码(这可能取决于生成的 类) .同样,Compiler Plugin 不提供指定编译源路径的方法:它编译当前 Maven 项目的所有源。
您有 2 个解决方案:使用 includes/excludes 编译器插件的 2 次执行或将其拆分为 2 个模块。
Include/Exclude
这个想法是执行 2 次:一次 exclude
your main classes (and compile the generated ones), while the other execution would include
他们。请注意,inclusion/exclusion 机制适用于 类 的完全限定名称, 而不是 目录结构;所以你不能排除 src/main/java
.
假设所有主要 java 源文件都在 my.package
包下,您可以:
<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
执行,因此它确保首先编译生成的 类。
使用两个模块
因此,您需要将它分成两个模块,第一个模块将生成源代码并编译它们,而第二个模块将依赖于第一个模块。创建一个 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>
.
第一个模块 my-generating-module
负责生成和编译源代码,无需任何 lint 检查。默认情况下,showWarnings
是 false
所以你可以保持默认配置。
然后,在第二个模块中,您可以依赖第一个模块,添加 lint 检查。因为这只会编译您的项目源代码(生成的 类 已经编译并打包到另一个模块中),您不会收到任何警告。