9

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

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

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

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

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

宇宙人
  • 1,197
  • 3
  • 10
  • 28

4 Answers4

8

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.

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.

Before (Original order):

enter image description here

After (Revised order):

enter image description here

Example:

  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>
    

Output:

    <?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>
Rafael Ibasco
  • 1,342
  • 3
  • 14
  • 19
  • actually I cannot reproduce the problem anymore. I tested that run flatten before shade which succeeded as well – 宇宙人 Apr 29 '20 at 09:39
  • https://stackoverflow.com/users/2977588/%e5%ae%87%e5%ae%99%e4%ba%ba This becomes an issue when you build the modules separately, but if built in the same resolver it doesn't show up. if B -> A and -rf B or -pl :B even though A is in resolver and not built, the issue will show A published pom from .m2/repo and not from resolver. – Greg Domjan Aug 06 '20 at 03:32
  • Noticed that Order in plugin management wasn't enough to resolve, the order that applies is within build list. Often flatten is added in a profile where it may be inappropriate to list shade plugin. So you may have to list it in the pom.xml of the artifact using shade to be able to list both in order as in file 2.Aggregator pom – Greg Domjan Aug 06 '20 at 03:36
4

I faced the same issue with the ${revision} property but using the option <createDependencyReducedPom>false</createDependencyReducedPom> solved my issue. This solution is not working if you need the dependency-reduced pom.

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

PaulT
  • 41
  • 2
1

Well that as fun

This looks to work properly, and flattens the shaded pom file rather than the root

The ordering is pretty imported, even though phase is specified - The installed and released pom and jar are the shaded jar, and the flattened 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>

Marc Magon
  • 713
  • 7
  • 11
-1

the issue for me was the default parameter createDependencyReducedPom=true that modify the relativePath in a bad way with parent artifact that cannot be resolved with CI_friendly_version property (aka ${revision} (see snapshot below)

enter image description here

benGiacomo
  • 67
  • 5