【问题标题】:Use maven-flatten-plugin and maven-shade-plugin at the same time同时使用 maven-flatten-plugin 和 maven-shade-plugin
【发布时间】:2019-03-04 06:34:46
【问题描述】:

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

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

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

但是 maven-shade-plugin 会生成一个简化的 pom,但 ${revision} 不变。

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

【问题讨论】:

  • 我也有同样的问题。

标签: maven maven-shade-plugin


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    之前(原订单):

    之后(修改顺序):

    示例:

    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>
    

    【讨论】:

    • 实际上我无法再重现该问题。我测试了在阴影之前运行变平,也成功了
    • stackoverflow.com/users/2977588/%e5%ae%87%e5%ae%99%e4%ba%ba 当您单独构建模块时,这会成为一个问题,但如果构建在同一个解析器中,它不会出现。如果 B -> A 和 -rf B 或 -pl :B 即使 A 在解析器中且未构建,问题也会显示来自 .m2/repo 而不是来自解析器的已发布 pom。
    • 注意到插件管理中的顺序不足以解决,适用的顺序在构建列表中。通常在配置文件中添加 flatten 可能不适合列出 shade 插件。因此,您可能必须使用 shade 在工件的 pom.xml 中列出它,以便能够按文件 2.Aggregator pom 中的顺序列出两者
    【解决方案3】:

    对我来说,问题是默认参数 createDependencyReducedPom=true 以错误的方式修改 relativePath,父工件无法通过 CI_friendly_version 属性(又名 ${revision}(见下面的快照)解决)

    【讨论】:

    • 请分享实际代码而不是截图^^
    【解决方案4】:

    那么有趣

    这看起来工作正常,并展平了阴影 pom 文件而不是根

    即使指定了阶段,订单也很重要 - 安装和发布的pom和jar分别是shaded jar和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>
    
    

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 2013-08-19
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多