今天無意中刺激到我啦,這促使我發現自己還不夠強大,我需要不斷充實自己,擴充自己的實力,加油吧,騷年!!!
今天我要講的是maven靈活構建相關的知識,請做好準備,知識點有點多哦~~~
maven屬性
首先帶來的是maven
的屬性知識,在maven
中一共有6大屬性,分別為:
內置屬性
basedir
項目pom
文件所在地址
version
項目版本
使用方式為直接使用${basedir}
的方式進行引用
pom屬性
可以使用該類屬性引用pom
文件中的配置,因為pom
文件根元素為project
,所以使用時通過${project.version}
,${project.groupId}
的方式引用
settings屬性
可以使用該類屬性引用settings
文件中的配置,前面已經提到,我們推薦在用戶目錄下使用該settings.xml
文件,比如我在windows
系統中該文件位置為:C:\Users\Administrator\.m2\settings.xml,settings.xml
中根元素為settings
,所以引用需要通過${settings.localRepository}
的方式
自定義屬性
這也許是我們最常用的一種屬性了,在pom
文件的properties
中定義我們想要引用的屬性,然后再通過${property_name}
的方式引用,比如
<project>
<properties>
<spring.version>3.2.0</spring.version>
</properties>
</project>
通過${spring.version}
方式引用
java系統屬性
這種屬性可以通過在控制臺中輸入mvn help:system
的方式查詢當前系統中存在的系統屬性,通過${prop_name}
的方式引用,比如${file.encoding.pkg}
環境變量屬性
環境變量屬性也可以通過mvn help:system
的方式查看,引用的方式通過${env.varname}
的方式引用
使用這些屬性可以幫我們在引入模塊時簡化維護和配置的工作
管理多模塊依賴
groupId
,version
都是相同的,這時可以通過pom
屬性將groupId
,versionId
進行簡化
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>account-service</artifactId>
<version>${project.version}</version>
</dependency>
管理項目依賴
依賴某一個模塊,比如spring
一系列的jar
包時,他們的version
其實是一樣的,所以我們也可以通過自定義屬性定義spring.version
屬性,將版本號抽離出來放入<properties>
標簽中然后進行引用
配置插件
大量插件也用到了屬性,比如maven-surefire-plugin
中的reportsDirectory
我們可以通過pom
屬性修改報告的位置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<reportsDirectory>${project.build.directory}/test-reports/</reportsDirectory>
</configuration>
</plugin>
構建環境的差異
在不同的環境中,項目的源碼應該會用不同的方式進行構建,比如數據庫的配置,在測試、開發、正式這三個不同的環境下是不會一樣的,按照傳統的方式,在src/main/resources
下放置有配置文件jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
其中包含了數據庫連接的配置信息,當在不同的環境中時,手工修改這個數據庫配置,然后再構建,但這種方式是非常麻煩笨拙的
資源過濾(命令行)
為了解決上面的變化,maven
提供了一種解決方式,通過profile
隔離環境的變化
首先將jdbc.properties
變化的配置抽離出來
jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
通過maven
屬性與profile
結合的方式配置不同環境的特有屬性
<profiles>
<profile>
<id>dev</id>
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName/>
<jdbc.url>jdbc:mysql://localhost:3306/test</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>root</jdbc.password>
</properties>
</profile>
</profiles>
我們知道,對于maven
的屬性,只能在pom
文件中使用,那么在資源配置文件jdbc.properties
中是無法訪問的,為了讓其能夠訪問到屬性,我們需要使用maven-resources-plugin
做一些配置:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
通過directory
指定資源配置文件所在的目錄,并通過filtering=true
表示當前目錄下的資源文件都需要進行過濾,也就是其中配置的屬性引用需要被pom
屬性值替換
配置完之后。我們可以通過命令行mvn clean install -Pdev
的方式運行,完成后輸出目錄中的數據庫配置就被dev
中的屬性替換掉了
maven profile
上面提到,我們可以通過profile
來隔離不同環境的屬性變化,然后通過命令行-PprofileId
的方式激活profile
,對于profile
的激活,還可以有其他幾種方式
profile激活
命令行激活方式
mvn
命令行參數-P
加上profileId
來激活profile
,多個profile
用逗號隔開
mvn clean install -Pdev-x,dev-y
,這里將會激活dev-x,dev-y
的配置信息
settings文件顯示激活
如果希望某一配置默認一直處于激活狀態,就可以配置settings.xml
文件中的activeProfiles
元素,它表示配置的profile
對所有項目都處于激活狀態
<settings>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
系統屬性激活
可以用當某一個系統屬性存在的時候自動激活profile
<profiles>
<profile>
<activation>
<property>
<name>test</name>
</property>
</activation>
...
</profile>
</profiles>
可以進一步精確,當存在屬性test
,且值為x
時激活profile
<profiles>
<profile>
<activation>
<property>
<name>test</name>
<value>x</value>
</property>
</activation>
...
</profile>
</profiles>
我們可以通過命令行聲明系統屬性,所以我們可以通過
mvn clean install -Dtest=x
的方式從命令行激活profile
操作系統環境激活
<profiles>
<profile>
<activation>
<os>
<name>Windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
</profile>
</profiles>
family
包括Windows
,UNIX
,Mac
等,其他幾項name
,arch
,version
可以通過mvn help:system
方式查看os.
開頭的系統屬性,這里分別對應os.name
,os.arch
,os.version
文件是否存在激活
<profiles>
<profile>
<activation>
<file>
<missing>x.properties</missing>
<exists>y.properties</exists>
</file>
</activation>
...
</profile>
</profiles>
存在y.properties
,且不存在x.properties
時激活profile
默認激活(開發中用的最多)
用戶可以在配置profile
時指定默認激活
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
不過需要注意的是,如果有任何一個profile
通過其他以上任意一種方式被激活了,那么當前的默認激活配置就會失效
如果項目中配置了多種激活方式,那么我們可以通過maven-help-plugin
查看
查看當前已激活的配置
mvn help:active-profiles
列出當前所有profile
mvn help:all-profiles
profile的種類
根據需要,用戶可以將profile
配置到:
pom.xml 只對當前項目有效
用戶settings.xml 對本機該用戶所有項目有效
全局settings.xml 安裝maven
目錄下的conf/settings.xml
中的profile
對本機所有用戶的所有項目有效
在profile
中可以使用的pom
元素
<project>
<repositories></repositories>
<pluginRepositories></pluginRepositories>
<distributionManagement></distributionManagement>
<dependencies></dependencies>
<dependencyMangement></dependencyManagement>
<modules></modules>
<properties></properties>
<reporting></reporting>
<build>
<plugins></plugins>
<defaultGoal></defaultGoal>
<resources></resources>
<testResources></testResources>
<finalName></finalName>
</build>
</project>
web資源過濾
與普通的配置文件一樣,web
資源(src/main/webapps/
下的資源文件css/js/img
等)也不能使用pom
屬性,需要通過maven-war-plugin
進行配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.css</include>
<include>**/*.js</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
通過directory
聲明web
資源目錄src/main/webapp
(這也是默認的web
資源目錄),然后配置filtering
開啟過濾,并使用includes
指定要過濾的文件,這里指定所有的js
,css
都需要進行過濾
在工作中的使用
我們需要根據不同環境配置不同的參數,就拿數據庫配置為例,account-web
表示項目名
我們需要在account-web
下建立不同環境的配置文件,用于分離環境參數配置
dev.properties
的配置如下,其他環境比如test
,production
環境可能需要配置不同的數據庫連接方式
jdbc.username=root
jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
在pom
中指定三種不同環境的profile
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>${basedir}/config/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.xsd</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
這里需要注意使用includes
表示構建時需要將那些資源文件納入構建中,這里需要指定所有需要用到的資源文件,否則在目標構建中將不會包含include
之外的配置文件
如此在運行maven
命令進行打包時就會使用激活的profile
對應的配置替換其中變量
好啦,今天就講到這里,剛好00:00,還有其他事情要做,今天就到這里吧~~~