同时使用maven-flatten-plugin和maven-shade-plugin

11
如何同时使用maven-flatten-plugin和maven-shade-plugin?
我使用revisionsha1changelist来管理多模块项目的版本。
为了部署可消费的构件,我使用maven-flatten-plugin生成一个扁平化的pom文件,将${revision}转换为实际值。
但是,maven-shade-plugin会生成一个减小的pom文件,其中的${revision}保持不变。
如何指定maven-shade-plugin使用扁平化的pom文件来减小pom文件。

我有同样的问题。 - Juan Lopes
4个回答

8
今天我遇到了同样的问题,但在网上找不到真正的解决方案。虽然PaulT的建议可能对某些人有效,但我发现这是不可接受的,因为尽管将<promoteTransitiveDependencies>设置为true,但仍未在生成的pom中包含传递性依赖项。
我能够通过简单地改变flattenshade之间的执行顺序来解决这个问题。你只需要确保flattenshade之后运行即可。如果你已经在父pom中定义了flatten插件,只需在聚合器项目中添加相同的插件定义和相同的执行ID即可。 改变前(原始顺序): enter image description here 改变后(修改后的顺序): enter image description here 示例:
  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>
    
  2. 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>

实际上我现在无法再复现这个问题了。我测试了先运行flatten再运行shade的方法,也成功了。 - 宇宙人
https://stackoverflow.com/users/2977588/%e5%ae%87%e5%ae%99%e4%ba%ba当你分别构建模块时,这会成为一个问题,但如果在同一个解析器中构建,则不会显示出来。 如果B -> A并且-rf B或-pl:B即使A在解析器中而没有构建,问题也会显示A从.m2 / repo发布的pom而不是从解析器中。 - Greg Domjan
注意到插件管理中的顺序不足以解决问题,适用的顺序在构建列表内部。 通常情况下,flatten会被添加到一个配置文件中,但在其中列出shade插件可能是不合适的。 因此,您可能需要在使用shade的构件的pom.xml中列出它,以便能够按照文件2中的顺序列出两者。聚合器pom - Greg Domjan

4

1

很有趣

看起来可以正常工作,并且将阴影的POM文件平铺而不是根目录

顺序非常重要,即使指定了阶段 - 安装和发布的pom和jar都是阴影的jar,以及平铺的pom

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <id>shade</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.2.5</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenedPomFilename>flatter.pom</flattenedPomFilename>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten22</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean22</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


<plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>copy-file</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <sourceFile>flatter.pom</sourceFile>
                            <destinationFile>dependency-reduced-pom.xml</destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


-1

对我来说问题出在默认参数createDependencyReducedPom=true,它以一种不好的方式修改了相对路径,导致父级工件无法通过CI_friendly_version属性(也就是${revision},如下面所示)解决

enter image description here


请分享实际的代码而不是它的截图 ^^ - finnmglas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接