java - Maven:在不扩展依赖项的情况下使用 flatten 解析版本

标签 java maven version maven-plugin flatten

我想在构建之后解析所有的修订标签,所以我正在使用 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 e A 中声明的所有依赖项,以及 B 的dependencyManagement。

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

最佳答案

我通过添加 <flattenMode>bom</flattenMode> 解决了这个问题至<configuration>我的模块中仅包含 dependencyManagement 的部分部分。

根据Maven Flatten Plugin Documentation

Option bom is Like ossrh but additionally keeps dependencyManagement and properties. Especially it will keep the dependencyManagement as-is without resolving parent influences and import-scoped dependencies. This is useful if your POM represents a BOM (Bill Of Material) and you do not want to deploy it as is (to remove parent and resolve version variables, etc.).



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

根模块
 <?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>

关于java - Maven:在不扩展依赖项的情况下使用 flatten 解析版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200229/

相关文章:

linux - curl 响应显示 "HTTP version not supported",错误 505

Java JRebel 与 MyRebel 许可证插件

java - 如何解决 Product 中的以下错误 Product() 无法应用?

java - 当我尝试使用 JSON 数据时,POST 到 Jersey REST 服务时出现错误 415 不支持的媒体类型

java - 在 Maven 中重命名 groupId

替换一个 dll 时的 .net dll 版本问题

java - 与SVN服务器交互的API?

java - 使用 Stream API Java 8 进行嵌套对象转换

java - Maven 构建配置文件激活

iphone - 在不同版本的操作系统上测试 iPhone 软件