Maven: 使用"flatten"解决版本冲突问题而不扩展依赖。

6
我想在构建后解决所有修订标签,所以我使用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>

我尝试将这个部分添加到<configuration>标签内:

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

这在一定程度上解决了问题,因为它解决了所有版本的问题,但pom文件过于冗长,因为它扩展了所有父级依赖。因此,结果是C的平面化pom明确包含B和A中声明的所有依赖项以及B的dependencyManagement。

有没有办法仅仅解决版本问题,而不必扩展子pom中的所有依赖关系?

1个回答

3
我将<flattenMode>bom</flattenMode> 添加到仅包含dependencyManagement部分的模块的<configuration>部分,解决了此问题。
根据Maven Flatten Plugin文档,选项bom类似于ossrh,但还保留dependencyManagement和properties。它特别保留dependencyManagement而不解析父级影响和import-scoped依赖项。如果你的POM代表一个BOM(材料清单),并且不想按原样部署它(去除parent和解析版本变量等),这非常有用。下面是一个示例:
根模块:
 <?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>
< p > 自动驾驶-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>

自动驾驶-TestNG-运行程序的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. 如果我通过<parent>keep</parent>强制保留父级,则无法解析其修订标签。 目前我正在使用带有<parent>expand</parent>的bom,但扁平化的pom太长了。
- robertobatts
我尝试深入研究flatten-maven-plugin的Github代码,发现这是一个flattenMode resolveCiFriendliesOnly的错误,只有在放置在根pom.xml上时才解决dependencyManagement的修订标签。我将很快为该项目做出贡献,以改善此行为。 - robertobatts
我会在我的回答中包含我的pom文件,因为我没有遇到任何问题,我只为我的依赖模块使用bom,而为其他模块使用oss。我将所有的依赖管理都放在我的bom模块中。这可能会帮助你克服这个问题。 - eHayik
我看到了你的例子,但我的意思是:尝试将autopilot-dependencies作为autopilot-testng-runner的父级。在这种情况下,testng-runner是否继承其父级的dependencyManagement?在我的情况下,它没有继承。 - robertobatts
是的,它确实会继承根项目中dependencyManagement部分声明的依赖项。 - eHayik
显示剩余3条评论

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