Maven:在不扩展依赖关系的情况下使用拼合来解决版本

问题描述 投票:1回答:1

我想在构建后解析所有修订标签,所以我正在使用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中的所有依赖项?

java maven version maven-plugin flatten
1个回答
0
投票

我解决了此问题,为仅包含<flattenMode>bom</flattenMode>部分的模块将<configuration>添加到dependencyManagement部分中。

根据Maven Flatten Plugin Documentation

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

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

Root module

 <?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>
        <java.version>11</java.version>
        <java.showDeprecation>false</java.showDeprecation>
        <java.showWarnings>false</java.showWarnings>
        <java.optimize>true</java.optimize>
        <java.debug>true</java.debug>
    </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <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>

自动驾驶仪测试仪运行程序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>
© www.soinside.com 2019 - 2024. All rights reserved.