【问题标题】:Maven: resolve versions with flatten without expanding the dependenciesMaven:在不扩展依赖项的情况下使用 flatten 解析版本
【发布时间】:2020-03-30 16:26:16
【问题描述】:

我想在构建之后解析所有的修订标签,所以我正在使用 flatten。我有一个这样的多模块项目:

A (root)
|_B (parent = A, dependencyManagement with version = ${revision}
|_C (parent = B, dependencies declared in dependencyManagement without specifying the version)

问题是在 B 的扁平 pom 中,${revision} 没有解决。此外,在 C 的扁平化 pom 中,该版本仍然缺失,而我希望找到在 B 中的dependencyManagement 中声明的版本。

这是我配置扁平化的方式:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</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>

我尝试在&lt;configuration&gt; 中添加此部分:

<pomElements>
    <dependencyManagement>expand</dependencyManagement>
    <dependencies>expand</dependencies>
</pomElements>

这部分解决了问题,因为它解决了所有版本,但是 pom 变得过于冗长,因为它扩展了父级的所有依赖项。所以结果是C的扁平化pom显式包含了B e A中声明的所有依赖,以及B的dependencyManagement。

有没有办法只解决版本而不扩展子 pom 中的所有依赖项?

【问题讨论】:

    标签: java maven version maven-plugin flatten


    【解决方案1】:

    我解决了这个问题,将&lt;flattenMode&gt;bom&lt;/flattenMode&gt; 添加到我的模块中仅包含dependencyManagement 部分的&lt;configuration&gt; 部分。

    根据Maven Flatten Plugin Documentation

    选项 bom 与 ossrh 类似,但还保留了 dependencyManagement 和属性。特别是它将保持dependencyManagement原样 没有解决父影响和导入范围的依赖关系。 如果您的 POM 代表 BOM(物料清单)并且您 不想按原样部署它(删除父级并解析版本 变量等)。

    您可以在下面看到一个示例

    根模块

     <?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.github.eljaiek.autopilot</groupId>
        <artifactId>autopilot</artifactId>
        <packaging>pom</packaging>
        <version>${revision}</version>
    
        <properties>
            <revision>1.0.0.M1</revision>
            <flatten.mode>clean</flatten.mode>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        </properties>
    
        <modules>
            <module>autopilot-testng-runner</module>
            <module>autopilot-dependencies</module>
        </modules>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>flatten-maven-plugin</artifactId>               
                </plugin>
            </plugins>
    
            <pluginManagement>                    
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>flatten-maven-plugin</artifactId>
                        <version>1.1.0</version>
                        <configuration>
                            <updatePomFile>true</updatePomFile>
                            <flattenMode>${flatten.mode}</flattenMode>
                        </configuration>          
                    <executions>
                        <execution>
                            <id>flatten</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>flatten</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>flatten.clean</id>
                            <phase>clean</phase>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    自动驾驶依赖模块

    <?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>
    
        <parent>
            <artifactId>autopilot</artifactId>
            <groupId>com.github.eljaiek.autopilot</groupId>
            <version>${revision}</version>
        </parent>
    
        <artifactId>autopilot-dependencies</artifactId>
        <packaging>pom</packaging>
    
        <properties>
            <slf4j.version>1.7.26</slf4j.version>
            <flatten.mode>bom</flatten.mode>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>autopilot-testng-runner</artifactId>
                    <version>${project.version}</version>
                    <scope>test</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <version>${slf4j.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.10</version>
                    <scope>provided</scope>
                </dependency>
    
                <!--testng-->
                <dependency>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                    <version>7.0.0</version>
                </dependency>
    
                <!--dependency injection-->
                <dependency>
                    <groupId>com.google.inject</groupId>
                    <artifactId>guice</artifactId>
                    <version>4.2.2</version>
                </dependency>
    
                <dependency>
                    <groupId>com.netflix.governator</groupId>
                    <artifactId>governator</artifactId>
                    <version>1.17.9</version>
                </dependency>
    
                <dependency>
                    <groupId>javax.annotation</groupId>
                    <artifactId>javax.annotation-api</artifactId>
                    <version>1.3.2</version>
                </dependency>
    
                <dependency>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.25.0-GA</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    autopilot-testng-runner 模块

    <?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>
    
        <parent>
            <groupId>com.github.eljaiek.autopilot</groupId>
            <artifactId>autopilot</artifactId>
            <version>${revision}</version>
        </parent>
    
        <artifactId>autopilot-testng-runner</artifactId>
    
        <properties>
            <flatten.mode>oss</flatten.mode>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.netflix.governator</groupId>
                <artifactId>governator</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>autopilot-dependencies</artifactId>
                    <version>${project.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    autopilot-testng-runner pom 扁平化

    <?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>
      <groupId>com.github.eljaiek.autopilot</groupId>
      <artifactId>autopilot-testng-runner</artifactId>
      <version>1.0.0.M1</version>
      <dependencies>
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.10</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>7.0.0</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.google.inject</groupId>
          <artifactId>guice</artifactId>
          <version>4.2.2</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.netflix.governator</groupId>
          <artifactId>governator</artifactId>
          <version>1.17.9</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    </project>
    

    【讨论】:

    • bom 有多个问题:1. 父级的dependencyManagement 未解决2. 父级被删除(所以在孩子的扁平pom 中我完全丢失了有关父级的dependencyManagement 的信息)3. 如果我强制keep 在那里的父级它不会解析其修订标签。目前我正在使用带有 expand 的 bom 但是扁平的 pom 太长了
    • 我尝试挖掘了flatten-maven-plugin的Github代码,我发现这是flattenMode resolveCiFriendliesOnly的错误,它仅在放在根目录时才解析dependencyManagement的修订标签pom.xml。我将很快为改善这种行为的项目做出贡献
    • 我将在我的回答中包含我的 poms,因为我没有遇到任何问题,我仅将 bom 用于我的依赖项模块,oss 用于其他模块。我将所有部门管理都保存在我的 bom 模块中。我可能会帮你解决这个问题
    • 我看到了你的例子,但我想说的是:尝试将 autopilot-dependencies 作为 autopilot-testng-runner 的父级。在这种情况下,testng-runner 是否继承了其父级的dependencyManagement?就我而言,它没有
    • 是的,它甚至继承了根项目的dependencyManagement部分中声明的依赖项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2015-02-07
    • 2012-10-30
    • 2010-12-27
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多