如何动态跳过maven antrun copy/shade目标



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

所以我已经组织了一个SuperPom中的共享资源。

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

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

我试过mvn clean install -Dmaven.shade.skip=true, mvn clean install -Dmaven.copy.skip=true, mvn 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>

您是否尝试在super-pom中将maven-shade-plugin的阶段设置为none,然后在客户端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>

在需要它的儿童诗歌中:

<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>

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

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

这种方式默认激活,除非您添加-DskipShade-DskipShade=true

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下的user's 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。配置文件到你的子项目模板,默认情况下为项目预定义阴影。(需要一个用于预定义公司标准的CVS模板。)

使用maven的方法。如果你的一些团队成员在他们的settings.xml文件中没有配置文件,那么Config是有用的,并且你必须注意大多数情况下着色将被完成。

.mvn/maven.config:

-Pshading

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

请注意如果你正在使用maven。Intellij(2020.2.2)中的.mvn/maven. config文件:配置文件必须存在于root aggregator pom文件夹的子目录下。从IDE构建子项目不尊重.mvn/maven。当前在子项目级别上的配置文件。从子项目文件夹中的命令行运行mvn命令将尊重这两个子项目。mvn/maven。

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

skip选项是在版本3.3.0的shade插件中引入的,所以现在可以动态地跳过,例如,使用属性:

  <properties>
    ....
    <skipShaded>true</skipShaded>
  </properties>
  ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <skip>${skipShaded}</skip>
              ...
            </configuration>
          </execution>
        </executions>
      </plugin>

在上面的代码中,默认是跳过,并且可以通过将-DskipShaded=false传递给mvn来覆盖它。

最新更新