【问题标题】:Using profiles to exclude WAR modules from 'mvn install'使用配置文件从“mvn install”中排除 WAR 模块
【发布时间】:2017-01-02 04:25:42
【问题描述】:

我的父 pom 定义了 7 个模块,其中 5 个是依赖 jars,两个是依赖于这些 jar 的 wars。

问题:是否可以使用maven profiles(或其他解决方案)来定义在针对父pom运行mvn install以排除两个war包时包含哪些模块?

然后我想有一个不同的配置文件(或另一个解决方案)来打包这两个战争。如果运行该配置文件,则仅当存储库中缺少依赖项 jar 模块时才应重新构建和安装它们。

【问题讨论】:

  • 你考虑过mvn --projects Module --also-make-dependent吗?

标签: maven maven-profiles maven-install-plugin


【解决方案1】:

您可以使用父文件pom.xml 中的build-helper-maven-plugin 来创建基于packaging 的新属性(在运行时,它会从父文件的pom 更改为jarwar对于模块)。然后可以使用这个新属性来动态跳过maven-install-plugin

一个简单的例子:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>build-helper-regex-is-packaging-war</id>
            <phase>validate</phase>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>only.when.war.is.used</name>
                <value>${project.packaging}</value>
                <regex>war</regex>
                <replacement>true</replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <skip>${only.when.war.is.used}</skip>
    </configuration>
</plugin>

这样做时,动态 ${only.when.war.is.used} 属性将设置为 true,仅当 project.packaging 具有值 war 并因此通过其 skip 选项有效地跳过 maven-install-plugin 执行。


然后,您可以将此行为移至配置文件,并为 jarwar 设置不同的设置,将它们保持在一个共同的位置:根 pom.xml,这要归功于它们的动态行为。


关于检测是否已安装工件的能力,official 插件文档中没有这样的选项,我认为您不会仅仅使用插件就可以有这样的行为。

但是,您可以使用maven profile 激活机制以防文件丢失(已安装的文件)并相应地激活配置文件。

您可以以动态方式(仅基于标准属性)采用以下方法:

<profiles>
  <profile>
    <activation>
      <file>
        <missing>${settings.localRepository}/${project.groupId}/${project.artifactId}/${project.build.fileName}.${project.packaging}</missing>
      </file>
    </activation>
    ...
  </profile>
</profiles>

【讨论】:

  • @amphibient 你解决了这个问题吗?答案对你有帮助吗?
猜你喜欢
  • 2015-02-09
  • 2016-06-17
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多