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

时间:2013-11-08 16:57:31

标签: maven compiler-warnings

我们有一个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>

2 个答案:

答案 0 :(得分:2)

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

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

即使我没有配置default-compile,也会始终执行default-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>

答案 1 :(得分:0)

我发现David W.'s answer非常有用。不幸的是,我找不到使用generated-sources / <includes>(和{{1}的<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中,否则将不会被编译。