覆盖多个级别的POM中的依赖项/插件管理

时间:2019-01-10 06:59:47

标签: java maven maven-3

我正在具有3级结构的Maven环境中工作:

  

Maven项目POM结构
  公司POM
  --项目POM
  ----模块1 POM
  ----模块... POM

公司POM定义了dependencyManagment和pluginManagement。这些默认值在公司级别维护。在项目POM中,我想覆盖项目POM中的某些默认值,并将其用于我的所有模块。

问题: 是否可以通过公司POM利用项目POM中的dependencyManagement和pluginManagement设置?

如果我需要详细说明问题或提供更多信息,请相应地指导我!

“项目POM”->是指公司POM。

<?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.company</groupId>
    <artifactId>company-pom</artifactId>
    <version>6.0.2</version>
</parent>

<groupId>com.project</groupId>
<artifactId>project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<!-- Properties -->
<properties>
    <!-- Settings -->
    <java.version>11</java.version>
    <maven.version>3.6.0</maven.version>

    <!-- Project Configuration -->
    <project.encoding>UTF-8</project.encoding>
    <project.gitlab.connection>...</project.gitlab.connection>
    <project.artifactory.releases>...</project.artifactory.releases>
    <project.artifactory.snapshots>...</project.artifactory.snapshots>

    <!-- Version Control -->
    <spring.version>5.1.3.RELEASE</spring.version>
    <spring-boot.version>2.1.1.RELEASE</spring-boot.version>

</properties>

<!-- Source Control Management -->
<scm>
    <connection>scm:git:${project.gitlab.connection}</connection>
    <developerConnection>scm:git:${project.gitlab.connection}</developerConnection>
    <tag>HEAD</tag>
</scm>

<!-- Distribution Management -->
<distributionManagement>
    <repository>
        <id>artifactory</id>
        <url>${project.artifactory.releases}</url>
    </repository>
    <snapshotRepository>
        <id>artifactory</id>
        <url>${project.artifactory.snapshots}</url>
    </snapshotRepository>
</distributionManagement>

<!-- Local Dependencies -->
<!-- Project Dependencies -->
<dependencyManagement>
    <dependencies>
        <!-- Spring Dependency Management -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Spring Boot Dependency Management -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <!-- Local Plugins -->

    <!-- Project Plugins -->
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>${java.version}</release>
                    <encoding>${project.encoding}</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0-M2</version>
                <configuration>
                    <rules>
                        <requireJavaVersion>
                            <version>${java.version}</version>
                        </requireJavaVersion>
                        <requireMavenVersion>
                            <version>${maven.version}</version>
                        </requireMavenVersion>
                    </rules>
                </configuration>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<modules>
    <module>module1</module>
    <module>module2</module>
</modules>

</project>

我要使用公司POM中指定的插件/ spring版本的模块POM。

<?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.project</groupId>
        <artifactId>project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>module1</artifactId>
    <packaging>jar</packaging>    

    <dependencies>
        <!-- Dependencies - Spring -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Dependencies - Application -->
        <dependency>
            <groupId>org.javamoney</groupId>
            <artifactId>moneta</artifactId>
            <version>1.3</version>
            <type>pom</type>
        </dependency>
        <!-- Dependencies - Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- Local Plugins -->
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>versions-maven-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- Global Plugins -->
    </build>

</project>

1 个答案:

答案 0 :(得分:0)

问题:是否可以通过公司POM利用项目POM中的dependencyManagement和pluginManagement设置?

您可以这样做吗?是的,可以同时指定依赖项/插件管理以及实际的/来覆盖父设置。

您应该这样做吗?否,这使得POM管理的方法非常“不干净”,并且违背了最初使用dependencyManagement和pluginManagement的逻辑。