如何在 Maven 项目的命令行中禁用 jar 创建? [英] How to disable jar creation in commandline in a maven project?

查看:77
本文介绍了如何在 Maven 项目的命令行中禁用 jar 创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Maven 项目,我正在运行两个单独的构建.

I have a maven project for which I'm running two separate builds.

在一个构建中,我想通过在其中禁用 jar 创建 Maven 模块来节省构建时间.(有 45 个 Maven 模块).有一个 Maven-Jar-Plugin 用于创建 jar.

In one build I want to save the build time by disabling the jar creation of maven modules in it.(There are 45 maven modules). There is a Maven-Jar-Plugin that is being used to create the jars.

我想在命令行有条件地禁用jar创建,即寻找类似于-Dskiptests的东西,用于跳过单元测试,尽管默认情况下有一个surefire插件.>

I want to conditionally disable the jar creation at the command line, that is, looking for something similar to -Dskiptests used to skip the unit tests though there is a surefire plugin by default.

推荐答案

maven-jar-plugin 不提供任何跳过选项.

但是,有多种方法可以满足您的要求.

However, several ways are possible to achieve your requirement.

您可以跳过阶段默认情况下带来(通过默认映射) jar 创建,即 package 阶段,因此只需调用

You may just skip the phase which brings by default (via default mappings) the jar creation, that is, the package phase, and as such simply invoke

mvn clean test

如果您无论如何都不创建 jar 文件,那么额外的阶段将没有意义:packageinstalldeploy 将没有任何内容处理.此外,其他集成阶段也可能受到影响,具体取决于您的集成测试策略(如果有).

The additional phases would not make sense if you do not create a jar file anyway: package, install, deploy would not have anything to process. Moreover, the additional integration phases may also be impacted depending on your strategy for integration tests, if any.

或者,您可以按如下方式配置您的 pom:

Alternatively, you can configure your pom as following:

<properties>
    <jar.creation>package</jar.creation>
</properties>

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>${jar.creation}</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

因此,默认行为仍会提供 jar 创建,同时执行 maven 如下:

As such, the default behavior would still provide a jar creation, while executing maven as following:

mvn clean install -Djar.creation=false

而是跳过 jar 的创建.

Would instead skip the creation of the jar.

我们实际在做什么:

  • 我们正在重新定义 maven-jar-plugin
  • 的默认执行
  • 我们正在覆盖它的执行 ID,因此可以更好地控制它
  • 我们将其执行阶段绑定到可配置(通过属性)阶段
  • 默认阶段(属性值)保持为package
  • 在命令行时,您仍然可以将其更改为与标准 maven 阶段不同的任何值.也就是说,-Djar.creation=none 也可以.

这篇关于如何在 Maven 项目的命令行中禁用 jar 创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆