Maven 3: How to exclude generated sources from Xlint check?

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.

So the question is: how to exclude generated sources from Xlint check? (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>
+4
source share
1 answer

There is no maven-compiler-plugindirect configuration. The parameters passed are intended for all execution, so the transfer -Xlint:allwill be applied to all sources for compilation.

, : - , ( ). , : Maven.

2 : 2 / 2 .

/

, 2 : , exclude ( ), include . , / , ; 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>

, default-compile, Maven compile, .

, 2 , , . Maven my-parent 2 :

<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, - . showWarnings false, .

, . ( ), .

+6

Source: https://habr.com/ru/post/1657480/


All Articles