第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Maven中靈活統(tǒng)一添加依賴

標(biāo)簽:
Java

依赖继承

在一个大型Maven项目中,往往包含多个子项目,而这些子项目很有可能都引用了相同的依赖,所以可以在parent pom中进行依赖的设置,以便子项目进行继承,进而减少重复的设置。

例如在parent pom中添加spring-boot-starter-web依赖,那么所有子项目就都获得了这个依赖。

            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>

选择性继承

但是在有些情况下,有可能只有一部分子项目需要这个依赖,其他子项目是不需要的,这时候在parent pom中直接添加dependency就不合适了,因为它会被所有的子项目直接继承。如果我们需要选择性继承,则可以使用profile。

首先,我们在parent pom中添加一个profile,在profile中我们设置依赖。

    <profiles>
        <profile>
            <id>web</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

接下来需要设置何时激活profile,来达到选择性继承的目的。

property activation的bug

profile的激活有很多种,一眼看上去最合适的就是property,官方文档上对它如下说明

property: The profile will activate if Maven detects a property (a value which can be dereferenced within the POM by ${name}) of the corresponding name=value pair.

那么假设如下的profile

    <profiles>
        <profile>
            <id>web</id>
            <activation>
                <property>
                    <name>type</name>
                    <value>web</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

那么在需要这个依赖的子项目中设置以下properties不就好了么?

    <properties>
        <type>web</type>
    </properties>

然而事实证明想多了,这个properties只能是system property。
https://issues.apache.org/jira/browse/MNG-6851

最终实现选择性继承

这样只能考虑用file来进行选择性依赖了。

    <profiles>
        <profile>
            <id>web</id>
            <activation>
                <file>
                    <exists>${basedir}/web</exists>
                </file>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

在需要依赖的子项目中建立web文件,就可以获得相关依赖了。

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消