同时使用 maven-flatten-plugin 和 maven-shade-plugin [英] Use maven-flatten-plugin and maven-shade-plugin at the same time

查看:104
本文介绍了同时使用 maven-flatten-plugin 和 maven-shade-plugin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何同时使用maven-flatten-plugin和maven-shade-plugin?

How to use the maven-flatten-plugin and maven-shade-plugin at the same time?

我使用 revision,sha1,changelist 来管理多模块项目的版本.

I use revision,sha1,changelist to manage the versions of a multi-module project.

为了部署 cosumable artifacts,我使用 maven-flatten-plugin 生成一个扁平的 pom,使 ${revision} 具有实际价值.

In order to deploy cosumable artifacts, I use maven-flatten-plugin to generate a flattened pom which make ${revision} to real value.

但是 maven-shade-plugin 在 ${revision} 不变的情况下产生了一个减少的 pom.

But the maven-shade-plugin produces a reduced pom with ${revision} unchanged.

如何指定maven-shade-plugin使用flattened pom来减少pom.

How to specify the maven-shade-plugin to use the flattened pom to reduce the pom.

推荐答案

我今天遇到了同样的问题,但我在网上找不到真正的解决方案.虽然 PaulT 的建议可能对某些人有用,但我发现这是不可接受的,因为尽管将 设置为 true,但生成的 pom 中仍未包含传递依赖项.

I experienced this same issue today and I found no real solution on the web. While PaulT's suggestion might work for some, I found this unacceptable since the transitive dependencies were still not being included in the generated pom despite the setting of <promoteTransitiveDependencies> to true.

我可以通过简单地改变 flattenshade 之间的执行顺序来解决这个问题.您只需要确保 flatten 运行 AFTER shade.如果您在父 pom 中定义了 flatten 插件,只需在您的聚合器项目中添加相同的插件定义,并使用相同的执行 ID.

I was able to fix this by simply changing the order of execution between flatten and shade. You just need to make sure that flatten runs AFTER shade. If you have defined the flatten plugin in your parent pom, simply add the same plugin definition on your aggregator project with the same execution id.

之前(原订单):

之后(修改后的顺序):

示例:

  1. 父项目 (POM)

  1. Parent Project (POM)

<?xml version="1.0" encoding="UTF-8"?>
<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>com.ibasco.test</groupId>
    <artifactId>ucgd-parent</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>

    <properties>
        <revision>2.0.0-alpha</revision>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>module-one</module>
        <module>module-two</module>
        <module>module-three</module>
        <module>assembly</module>
    </modules>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>flatten-maven-plugin</artifactId>
                    <version>1.1.0</version>
                    <configuration>
                        <updatePomFile>true</updatePomFile>
                        <flattenMode>resolveCiFriendliesOnly</flattenMode>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <!-- Flatten -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>package</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

  • 聚合器项目 (POM)

  • Aggregator Project (POM)

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>ucgd-parent</artifactId>
            <groupId>com.ibasco.test</groupId>
            <version>${revision}</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>ucg-display</artifactId>
        <packaging>jar</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.ibasco.test</groupId>
                <artifactId>module-two</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ibasco.test</groupId>
                <artifactId>module-one</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <!-- A little workaround to disable the jar warning -->
                        <classesDirectory>src</classesDirectory>
                        <excludes>
                            <exclude>**</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <!-- Javadoc -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>aggregate-javadocs</id>
                            <phase>package</phase>
                            <goals>
                                <goal>aggregate-jar</goal>
                            </goals>
                            <configuration>
                                <includeDependencySources>true</includeDependencySources>
                                <dependencySourceIncludes>
                                    <dependencySourceInclude>com.ibasco.test:*</dependencySourceInclude>
                                </dependencySourceIncludes>
                                <dependencySourceExcludes>
                                    <dependencySourceExclude>com.ibasco.test:module-three</dependencySourceExclude>
                                </dependencySourceExcludes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- Shade plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <createSourcesJar>true</createSourcesJar>
                                <shadedArtifactAttached>false</shadedArtifactAttached>
                                <createDependencyReducedPom>true</createDependencyReducedPom>
                                <!-- Make sure the transitive dependencies are written to the generated pom under <dependencies> -->
                                <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
                                <artifactSet>
                                    <includes>
                                        <include>com.ibasco.test:module-one</include>
                                        <include>com.ibasco.test:module-two</include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- Flatten -->
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>flatten-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>flatten</id>
                            <phase>package</phase>
                            <goals>
                                <goal>flatten</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

  • 输出:

        <?xml version="1.0" encoding="UTF-8"?>
        <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                <groupId>com.ibasco.test</groupId>
                <artifactId>ucgd-parent</artifactId>
                <version>2.0.0-alpha</version>
            </parent>
            <groupId>com.ibasco.test</groupId>
            <artifactId>ucg-display</artifactId>
            <version>2.0.0-alpha</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>3.9</version>
                    <scope>compile</scope>
                    <optional>false</optional>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <classesDirectory>src</classesDirectory>
                            <excludes>
                                <exclude>**</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>aggregate-javadocs</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>aggregate-jar</goal>
                                </goals>
                                <configuration>
                                    <includeDependencySources>true</includeDependencySources>
                                    <dependencySourceIncludes>
                                        <dependencySourceInclude>com.ibasco.test:*</dependencySourceInclude>
                                    </dependencySourceIncludes>
                                    <dependencySourceExcludes>
                                        <dependencySourceExclude>com.ibasco.test:module-three</dependencySourceExclude>
                                    </dependencySourceExcludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-shade-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <createSourcesJar>true</createSourcesJar>
                                    <shadedArtifactAttached>false</shadedArtifactAttached>
                                    <createDependencyReducedPom>true</createDependencyReducedPom>
                                    <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
                                    <artifactSet>
                                        <includes>
                                            <include>com.ibasco.test:module-one</include>
                                            <include>com.ibasco.test:module-two</include>
                                        </includes>
                                    </artifactSet>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>flatten-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>flatten</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>flatten</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </project>
    

    这篇关于同时使用 maven-flatten-plugin 和 maven-shade-plugin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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