Maven的構建配置檔案(Build Profiles)
在命令列使用構建配置檔案時,是-P,比如:mvn -Pinput
注意:這裡的構建配置檔案並不是一個真正的檔案,而是通過指定引數來做特定的事。
什麼是構建配置檔案?
構建配置檔案(A Build profile) 是一系列的配置項的值,可以用來設定或者覆蓋Maven構建預設值。使用構建配置檔案,你可以為不同的環境,比如說生產環境(Producation)和開發(Development)環境,定製構建方式。
配置檔案在pom.xml檔案中使用activeProfiles或者profiles元素指定,並且可以通過各種方式觸發。配置檔案在構建時修改POM,並且用來給引數設定不同的目標環境(比如說,開發(Development)、測試(Testing)和生產環境(Producation)中資料庫伺服器的地址)。
構建配置檔案的型別
構建配置檔案大體上有三種類型
型別 | 在哪定義 |
---|---|
專案級(Per Project) | 定義在專案的POM檔案pom.xml中 |
使用者級 (Per User) | 定義在Maven的設定xml檔案中 (%USER_HOME%/.m2/settings.xml) |
全域性(Global) | 定義在Maven全域性的設定xml檔案中 (%M2_HOME%/conf/settings.xml) |
配置檔案啟用
Maven的構建配置檔案可以通過多種方式啟用。
- 使用命令控制檯輸入顯式啟用。
- 通過maven設定。
- 基於環境變數(使用者或者系統變數)。
- 作業系統設定(比如說,Windows系列)。
- 檔案的存在或者缺失。
官方配置檔案啟用示例
實踐配置檔案啟用示例
新建的專案結構如下:
其中在src/main/resources資料夾下有三個用於測試檔案:
檔名 | 描述 |
---|---|
env.properties | 如果未指定配置檔案時預設使用的配置。 |
env.test.properties | 當測試配置檔案使用時的測試配置。 |
env.prod.properties | 當生產配置檔案使用時的生產配置。 |
注意:這三個配置檔案並不是代表構建配置檔案的功能,而是用於本次測試的目的;比如,我指定了構建配置檔案
注意:下面的例子仍然是使用AntRun外掛,因為此外掛能繫結Maven生命週期階段,並通過Ant的標籤不用編寫一點程式碼即可輸出資訊、複製檔案等,經此而已。其餘的與本次構建配置檔案無關。
1、顯示配置檔案啟用
pom.xml配置如下:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
注意:構建配置檔案採用的是<profiles>節點。
說明:上面新建了三個<profiles>,其中<id>區分了不同的<profiles>執行不同的AntRun任務;而AntRun的任務可以這麼理解,AntRun監聽test的Maven生命週期階段,當Maven執行test時,就除了發AntRun的任務,任務裡面為輸出文字並複製檔案到指定的位置;而至於要執行哪個AntRun任務,此時構建配置檔案起到了傳輸指定的作用,比如,通過命令列引數輸入指定的<id>。
執行命令:
mvn test -Ptest
提示:第一個test為Maven生命週期階段,第2個test為為構建配置檔案指定的<id>引數,這個引數通過-P來傳輸,當然,它可以是prod或者normal這些由你定義的<id>。
執行的結果如下:
可以看出成功的觸發了AntRun的任務。並且是對應構建配置檔案下的<id>為test的任務。
再測試其餘兩個命令,結果如下:
2、通過Maven設定啟用配置檔案
開啟%USER_HOME%/.m2目錄下的settings.xml檔案,其中%USER_HOME%代表使用者主目錄。如果setting.xml檔案不存在就直接拷貝%M2_HOME%/conf/settings.xml到.m2目錄,其中%M2_HOME%代表Maven的安裝目錄。對於為什麼可以這樣做,參考:http://www.cnblogs.com/EasonJim/p/6827058.html
配置setting.xml檔案,增加<activeProfiles>屬性:
<settings 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/settings-1.0.0.xsd"> ... <activeProfiles> <activeProfile>test</activeProfile> </activeProfiles> </settings>
執行命令:
mvn test
提示:此時不需要使用-Ptest來輸入引數了,上面的setting.xml檔案的<activeprofile>已經指定了test引數,代替了。
提示2:同樣可以使用在%M2_HOME%/conf/settings.xml的檔案進行配置,效果一致。
執行結果:
3、通過環境變數啟用配置檔案
先把上一步測試的setting.xml值全部去掉。
然後在pom.xml裡面的<id>為test的<profile>節點,加入<activation>節點:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <activation> <property> <name>env</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
執行命令:
mvn test -Denv=test
提示:上面使用-D傳遞環境變數,其中evn對應剛才設定的<name>值,test對應<value>。
提示2:在Windows 10上測試了系統的環境變數,但是不生效,所以,只能通過-D傳遞。
執行結果:
4、通過作業系統啟用配置檔案
5、通過檔案的存在或者缺失啟用配置檔案
6、通過JDK的版本啟用配置檔案
...
相關推薦
Maven的構建配置檔案(Build Profiles)
在命令列使用構建配置檔案時,是-P,比如:mvn -Pinput 注意:這裡的構建配置檔案並不是一個真正的檔案,而是通過指定引數來做特定的事。 什麼是構建配置檔案? 構建配置檔案(A Build profile) 是一系列的配置項的值,可以用來設定或者覆蓋Maven構建預設值。使用構建配
4.修改構建配置檔案(CMakeLists.txt)
ROS的構建系統catkin基本上使用CMake,並在功能包目錄中的CMakeLists.txt檔案中描述構建環境。在這個檔案中設定可執行檔案的建立、依賴包優先構建、聯結器(linker)的建立等等,以構建的功能包my_first_ros_pkg為例說明。 構建配置檔案(CMakeLists.t
[Golang] 從零開始寫Socket Server(4):將執行引數放入配置檔案(XML/YAML)
為了將我們寫好的Server釋出到伺服器上,就要將我們的程式碼進行build打包,這樣如果以後想要修改一些程式碼的話,需要重新給程式碼進行編譯打包並上傳到伺服器上。 顯然,這麼做過於繁瑣。。。因此常見的做法都是將Server執行中
ssm框架整合入門系列——編寫ssm整合的關鍵配置檔案(web.xml)
編寫ssm整合的關鍵配置檔案(web.xml) 前言 web.xml,一個Tomcat工程中最重要的配置檔案。web.xml沒有其實也可以----只要你確定你的專案裡面不需要任何過濾器、監聽器、Servlet等等 在啟動一個WEB專案的時候,WEB容器(比如t
3.修改功能包配置檔案(package.xml)
必要的ROS配置檔案之一的package.xml是一個包含功能包資訊的XML檔案,包括功能包名稱、作者、許可證和依賴功能包。 下面是對每個語句的說明。 ■ <?xml> 這是一個定義文件語法的語句,隨後的內容
解析SpringBoot中配置檔案(.yml/.yaml)
解析SpringBoot中配置檔案(.yml/.yaml) springBoot配置檔案的載入有先後順序為: 1.application.yml 2.application.yaml 3.application.properoties 後加載的會把先載入的給覆蓋掉 #普通資料的配置
001—玩轉Mysql的配置檔案(my.ini)
#====================================== #平臺:windows7 #MYsql版本:mysql-5.5.62-winx64(安裝版) #MYsql工具:SQLyog - 64&nbs
Terraform配置檔案(Terraform configuration)
Terraform配置檔案 翻譯自Terraform Configuration Terraform用文字檔案來描述裝置、設定變數。這些檔案被稱為Terraform配置檔案,以.tf結尾。這一部分將講述Terraform配置檔案的載入與格式。 配置檔案的格式,支援兩種方式:Terraform格式和JSON格式
mybatis原始碼-解析配置檔案(四-1)之配置檔案Mapper解析(cache)
1. 簡介 本文章主要講解的是, xxxMapper.xml 檔案中, cache 節點的原始碼。 2. 解析 XMLMapperBuilder.cacheElement() 方法主要負責解析 <cache> private void cacheElement(XNode context)
SpringBoot自定義配置檔案(xxx.properties)
轉載 :https://www.cnblogs.com/V1haoge/p/7183408.htmlSpringBoot中免除了大部分手動配置,但是對於一些特定的情況,還是需要我們進行手動配置的,SpringBoot為我們提供了application.properties配置檔案,讓我們可以進行自定義配置,來
為 eclipse 的 hibernate 配置檔案( hbm xml)加上自動提示功能
為 eclipse 的 hibernate 配置檔案(*.hbm.xml)加上自動提示功能 在編輯 *.hbm.xml 檔案時,myeclipse 帶有自動提示功能,但 eclipse 是沒有自動提示功能的。需要自己手工加上: 1、開啟專案中
Nginx配置檔案(nginx.conf)配置詳解
歡迎掃碼加入Java高知群交流 Nginx的配置檔案nginx.conf配置詳解如下: user nginx nginx ; Nginx使用者及組:使用者 組。window下不指定 worker_processes 8; 工作程序:數目。根據硬體調整,通常等於CPU數
spring boot 讀取配置檔案(application.yml)中的屬性值
在spring boot中,簡單幾步,讀取配置檔案(application.yml)中各種不同型別的屬性值: 1、引入依賴: <!-- 支援 @ConfigurationProperties
Nginx——配置檔案(nginx.conf)配置詳解
轉自:https://blog.csdn.net/tjcyjd/article/details/50695922Nginx的配置檔案nginx.conf配置詳解如下:user nginx nginx ;Nginx使用者及組:使用者 組。window下不指定worker_pro
瞭解npm的檔案結構(npm-folders)和配置檔案(npm-mrc)
一、npm的檔案結構 npm的安裝: 本地安裝 1. 將安裝包放在 ./node_modules 下(執行 npm 命令時所在的目錄),如果沒有 node_modules 目錄,會在當前執行 npm 命令的目錄下生成 node_modules 目錄。 2. 可以通過 require() 來引
source insight 配置檔案(超好)
下載地址:http://www.rayfile.com/zh-cn/files/fc8b2fdc-ce9b-11df-bc91-0015c55db73d/ 0. 此為C/C++的SourceInsight配置檔案,其他語言的為預設。(字型顏色在其他程式語言有效) 1.
4. 讀取配置檔案(application.yml)中的屬性值
在spring boot中,簡單幾步,讀取配置檔案(application.yml)中各種不同型別的屬性值:1、引入依賴: <!-- 支援 @ConfigurationProperties 註解 --> <dependency> <
source insight 配置檔案(超好) --轉載
http://zhuang0393.blog.163.com/blog/static/9285104201093111912636/ 0. 此為C/C++的SourceInsight配置檔案,其他語言的為預設。(字型顏色在其他程式語言有效) 1. 主要就是給那些習慣於
webpack下的react配置檔案(熱替換)
webpack_dev_onfig.js配置檔案const webpack = require('webpack'); const path = require("path"); const UglifyJSPlugin = require("uglifyjs-webpack
Nginx 配置檔案(nginx.conf)
user www www; worker_processes auto; error_log /data/wwwlogs/error_nginx.log crit; pid /usr/local/nginx/logs/nginx.pid; #Sp