我有一个 Maven 配置文件,它触发 xsd
和 wsdl
类的自动生成,如下所示:
<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-plugin
在 generate-soures
阶段执行,而另一个设置一个属性告诉 cxf-xjc-plugin
不要执行。
因此,当你想生成类时,你可以用mvn clean install
调用Maven,当你不想生成类时,你可以用mvn调用Maven全新安装-PnoGenerate
。
这里真正的收获和优势在于,您无需在每次决定生成或不生成类时都更改 POM。
关于java - 如何在 Maven 中的清洁包期间保护自动生成的源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783735/