首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven:在不扩展依赖项的情况下使用平面解析版本

Maven:在不扩展依赖项的情况下使用平面解析版本
EN

Stack Overflow用户
提问于 2019-12-05 17:16:25
回答 1查看 3.6K关注 0票数 6

我想要解决所有的修订标签后,构建,所以我使用扁平。我有一个像这样的多模块项目:

代码语言:javascript
复制
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中找到声明的版本。

我就是这样配置扁平化的:

代码语言:javascript
复制
    <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>中添加这个部分

代码语言:javascript
复制
<pomElements>
    <dependencyManagement>expand</dependencyManagement>
    <dependencies>expand</dependencies>
</pomElements>

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

是否有一种方法只解决版本而不扩展子pom中的所有依赖项?

EN

回答 1

Stack Overflow用户

发布于 2020-01-09 09:43:06

我解决了这个问题,在我的模块中将<flattenMode>bom</flattenMode>添加到<configuration>部分,只包含dependencyManagement部分。

根据Maven扁平插件文档

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

你可以看到下面的一个例子

根模块

代码语言:javascript
复制
 <?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>

自动驾驶仪-依赖项模块

代码语言:javascript
复制
<?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>

自动驾驶仪测试转轮模块

代码语言:javascript
复制
<?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>

自动驾驶仪-测试转轮pom扁平

代码语言:javascript
复制
<?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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59200229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档