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

时间:2018-09-28 09:30:31

标签: maven maven-shade-plugin

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

我使用class:revisionsha1管理多模块项目的版本。

为了部署可消耗的工件,我使用maven-flatten-plugin生成一个扁平化的pom,使$ {revision}变为实际值。

但是maven-shade-plugin产生的pom减少了,而$ {revision}不变。

如何指定maven-shade-plugin使用扁平化的pom来减少pom。

3 个答案:

答案 0 :(得分:2)

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

我能够通过简单地在flattenshade之间更改执行顺序来解决此问题。您只需要确保flatten运行之后 shade。如果您在父pom中定义了Flatten插件,只需在具有相同执行ID的聚合器项目上添加相同的插件定义。

之前(原始订单):

enter image description here

(修订后的订单)之后:

enter image description here

示例:

  1. 父母项目(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. 聚合器项目(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>

答案 1 :(得分:0)

我在使用$ {revision}属性时遇到了同样的问题,但是使用了 <createDependencyReducedPom>false</createDependencyReducedPom> 解决了我的问题。如果您需要减少依赖项的pom,则此解决方案不起作用。

https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html

答案 2 :(得分:-1)

对我来说,问题是默认参数createDependencyReducedPom = true,该参数使用父构件无法通过CI_friendly_version属性(也称为$ {revision}(请参见下面的快照)解决)以不良方式修改relativePath。

enter image description here

相关问题