4

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

2 回答 2

3

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

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

即使我没有配置,default-compile总是会执行(我想不出办法将其关闭)default-compile。在查看我定义的两个maven-compile-plugin插件执行之前,这将自动编译所有内容。为了解决这个问题,我将 WSDL 命名为 compile 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>
于 2013-11-11T18:51:43.393 回答
1

我发现大卫 W. 的回答非常有用。不幸的是,我找不到使用/ (和/ for )generated-sources为我当前项目分离的简单方法,因为它们似乎是根据相对于. <includes><excludes><testIncludes><testExcludes>default-testCompilecompileSourceRoots

最适合我的解决方案是<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(since${project.compileSourceRoots}被忽略),但缺点是since${project.compileSourceRoots}被忽略,compileSourceRoot对于每个生成器插件都必须<execution>显式添加,否则将不会被编译。

于 2019-03-23T03:18:21.307 回答