【问题标题】:How to choose which maven executions are run?如何选择运行哪些 Maven 执行?
【发布时间】:2014-08-21 01:40:25
【问题描述】:

设置

我有一个尝试使用 Maven 构建的复杂项目。以下是该项目的简化示例。目录结构如下:

  • main - 该项目有多个“耳朵”,每个“耳朵”包含多个“战争”以及其他支持的 jar。然而,它们共享一组通用的 WSDL/XSD 以及 Java 代码。
    • pom.xml - 这是主要的 pom.xml 文件,我将在下面更详细地描述它。
    • common-code-one - 两个 'ear' 包有一个共同的代码体,只是 java 代码,没有 wsdl,没有 xsd,所以没问题。
    • wsdl - 包含所有 XSD 和 WSDL 文件的单个包,它有一个 pom.xml,可以简单地将其全部打包到一个 jar 文件中,以供以后在子目录中使用。
    • 耳包一
      • common-code-two - 来自“wsdl”的一些 XSD 在此处解压缩,并通过 jaxb2 转换为代码。作为其中的一部分,会生成一个剧集文件。
      • war-package-one - 更多的 XSD 和 WSDL 文件在此处解压缩,并通过 jaxws 转换为代码。为避免重复代码,我需要来自“common-code-two”的剧集文件,并且我需要来自“wsdl”的 XSD/WSDL 文件。
      • war-package-two - 不同的 XSD/WSDL,但我仍然需要来自 common-code-2 的剧集文件,以及来自“wsdl”的 XSD/WSDL。
      • ejb-package-one - 不需要“wsdl”中的任何内容,但可能需要“common-code-two”中的一些代码,而不需要剧集文件。
      • ...
    • 耳包二
      • common-code-two - 同样的问题,不同的 XSD。
      • ejb-package-two - 同样的问题,不同的代码。
      • war-package-three - 同样的问题,不同的 XSD/WSDL。
      • war-package-four - 同样的问题,不同的 XSD/WSDL。

现在,主要的“pom.xml”文件。遵循 Maven 的 DRY 原则,我将 maven-dependency-plugin 的两个执行都放入了主 pom.xml。问题是,在上面的某些子包中,我只希望运行“解包模式”执行,而在其他一些子包中,我希望 both '解包模式'和'解包常见-绑定'运行。当然,对于那些都不需要的,我只是不将依赖插件添加到构建列表中。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>

    <execution>
      <id>unpack-schema</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>com.foo.bar</groupId>
        <artifactId>wsdl</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>jar</type>
        <overWrite>true</overWrite>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <includes>${schema.include.list}</includes>
      </artifactItem>
    </artifactItems>
    <overWriteReleases>true</overWriteReleases>
    <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
    </execution>

    <execution>
      <id>unpack-common-binding</id>
      <phase>generate-sources</phase>
      <goals>
    <goal>unpack</goal>
      </goals>
      <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>com.foo.bar</groupId>
        <artifactId>${common.code.jar}</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>jar</type>
        <overWrite>true</overWrite>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <includes>**/commonCode.xml</includes>
      </artifactItem>
    </artifactItems>
    <overWriteReleases>true</overWriteReleases>
    <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
    </execution>

  </executions>
</plugin>

问题

我应该在子项目的 pom.xml 文件中添加什么以便只执行所需的执行?

如果我简单地输入

  <build>
    <plugins>
...
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
      </plugin>
...
    </plugins>
  </build>

然后两次执行都会发生。在“战争”包中,没关系,这就是我想要发生的事情,但在“通用代码”包中,我只希望发生“解包模式”执行。

我见过this question,但它从来没有真正得到回答,并且评论的解决方案(运行多个构建)只是不适合我的环境。这将经历持续集成/构建,因此必须直接编译。无需脚本,无需多次构建。

【问题讨论】:

    标签: java maven xsd wsdl maven-dependency-plugin


    【解决方案1】:

    很简单,大部分插件都有&lt;skip/&gt;配置项,依赖插件也不例外!

    所以,你可以用&lt;skip/&gt;覆盖公共代码的pom来禁用某个执行!例如:

     <execution>
      <id>unpack-schema</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <skip>true</skip>
      </configuration>
    </execution>
    

    顺便说一句:给您的提示:对于多模块项目,在父 pom.xml 中配置的插件将在所有子模块上运行,除非您在子模块的 pom.xml 中使用 ! 禁用它

    祝你好运!

    【讨论】:

    • 完美!正是我需要的!我将'skip'设置放在父pom中,以及默认属性'skip.unpack.binding'设置'false',这样在大多数情况下都不会被跳过。然后我在那些我不希望 unpack-common-binding 执行运行的地方将“skip.unpack.binding”覆盖为“true”。
    猜你喜欢
    • 2017-07-14
    • 2016-02-06
    • 2012-07-08
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多