最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

maven父子工程中的依賴引用的實(shí)現(xiàn)

 更新時(shí)間:2024年10月29日 09:09:17   作者:峰晨樸樸  
本文主要介紹了Maven父子工程中的依賴引用的實(shí)現(xiàn),包括<dependencyManagement>和<dependencies>兩個(gè)標(biāo)簽的使用,以及父子pom文件的配置,感興趣的可以了解一下

簡(jiǎn)述

項(xiàng)目越來越趨向模塊化開發(fā),使用maven構(gòu)建工程,必然涉及到父子pom的關(guān)聯(lián),父pom文件的父級(jí)又會(huì)繼承springboot項(xiàng)目,就這樣在開發(fā)中踩坑不少,簡(jiǎn)單記錄一下。

看問題之前先了解maven中的兩個(gè)標(biāo)簽<dependencyManagement><dependencies>,明白的直接跳過。

maven標(biāo)簽

1、<dependencyManagement>這里其實(shí)是起到管理依賴jar版本號(hào)的作用,一般只會(huì)在項(xiàng)目的最頂層的pom.xml中使用到,所有子module如果想要使用到這里面聲明的jar,只需要在子module中添加相應(yīng)的groupId和artifactId即可,并不需要聲明版本號(hào),需要注意的是這里面只是聲明一個(gè)依賴,并不是真實(shí)的下載jar,只有在子module中使用到,才會(huì)去下載依賴。

2、<dependencies>我們是這里引入了一個(gè)jar包之后,這里如果沒有加上version版本號(hào)的話,那么maven就會(huì)去<dependencyManagement>里找對(duì)應(yīng)groupId和artifactId的jar,如果有就繼承他,如果沒有就會(huì)報(bào)錯(cuò),這時(shí)候其實(shí)在我們配置的本地倉庫中會(huì)真實(shí)的下載對(duì)應(yīng)的jar包,這時(shí)候所有的子module都會(huì)默認(rèn)繼承這里面所有聲明的jar。

總的來說,就是在中聲明依賴和版本號(hào),該標(biāo)簽中的依賴不會(huì)被子模塊繼承,僅僅是聲明,子pom中直接引入依賴,具體的版本號(hào)會(huì)在父子中去找。

父pom的packaging都是pom,子項(xiàng)目pom的packaging都是jar。關(guān)于在父子配置pom的引用有兩種方案,這里以springboot項(xiàng)目為例說明問題。

第一種pom配置

我們希望在父pom中引入相關(guān)依賴,都記錄在<dependencies>下,子模塊直接繼承父pom的依賴,在子模塊中開發(fā)中就不必再去引入依賴,但在項(xiàng)目中有模塊可能就是單一的工具包,它并不需要springboot的依賴,這時(shí)候啟動(dòng)就會(huì)沖突。可以這樣解決,在父pom中定義springboot版本號(hào),子模塊作為項(xiàng)目啟動(dòng)的模塊配置springboot插件依賴,普通的dao,serivce,common不必引入。如下配置文件,文件中只列舉個(gè)別依賴包,重在說明問題:

父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.demo</groupId>
    <artifactId>demo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>demo-web</module>
        <module>demo-common</module>
        <module>demo-service</module>
    </modules>
    <properties>
        <spring-boot.version>2.1.8.RELEASE</spring-boot.version>
        <java.version>1.8</java.version>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <fastjson.version>1.2.47</fastjson.version>
        <pagehelper.version>5.1.6</pagehelper.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <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>
    <dependencies>
		<!--這兩個(gè)依賴都將被子模塊繼承-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
		<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <!-- 是否默認(rèn) true表示默認(rèn)-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 測(cè)試環(huán)境 -->
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生產(chǎn)環(huán)境 -->
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>
</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>
        <artifactId>demo-api</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.common</groupId>
    <artifactId>demo-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-common</name>
    <description>demo-common project</description>
    <packaging>jar</packaging>
</project>

啟動(dòng)類子模塊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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>demo-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.demo.web</groupId>
    <artifactId>demo-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-web</name>
    <description>demo-web project</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
		<!--引入子模塊相關(guān)jar -->
        <dependency>
            <groupId>com.demo.common</groupId>
            <artifactId>demo-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.myway.service</groupId>
            <artifactId>share-read-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <!--重要 如果不設(shè)置resource 會(huì)導(dǎo)致application.yaml中的@@找不到pom文件中的配置-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
			<!--僅在啟動(dòng)項(xiàng)目中引入springboot插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

第二種pom配置

將所有的依賴在父pom的中聲明,子模塊把需要的都引入一遍:

父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.demo</groupId>
    <artifactId>demo</artifactId>
    <version>3.0.0</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <druid.version>1.1.14</druid.version>
        <pagehelper.boot.version>1.2.5</pagehelper.boot.version>
        <fastjson.version>1.2.70</fastjson.version>
    </properties>
    <!-- 依賴聲明 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot的依賴配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--阿里數(shù)據(jù)庫連接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- pagehelper 分頁插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <modules>
        <module>demo-web</module>
        <module>demo-common</module>
    </modules>
    <packaging>pom</packaging>
    <dependencies>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
	<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <!-- 是否默認(rèn) true表示默認(rèn)-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 測(cè)試環(huán)境 -->
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生產(chǎn)環(huán)境 -->
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>
</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>
        <artifactId>demo</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-system</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>demo-common</artifactId>
        </dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
		</dependency>
    </dependencies>
</project>

啟動(dòng)類子模塊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>
        <artifactId>demo</artifactId>
        <groupId>com.demo</groupId>
        <version>3.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>demo-admin</artifactId>
    <description>
        web服務(wù)
    </description>
    <dependencies>
		<dependency>
			<artifactId>demo</artifactId>
			<groupId>com.demo</groupId>
			<version>1.0.0</version>
		</dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.8.RELEASE</version>
                <configuration>
                    <fork>true</fork> <!-- 如果沒有該配置,devtools不會(huì)生效 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${project.artifactId}</warName>
                </configuration>
           </plugin>
        </plugins>
    </build>
</project>

以上是個(gè)人在構(gòu)建項(xiàng)目中總結(jié)出來的,可供參考,重在理解。

到此這篇關(guān)于maven父子工程中的依賴引用的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)maven父子工程依賴引用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)置換密碼加密解密

    java實(shí)現(xiàn)置換密碼加密解密

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)置換密碼加密解密,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 詳解Spring Boot 使用Spring security 集成CAS

    詳解Spring Boot 使用Spring security 集成CAS

    本篇文章主要介紹了詳解Spring Boot 使用Spring security 集成CAS,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java 基于雪花算法生成分布式id

    Java 基于雪花算法生成分布式id

    SnowFlake 算法(雪花算法), 是Twitter開源的分布式id生成算法。其核心思想就是: 使用一個(gè)64 bit的long型的數(shù)字作為全局唯一id。本文講述Java 基于雪花算法生成分布式id的方法
    2021-06-06
  • Java中常用的設(shè)計(jì)模式之裝飾器模式詳解

    Java中常用的設(shè)計(jì)模式之裝飾器模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java中常用的設(shè)計(jì)模式之裝飾器模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java.lang.Long.parseLong()方法詳解及示例

    Java.lang.Long.parseLong()方法詳解及示例

    這個(gè)java.lang.Long.parseLong(String s) 方法解析字符串參數(shù)s作為有符號(hào)十進(jìn)制長(zhǎng),下面這篇文章主要給大家介紹了關(guān)于Java.lang.Long.parseLong()方法詳解及示例的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 基于spring DI的三種注入方式分析

    基于spring DI的三種注入方式分析

    這篇文章主要介紹了基于spring DI的三種注入方式分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2021-07-07
  • SpringBoot實(shí)現(xiàn)發(fā)送QQ郵件的示例代碼

    SpringBoot實(shí)現(xiàn)發(fā)送QQ郵件的示例代碼

    這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)發(fā)送QQ郵件功能,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • Java如何遠(yuǎn)程調(diào)用對(duì)方接口

    Java如何遠(yuǎn)程調(diào)用對(duì)方接口

    這篇文章主要介紹了Java如何遠(yuǎn)程調(diào)用對(duì)方接口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java微信公眾號(hào)支付開發(fā)之現(xiàn)金紅包

    java微信公眾號(hào)支付開發(fā)之現(xiàn)金紅包

    這篇文章主要為大家詳細(xì)介紹了java微信公眾號(hào)支付開發(fā)之現(xiàn)金紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Spring?Boot條件注解之@ConditionalOnProperty完全解析

    Spring?Boot條件注解之@ConditionalOnProperty完全解析

    這篇文章主要介紹了SpringBoot中的@ConditionalOnProperty注解,通過配置文件屬性值控制Bean或配置類的加載,實(shí)現(xiàn)功能開關(guān)和環(huán)境配置,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02

最新評(píng)論

原阳县| 邹城市| 葫芦岛市| 鄂托克前旗| 丰宁| 崇州市| 安吉县| 榆树市| 望江县| 田林县| 抚松县| 永嘉县| 汉川市| 鄯善县| 临猗县| 滨海县| 滦南县| 外汇| 全南县| 鹤壁市| 英吉沙县| 宜丰县| 报价| 溧水县| 西贡区| 涡阳县| 道孚县| 连州市| 疏勒县| 拜泉县| 义乌市| 乐陵市| 聂拉木县| 满洲里市| 永春县| 青海省| 萍乡市| 海原县| 石狮市| 湾仔区| 东安县|