公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了如何在Maven中清洁封装时保护自动生成的源码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 PACKET’将始终删除这些类。我怎么才能防止它呢?是否可以让clean删除target目录中除generated/目录之外的全部内容?

推荐答案

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

这违反了Maven的惯例
每次希望生成这些类时,它都会强制您更改POM

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

您可以使用excludeDefaultDirectories和filesets参数排除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-plugin在generate-soures阶段执行,而另一个设置一个属性告诉cxf-xjc-plugin不要执行。

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

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

这篇关于如何在Maven中清洁封装时保护自动生成的源码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Maven 保护 如何在 封装 清洁 源码 自动生成
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定