How to disable jar creation in commandline in a maven project?

不问归期 提交于 2021-02-11 16:31:22

问题


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

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.

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.


回答1:


The maven-jar-plugin does not provide any skip option.

However, several ways are possible to achieve your requirement.

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

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.

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>

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

mvn clean install -Djar.creation=false

Would instead skip the creation of the jar.

What we are actually doing:

  • We are re-defining the default execution of the maven-jar-plugin
  • We are overriding its execution id, as such getting more control over it
  • We are placing its execution phase binding to a configurable (via property) phase
  • Default phase (property value) keeps on being package
  • At command line time you can still change it to any value different than a standard maven phase. That is, -Djar.creation=none would also work.


来源:https://stackoverflow.com/questions/37722967/how-to-disable-jar-creation-in-commandline-in-a-maven-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!