java - 如何在 Maven 中的清洁包期间保护自动生成的源?

标签 java maven cxf xjc

我有一个 Maven 配置文件,它触发 xsdwsdl 类的自动生成,如下所示:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>${cxf-xjc-plugin}</version>
        <executions>
            <execution>
                <id>generate-sources</id>
                <phase>generate-sources</phase>

                <configuration>
                    <sourceRoot>${project.build.directory}/generated/src/main/java</sourceRoot>
                    <xsdOptions>
                        //xsds, wsdls etc
                    </xsdOptions>
                </configuration>
                <goals>
                    <goal>xsdtojava</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

生成的类转到:target/generated/src/main/java

问题:运行“mvn clean package”总是会删除那些类。我该如何预防? clean 是否可以删除 target 目录中 generated/ 目录的全部内容?

最佳答案

使用 maven-clean-plugin 可以不删除某些目录,但这绝对不是一个好主意:

  • 它违反了 Maven 约定
  • 每次您想要生成这些类时,它都会强制您更改 POM

您的确切问题的解决方案(不推荐)

您可以使用 excludeDefaultDirectories 使用 maven-clean-plugin 排除目录和 filesets参数:

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.6.1</version>
    <configuration>
        <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
            <fileset>
                <directory>${project.build.directory}</directory>
                <excludes>
                    <exclude>generated/*</exclude>
                </excludes>
            </fileset>
        </filesets>
    </configuration>
</plugin>

请注意,我强烈建议您不要使用此解决方案。

建议的解决方案

您的实际问题不是每次构建时都重新生成类,因为这会花费很多时间。目标是通过使用自定义配置文件避免生成:

<profiles>
    <profile>
        <id>noGenerate</id>
        <properties>
            <xjc.generate>none</xjc.generate>
        </properties>
    </profile>
    <profile>
        <id>generate</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <xjc.generate>generate-sources</xjc.generate>
        </properties>
    </profile>
</profiles>

使用以下插件定义:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-xjc-plugin</artifactId>
    <version>${cxf-xjc-plugin}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>${xjc.generate}</phase>
            <configuration>
                <sourceRoot>${project.build.directory}/generated/src/main/java</sourceRoot>
                <xsdOptions>
                    //xsds, wsdls etc
                </xsdOptions>
            </configuration>
            <goals>
                <goal>xsdtojava</goal>
            </goals>
        </execution>
    </executions>
</plugin>

似乎 cxf-xjc-plugin 没有任何 skip 参数,所以我们不得不求助于将 phase 设置为 none 当我们想避免执行时(这是一个未记录的功能,但它有效)。

诀窍是定义两个配置文件:一个默认激活,设置一个属性告诉 cxf-xjc-plugingenerate-soures 阶段执行,而另一个设置一个属性告诉 cxf-xjc-plugin 不要执行。

因此,当你想生成类时,你可以用mvn clean install调用Maven,当你不想生成类时,你可以用mvn调用Maven全新安装-PnoGenerate

这里真正的收获和优势在于,您无需在每次决定生成或不生成类时都更改 POM。

关于java - 如何在 Maven 中的清洁包期间保护自动生成的源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783735/

相关文章:

java - SQL注入(inject)和可能的攻击

java - 无法加载驱动程序类 : com. microsoft.jdbc.sqlserver.SQLServerDriver

java - OSGI org.slf4j.impl 依赖项

jakarta-ee - cxf jaxrs 相当于 Jersey @NameBinding

java - 尝试检查输入是否为整数时出现 java.lang.NullPointerException

JavaAgent“java.lang.NoClassDefFoundError : de. bea.domingo.DNotesFactory

Java - TableModel 和 DefaultTableModel

java - 尝试使用 Jersey 创建 REst 服务

在 JBOSS 上使用 CXF 处理 SOAP 请求时出现 java.lang.ClassNotFoundException

java - 未找到 MultipartBody、multipart/form-data 的消息正文编写器