如何在Maven的干净打包期间保护自动生成的源?

mem*_*und 1 java cxf xjc maven

我有一个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>
Run Code Online (Sandbox Code Playgroud)

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

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

Tun*_*aki 5

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

  • 它违反了Maven约定
  • 它迫使您每次想要生成这些类时都更改POM

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

拟议的解决方案

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

<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>
Run Code Online (Sandbox Code Playgroud)

使用以下插件定义:

<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>
Run Code Online (Sandbox Code Playgroud)

似乎cxf-xjc-plugin没有任何skip参数,所以我们不得不求助于设置phasenone时我们要避免执行(这是一个未记录的功能,但它的工作原理)。

诀窍是定义两个配置文件:一个默认情况下激活的配置文件,它设置一个属性以告知cxf-xjc-plugin要在generate-soures阶段中执行,而另一个配置文件的属性则告诉其cxf-xjc-plugin不执行。

这样,当您想要生成类时,可以使用调用Maven,mvn clean install而当您不想生成类时,可以使用调用Maven mvn clean install -PnoGenerate

真正的好处是,您不必每次决定生成类时都不需要更改POM。