日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maven——profile介绍

發布時間:2025/3/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven——profile介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

4???????profile介紹

4.1?????profile簡介

profile可以讓我們定義一系列的配置信息,然后指定其激活條件。這樣我們就可以定義多個profile,然后每個profile對應不同的激活條件和配置信息,從而達到不同環境使用不同配置信息的效果。比如說,我們可以通過profile定義在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有時候我們可以通過操作系統的不同來使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等。具體的激活條件有哪些我在后文會講到。

4.2?????profile的定義位置

對于使用Maven3,我們可以有多個地方定義profile。定義的地方不同,它的作用范圍也不同。

(1)????針對于特定項目的profile配置我們可以定義在該項目的pom.xml中。

(2)????針對于特定用戶的profile配置,我們可以在用戶的settings.xml文件中定義profile。該文件在用戶家目錄下的“.m2”目錄下。

(3)????全局的profile配置。全局的profile是定義在Maven安裝目錄下的“conf/settings.xml”文件中的。

4.3?????profile中能定義的信息

profile中能夠定義的配置信息跟profile所處的位置是相關的。以下就分兩種情況來討論,一種是定義在settings.xml中,另一種是定義在pom.xml中。

4.3.1? profile定義在settings.xml中

當profile定義在settings.xml中時意味著該profile是全局的,它會對所有項目或者某一用戶的所有項目都產生作用。因為它是全局的,所以在settings.xml中只能定義一些相對而言范圍寬泛一點的配置信息,比如遠程倉庫等。而一些比較細致一點的需要根據項目的不同來定義的就需要定義在項目的pom.xml中。具體而言,能夠定義在settings.xml中的信息有<repositories>、<pluginRepositories>和<properties>。定義在<properties>里面的鍵值對可以在pom.xml中使用。

4.3.2? profile定義在pom.xml中

定義在pom.xml中的profile可以定義更多的信息。主要有以下這些:

l??<repositories>

l??<pluginRepositories>

l??<dependencies>

l??<plugins>

l??<properties>

l??<dependencyManagement>

l??<distributionManagement>

l??還有build元素下面的子元素,主要包括:

<defaultGoal>

<resources>

<testResources>

<finalName>

4.4?????profile的激活方式

Maven給我們提供了多種不同的profile激活方式。比如我們可以使用-P參數顯示的激活一個profile,也可以根據環境條件的設置讓它自動激活等。下面將對它們一一進行介紹:

4.4.1? 使用activeByDefault設置激活

先看下面一個配置

Xml代碼?

?

  • <profiles>??
  • ????????<profile>??
  • ?????????????<id>profileTest1</id>??
  • ?????????????<properties>??
  • ????????????????????<hello>world</hello>??
  • ?????????????</properties>??
  • ?????????????<activation>??
  • ????????????????????<activeByDefault>true</activeByDefault>??
  • ?????????????</activation>??
  • ????????</profile>??
  • ??????????
  • ????????<profile>??
  • ?????????????<id>profileTest2</id>??
  • ?????????????<properties>??
  • ????????????????????<hello>andy</hello>??
  • ?????????????</properties>??
  • ????????</profile>??
  • ?</profiles>??
  • ?

    ????????我們可以在profile中的activation元素中指定激活條件,當沒有指定條件,然后指定activeByDefault為true的時候就表示當沒有指定其他profile為激活狀態時,該profile就默認會被激活。所以當我們調用mvn package的時候上面的profileTest1將會被激活,但是當我們使用mvn package –P profileTest2的時候將激活profileTest2,而這個時候profileTest1將不會被激活。

    4.4.2? 在settings.xml中使用activeProfiles指定處于激活狀態的profile

    我們可以在settings.xml中使用activeProfiles來指定需要激活的profile,這種方式激活的profile將所有情況下都處于激活狀態。比如現在我們定義了如下兩個profile

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<properties>??
  • ???????????????????<hello>world</hello>??
  • ????????????</properties>??
  • ???????</profile>??
  • ?????????
  • ???????<profile>??
  • ????????????<id>profileTest2</id>??
  • ????????????<properties>??
  • ???????????????????<hello>andy</hello>??
  • ????????????</properties>??
  • ???????</profile>??
  • </profiles>??
  • ?

    ???????這里的profile可以是定義在settings.xml中的,也可以是定義在pom.xml中的。這個時候如果我們需要指定profileTest1為激活狀態,那么我們就可以在settings.xml中定義activeProfiles,具體定義如下:

    Xml代碼?

    ?

  • <activeProfiles>??
  • ?????<activeProfile>profileTest1</activeProfile>??
  • </activeProfiles>??
  • ?

    ???????考慮這樣一種情況,我們在activeProfiles下同時定義了多個需要激活的profile。這里還拿上面的profile定義來舉例,我們定義了同時激活profileTest1和profileTest2。

    Xml代碼?

    ?

  • <activeProfiles>??
  • ?????<activeProfile>profileTest1</activeProfile>??
  • ?????<activeProfile>profileTest2</activeProfile>??
  • </activeProfiles>??
  • ?

    ???????從profileTest1和profileTest2我們可以看出它們共同定義了屬性hello。那么這個時候我在pom.xml中使用屬性hello的時候,它到底取的哪個值呢?是根據activeProfile定義的順序,后面的覆蓋前面的嗎?根據我的測試,答案是非也,它是根據profile定義的先后順序來進行覆蓋取值的,然后后面定義的會覆蓋前面定義的。

    4.4.3? 使用-P參數顯示的激活一個profile

    假設我們現在有如下定義的profiles

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ??????????????<id>profileTest1</id>??
  • ??????????????<properties>??
  • ?????????????????????<hello>world</hello>??
  • ??????????????</properties>??
  • ???????</profile>??
  • ???????<profile>??
  • ??????????????<id>profileTest2</id>??
  • ??????????????<properties>??
  • ?????????????????????<hello>andy</hello>??
  • ??????????????</properties>??
  • ???????</profile>??
  • <profiles>??
  • ?

    ???????那么當我們在進行Maven操作時就可以使用-P參數顯示的指定當前激活的是哪一個profile了。比如我們需要在對項目進行打包的時候使用id為profileTest1的profile,我們就可以這樣做:

    Cmd代碼?

    ?

  • mvn?package?–P?profileTest1??
  • ?

    ???????當我們使用activeByDefault或settings.xml中定義了處于激活的profile,但是當我們在進行某些操作的時候又不想它處于激活狀態,這個時候我們可以這樣做:

    Cmd代碼?

    ?

  • Mvn?package?–P?!profileTest1??
  • ?

    ???????這里假設profileTest1是在settings.xml中使用activeProfile標記的處于激活狀態的profile,那么當我們使用“-P !profile”的時候就表示在當前操作中該profile將不處于激活狀態。

    4.4.4根據環境來激活profile

    profile一個非常重要的特性就是它可以根據不同的環境來激活,比如說根據操作系統的不同激活不同的profile,也可以根據jdk版本的不同激活不同的profile,等等。

    4.4.4.1根據jdk來激活profile

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ??????????????<id>profileTest1</id>??
  • ??????????????<jdk>1.5</jdk>??
  • ???????</profile>??
  • <profiles>??
  • ?

    ???????上面情況表示在jdk為1.5版本系列的時候激活profileTest1。

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ??????????????<id>profileTest1</id>??
  • ??????????????<jdk>[1.4,1.7)</jdk>??
  • ???????</profile>??
  • <profiles>??
  • ?

    ???????上面的情況表示在jdk為1.4、1.5和1.6的時候激活profileTest1。

    4.4.4.2根據操作系統來激活profile

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<os>??
  • ???????????????????<name>Windows?XP</name>??
  • ???????????????????<family>Windows</family>??
  • ???????????????????<arch>x86</arch>??
  • ???????????????????<version>5.1.2600</version>??
  • ??????????????</os>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ?

    ???????上面的情況就是根據操作系統的類型來激活profileTest1。

    4.4.4.3根據系統屬性來激活profile

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<property>??
  • ???????????????????<name>hello</name>??
  • ???????????????????<value>world</value>??
  • ??????????????</property>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ?

    上面的profileTest1將在提供了系統屬性hello,并且其值為world的時候激活。下面的做法可以激活profileTest1。

    Cmd代碼?

    ?

  • mvn?package?–Dhello=world??
  • ?

    ???????當是下面的這種定義形式時,profileTest1將在指定了系統屬性hello,且其值為任意值的時候被激活。

    Xml代碼?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<property>??
  • ???????????????????<name>hello</name>??
  • ??????????????</property>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ?

    4.4.4.4根據文件是否存在激活profile

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<file>??
  • ???????????????????<exists>target</exists>??
  • ??????????????</file>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ?

    上面的定義表示當存在target文件時激活profileTest1。

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<file>??
  • ???????????????????<missing>target</missing>??
  • ??????????????</file>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ?

    ???????上面的定義表示當不存在target文件時激活profileTest1。

    4.5?????查看當前處于激活狀態的profile

    我們可以同時定義多個profile,那么在建立項目的過程中,到底激活的是哪一個profile呢?Maven為我們提供了一個指令可以查看當前處于激活狀態的profile都有哪些,這個指定就是mvn help:active-profiles。

    現在假設我們的settings.xml文件中有如下profile的定義:

    Xml代碼?

    ?

  • <profiles>??
  • ???????<profile>??
  • ????????????<id>profileTest1</id>??
  • ????????????<activation>??
  • ??????????????<file>??
  • ???????????????????<missing>target</missing>??
  • ??????????????</file>??
  • ????????????</activation>??
  • ???????</profile>??
  • </profiles>??
  • ??
  • <activeProfiles>??
  • ?????<activeProfile>profileTest1</activeProfile>??
  • </activeProfiles>??
  • ?

    ???????這個時候我們可以看到,我們已經定義了profileTest1始終為激活狀態,這個時候我們使用mvn help:active-profiles查看處于激活狀態的profile時,就會打印出如下內容:


    轉載于:https://my.oschina.net/mlh3823/blog/801982

    總結

    以上是生活随笔為你收集整理的Maven——profile介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。