Maven:在常规编译期间报告警告,但不对生成的源报告

问题描述 投票:2回答:2

我们有一个Maven项目,它使用WSDL文件,这些文件被转换为Java源文件并在以后编译。

当这个项目使用Ant时,我们分别编译生成的Java源文件和普通开发人员编写的Java源文件。这允许我在编译开发人员编写的Java文件时打开弃用和警告,但是关闭编译WSDL生成的Java文件。我希望开发人员修复他们的警告和弃用,但我不能让开发人员负责WSDL生成的代码。

现在,我们已将项目移至Maven,我想做同样的事情:编译WSDL生成的Java源代码而不发出警告,并编译带有警告的开发人员编写的Java源代码。是否可以使用Maven? (我的意思是不在Ant中编写它并将其嵌入到pom.xml中)。


POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.vegicorp</groupId>
    <artifactId>crypto</artifactId>
    <packaging>bundle</packaging>
    <version>2.0.4</version> <!--package version-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <axis2.version>1.5.6</axis2.version>
        <maven.dir>${project.build.directory}/maven/crypto.jar</maven.dir>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugins</artifactId>
                    <version>3.3</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <debug>true</debug>
                    <debugLevel>lines,vars,source</debugLevel>
                    <compilerArgs>
                        <arg>-Xlint</arg>
                        <arg>Xmaxwarns</arg>
                        <arg>9999</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>2.3.7</version>
                <configuration>
                    <archive>
                        <manifestSections>
                            <manifestSection>
                                <name>Build-Information</name>
                                <manifestEntries>
                                    <Project-Name>${env.JOB_NAME}</Project-Name>
                                    <Build-Number>${env.BUILD_NUMBER}</Build-Number>
                                    <SVN-Revision>${env.SVN_REVISION}</SVN-Revision>
                                </manifestEntries>
                            </manifestSection>
                            <manifestSection>
                                <name>Module-Information</name>
                                <manifestEntries>
                                    <Group-ID>${project.groupId}</Group-ID>
                                    <Artifact-ID>${project.artifactId}</Artifact-ID>
                                    <Version>${project.version}</Version>
                                </manifestEntries>
                            </manifestSection>
                        </manifestSections>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
                <version>${axis2.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsdl2code</goal>
                        </goals>
                        <configuration>
                            <packageName>com.safenet.tokenization.wsclient</packageName>
                            <wsdlFile>src/main/wsdl/SafeNetTokenizer.wsdl</wsdlFile>
                            <databindingName>adb</databindingName>
                            <skipBuildXML>true</skipBuildXML>
                            <syncMode>sync</syncMode>
                            <overWrite>true</overWrite> 
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <targetSourceFolderLocation>generated-sources</targetSourceFolderLocation> 
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <xmlOutput>true</xmlOutput>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <reuseFork>true</reuseFork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                 <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                 <source>${project.build.directory}/generated-sources</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.0.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
            </plugin>
        </plugins>
    </reporting>

    <dependencies>
        <!-- COMPILE -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        ...
    </dependencies>
</project>
maven compiler-warnings
2个回答
2
投票

好的,我想出了如何做到这一点,但我并不喜欢这个解决方案:

  • 定义两个单独的maven-compiler-plugin执行。一个叫default-compile,我编译WSDL代码,一个叫main-compile,我编译其余的代码。
  • 在main-compiler-plugin执行配置中使用<includes/><excludes>来包含和排除我想编译的代码。
  • 我仍然需要build-helper-maven-plugin来定义两个独立的源。

即使我没有配置default-compiledefault-compile总是被执行(我无法找到关闭它的方法)。在查看我的两个定义的maven-compile-plugin插件执行之前,这会自动编译所有内容。为了解决这个问题,我将WSDL命名为default-compile

这是mavin-compiler-plugin配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>default-compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/com/safenet/**</include>
                </includes>
                <excludes>
                    <exclude>**/com/vegicorp/**</exclude>
                </excludes>
                <source>1.6</source>
                <target>1.6</target>
                <showDeprecation>false</showDeprecation>
                <showWarnings>false</showWarnings>
                <debug>true</debug>
                <debugLevel>lines,vars,source</debugLevel>
                <verbose>true</verbose>
            </configuration>
        </execution>
        <execution>
            <id>main-compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/com/vegicorp/**</include>
                </includes>
                <excludes>
                    <exclude>**/com/safenet/**</exclude>
                </excludes>
                <source>1.6</source>
                <target>1.6</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <debug>true</debug>
                <debugLevel>lines,vars,source</debugLevel>
                <compilerArgs>
                    <arg>-Xlint</arg>
                    <arg>Xmaxwarns</arg>
                    <arg>9999</arg>
                </compilerArgs>
            </configuration>
        </execution>
    </executions>
</plugin>

0
投票

我发现David W.'s answer非常有用。不幸的是,我找不到一个简单的方法来分离generated-sources为我当前的项目使用<includes> / <excludes>(和<testIncludes> / <testExcludes>default-testCompile),因为它们似乎是针对相对于compileSourceRoots的路径进行检查。

对我来说最有效的解决方案是为每个<compileSourceRoots>指定<execution>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.0</version>
  <executions>
    <execution>
      <id>default-compile</id>
      <configuration>
        <compileSourceRoots>
          <compileSourceRoot>${project.build.directory}/generated-sources/swagger/src/main/java</compileSourceRoot>
        </compileSourceRoots>
        <showWarnings>false</showWarnings>
      </configuration>
    </execution>
    <execution>
      <id>compile-with-warnings</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <compileSourceRoots>
          <compileSourceRoot>${project.build.sourceDirectory}</compileSourceRoot>
        </compileSourceRoots>
        <showWarnings>true</showWarnings>
        <compilerArgs>
          <arg>-Xlint:all</arg>
        </compilerArgs>
      </configuration>
    </execution>
    <execution>
      <id>default-testCompile</id>
      <configuration>
        <compileSourceRoots>
          <compileSourceRoot>${project.build.testSourceDirectory}</compileSourceRoot>
        </compileSourceRoots>
        <showWarnings>true</showWarnings>
        <compilerArgs>
          <arg>-Xlint:all</arg>
        </compilerArgs>
      </configuration>
    </execution>
  </executions>
</plugin>

这样做的优点是不再需要build-helper-maven-plugin(因为忽略了${project.compileSourceRoots}),但缺点是,由于${project.compileSourceRoots}被忽略,每个生成器插件的compileSourceRoot必须明确地添加到<execution>或者它不会被编译。

© www.soinside.com 2019 - 2024. All rights reserved.