【问题标题】:How to protect auto-generated sources during clean package in maven?如何在 Maven 中清理包期间保护自动生成的源?
【发布时间】:2015-12-23 09:04:16
【问题描述】:

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

    <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”总是会删除这些类。我该如何预防?除了generated/ 之外,是否可以让clean 删除target 目录的全部内容?

【问题讨论】:

  • 为什么要这样做? clean 删除了target 目录,Maven 生成的所有东西都应该放在这个目录下,所以一切都像它应该的那样运行。这些类将在下一次构建时重新生成。
  • 是的,但是 - 书面 - 我想阻止再生。我有一些很大的 wsdl 文件,并且重新生成需要很长时间。尽管如此,当移动或重命名包时,最终的war 包需要mvn clean,否则目标可能包含旧的编译类。另一种情况是升级了jar 依赖项,并且应该删除旧的依赖项。在所有这些情况下,不重新生成 wsdl java 类将节省几分钟,因为它们不是必需的。
  • 所以你真正的问题不是“如何使maven-clean-plugin不删除generated/”而是“我怎样才能避免再生”。这不是同一个问题。例如,这可以通过使用配置文件来完成。你能相应地更新问题吗?
  • 如果clean期间无法保护文件夹,那么是的,这就是我的问题。
  • 一气之下,Y turned into X。不错的工作。 :)

标签: java maven cxf xjc


【解决方案1】:

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

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

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

您可以使用excludeDefaultDirectoriesfilesets 参数排除带有maven-clean-plugin 的目录:

<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 clean install -PnoGenerate调用Maven。

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

【讨论】:

  • 使用excludeDefaultDirectories,难道不能使用mvn gnerate-sources来触发显式再生吗?
  • @membersound mvn generate-sources 总是会执行generate-sources 阶段,所以类将会生成。 clean 不会与 mvn generate-sources 一起调用。
【解决方案2】:

我会有另一种方法。

1st) 将您的 xjc 插件配置为不删除任何文件,即:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
    <execution>
        <goals>
            <goal>xjc</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <clearOutputDir>false</clearOutputDir>
    <outputDirectory>${project.build.directory}</outputDirectory>
    <sources>
        <source>  [any xsd] </source>
    </sources>
</configuration>

2nd) 使用 maven-clean-plugin 在 clean 阶段清理 jxc 类生成的目录:

<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
    <filesets>
        <fileset>
            <directory>${basedir}/generated/src/main/java/...where the generated classes are</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
            <followSymlinks>false</followSymlinks>
        </fileset>
    </filesets>
</configuration>

这对我有用,希望对我有用。

【讨论】:

    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 2020-08-19
    • 2017-02-19
    • 2014-12-05
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多