maven pom.xml积累

1、多模块项目依赖管理与依赖继承

1.1、指定父模块与默认继承

子项目pom.xml中添加如下的父依赖

<parent>
		<groupId>com.w3c.shop</groupId>
		<artifactId>shop-parentpom</artifactId>
		<version>1.19.30.05</version>
	</parent>

子项目中dependencies只添加父依赖没有添加的即可,因为子项目会从父项目中继承所有依赖项(全部继承)

1.2、依赖管理

dependencyManagement只是声明依赖,并不实际引入,因此子项目需要显式的用dependency声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
父pom.xml

<?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.w3c.shop</groupId>
    <artifactId>shop-parentpom</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>shop-Dao</module>
        <module>shop-Service</module>
    </modules>


    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>RELEASE</spring.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
</project>

子pom.xml

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

     <!--声明自己的父模块,将继承父模块的所有依赖-->
    <groupId>com.w3c.shop</groupId>
	<artifactId>shop-parentpom</artifactId>
	<version>1.19.30.05</version>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>shop-Dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
            <!--版本是从父模块依赖过来的properties-->
        </dependency>
    </dependencies>

</project>

这样的父模块与子模块都并没有依赖junit了,这时的依赖只是用于管理,并没有真正依赖。
dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。
使用dependencyManagement声明的依赖即不会引入依赖,也不会给他的子模块引入依赖。但这段配置是可以继承的。
在子类中,依赖配置较原来就简单了。可以在子类中只配置groupId和artifactId ,省去了version。因为完整的依赖声明已经包含在父POM中。 这样可以统一项目范围中依赖的版本,帮助降低依赖冲突的几率。
如果子模块不声明依赖的使用,即使该依赖已经在父POM的dependencyManangement中声明了,也不会产生任何实际的效果。
如果想要在某个模块中使用和另一个模块中完全一样的dependencyManagement配置,除了赋值和继承外,还可以使用import范围依赖将这一配置导入。
我们要达到的目的是:父模块作版本管理不实际依赖,子模块按需依赖。

标准demo

父pom.xml

<?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.w3c.shop</groupId>
	<artifactId>shop-parentpom</artifactId>
	<version>1.19.30.05</version>
    <packaging>pom</packaging>
    <modules>
        <module>shop-Dao</module>
        <module>shop-Service</module>
    </modules>
    
    <!--版本管理-->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>RELEASE</spring.version>
    </properties>

    <!--依赖声明-->
    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

子pom.xml

<?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">
    <parent>
        <groupId>com.w3c.shop</groupId>
	    <artifactId>shop-parentpom</artifactId>
	    <version>1.19.30.05</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>shop-Service</artifactId>


    <dependencies>
        <!--按需依赖,版本被父模块控制,可以自行声明,优先级更高-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

父pom.xml中通过dependencyManagement声明junit依赖,并且指定版本。子pom.xml中显示引入依赖,并且不指定版本,因为父pom.xml中已经声明了junit的版本。

发布了84 篇原创文章 · 获赞 40 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/ljb825802164/article/details/99435620