【问题标题】:How to skip the maven antrun copy/shade:shade goal dynamically?如何动态跳过 maven antrun copy/shade:shade 目标?
【发布时间】:2014-07-26 09:27:45
【问题描述】:

我正在使用 maven 来配置由多个小服务组成的应用程序。大部分用java开发的服务共享相同的maven配置,在相同的构建生命周期中,一些共享资源(如spring AMQP)。

所以我在 SuperPom 中组织了共享资源。

虽然 shade 插件似乎并没有真正干扰安装过程,但 antrun 插件当然不会找到它应该复制的任何文件,因为 shade 插件没有创建任何 jar 文件。

由于我希望在 SuperPom 中抽象 shade/antrun 插件的配置,我需要跳过 shade/copy 目标。

我试过mvn clean install -Dmaven.shade.skip=truemvn clean install -Dmaven.copy.skip=truemvn clean install -Dmaven.shade.shade.skip=true

这里有一个小样本供你玩:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Test</groupId>
    <artifactId>SuperTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <log4j.version>1.2.17</log4j.version>
        <destination>pleasedeleteme</destination>
        <mainpackage>com.uk.cc.it.info.gov.test.xxx</mainpackage>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${mainpackage}.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${groupId}</groupId>
                                    <artifactId>${artifactId}</artifactId>
                                    <version>${version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${destination}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>

</project>

【问题讨论】:

  • 现在是 2021 年,这里的帖子是 2014 年的,Shade Plugin 的作者仍然是arguing about adding a skip flag,因为他奇怪地认为这是一种代码气味。所以下面的技巧之一是唯一的方法。

标签: java maven maven-shade-plugin maven-antrun-plugin maven-lifecycle


【解决方案1】:

您是否尝试在超级 pom 中将 maven-shade-plugin 的阶段设置为 none,然后在客户端 pom 中覆盖它?

所以在父 pom 中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>shade</id>
            <phase>none</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- ... -->
            </configuration>
        </execution>
    </executions>
</plugin>

在需要它的子 poms 中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <!-- no need to specify version -->
    <executions>
        <execution>
            <id>shade</id>
            <phase>package</phase>
            <!-- no need to specify configuration -->
        </execution>
    </executions>
</plugin>

【讨论】:

    【解决方案2】:

    maven-shade-plugin 没有要跳过的parameter。通常,shade-plugin 不仅仅是为了好玩,所以你可能想知道你是否真的想跳过这个。如果您认为它仍然有效,则必须创建一个激活配置文件,如下所示:

    <activation>
      <property>
        <name>skipShade</name>
        <value>!true</value>
      </property>
    </activation>
    

    这种方式默认是激活的,除非你加-DskipShade或者-DskipShade=true

    【讨论】:

    • 尽管您的帖子很有帮助,但您是否愿意阅读我的帖子?我想将构建过程抽象为 SuperPom,因为它对所有服务都是相同的。但是当我在其中包含 shade/antrun 插件时,我无法安装 SuperPom。需要明确的是:SuperPom 只是其他人继承的单个 pom 文件。那里没有什么可以遮蔽、打包或复制的东西......
    【解决方案3】:

    Maven 3.6.1 为您提供了一种新方法。

    在 superPom 中,您可以为您的着色配置定义配置文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Test</groupId>
    <artifactId>SuperTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <log4j.version>1.2.17</log4j.version>
        <destination>pleasedeleteme</destination>
        <mainpackage>com.uk.cc.it.info.gov.test.xxx</mainpackage>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${groupId}</groupId>
                                    <artifactId>${artifactId}</artifactId>
                                    <version>${version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${destination}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    
    <profiles>
        <profile>
            <id>shade</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.2</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <transformers>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>${mainpackage}.Main</mainClass>
                                        </transformer>
                                    </transformers>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    在 .m2 下的用户 settings.xml 中,您可以添加相同 id 的配置文件以启用您的 superPom 的阴影配置文件配置。这使您可以选择从 IDE 内部简单地切换阴影,例如 Intellij IDEA(仅在 Intellij 中测试)。

    <settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://maven.apache.org/SETTINGS/1.0.0"
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
    
        ...
    
        <!-- toggle shading from inside Intellij IDEA -->
        <profiles>
            <profile>
                <id>shade</id>
            </profile>
        </profiles>
    
        <!-- Shade Profile has to be activeProfile to be 
        able to explicitly disable shading -->
        <activeProfiles>
            <activeProfile>shade</activeProfile>
        </activeProfiles>
    </settings>
    

    在子项目中,您可以将 .mvn/maven.config 文件添加到子项目模板中,以默认为项目预定义阴影。 (需要用于预定义公司标准的 CVS 模板。)

    如果您的一些团队成员在他们的 settings.xml 文件中没有配置文件,并且您必须注意大部分时间将完成着色,则使用 maven.config 的方法很有用。

    .mvn/maven.config:

    -Pshading
    

    配置文件也可以在默认情况下使用 jenkinsfile 为 Jenkins 通过传递 -Pshade 来激活。它将覆盖 maven.config 设置。禁用使用 -P!shade

    如果您在 Intellij (2020.2.2) 中使用 maven.config 文件,请注意:.mvn/maven.config 文件必须存在于 root aggregator pom 文件夹的子目录中。目前,从 IDE 构建子项目不尊重子项目级别的 .mvn/maven.config 文件。从子项目文件夹中的命令行运行 mvn 命令将重复子项目 .mvn/maven.config 和父项目 .mvn/maven.config。

    【讨论】:

    • 没错,但是像这样为每个插件制作配置文件是使用大锤来完成钉子。将它绑定到 none 阶段会很容易。
    【解决方案4】:

    禁用 maven shade 插件对我有用。在我禁用 Maven 阴影插件之前,构建是试图生成减少依赖的 pom 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多