1. 程式人生 > >Maven pom.xml模板、打包、版本控制

Maven pom.xml模板、打包、版本控制

18.11.12~13 松江圖書館

本章主要涉及以下幾點 :

pom.xml模板

maven專案在實際開發過程中,經常會依賴jar包或者安裝外掛,這都會涉及到對pom.xml檔案的修改。

而充分了解標籤的功能和標籤層級巢狀關係,顯得尤為重要

以下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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.pubei</groupId> <artifactId>afdre</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>
war</packaging> <!--以上標籤資訊,在建立maven專案時,就會自動新增,本章不作討論--> <!--在pro.xml priject中,日常會經常用到的4個子標籤 dependencyManagment、dependencies、build和profiles--> <!--dependencies標籤中,配置dependency 和 exclusions 2部分--> <dependencies> <
dependency
>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.2.RELEASE</version> </dependency> ...多個dependency標籤... <exclusions> <exclusion> <groupId>com.centit.framework</groupId> </exclusion> ...多個exclusion標籤... </exclusions> </dependencies> <!--dependencyManagement標籤中配置dependencies--> <dependencyManagement> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> ...多個dependency標籤... </dependencies> </dependencyManagement> <!--bulid標籤中,配置plugins 和 resources 2部分--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> ...配置多個plugin標籤... </plugins> <resources> <!--設定配置檔案讀取的路徑,配合profiles標籤中使用--> <resource> <directory>src/main/resources/${env}</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> ... 多個resource標籤... </resources> </build> <!--profiles 標籤中,設定配置檔案載入的資料夾--> <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>product</id> <properties> <env>product</env> </properties> </profile> </profiles> </project>

maven-compiler-plugin 編譯外掛,配置預設jdk

通過eclipse構建的maven web專案,預設使用的jdk是1.5版本,預設servlet為2.3版本。 而目前我們主流開發用的的jdk是1.7或1.8,servlet也是基於註解功能的3.o,版; 而maven編譯和動行本身是需要我們本地的java環境作為支援的,所以,統一版本,就很有必要;

我本人用的本地java是1.8的,所以我配置了 <source>1.8</source> <target>1.8</target> maven編譯環境;

eclipse開啟 windows–properties–maven–project Facets 修改jdk為1.8,同時修改Dynamic Web Module為 3.0 在這裡插入圖片描述

dependencyManagment和dependencies 區別

dependencyManagement標籤和denpendencise標籤和可作為同級標籤,也可作為上下級標籤;

同級:dependencyManagement標籤功能在於“申明”依賴,denpendencise標籤在於“申明和使用”依賴

上下級: 因為dependencyManagement 申明一個依賴需要藉助denpendencise標籤,此時denpendencise標籤為子標籤

關於“申明”和“申明和使用”依賴的解釋:

dependencyManagement標籤申明依賴,主要功能為是版本version控制;

尤其在多模構建專案時,在統一的父模組中(父模組<packaging>pom</packaging>),通過dependencyManagement標籤,申明依賴,確認了依賴的版本version資訊,子模組只需要通過denpendencise標籤來引入依賴,不用指定版本,預設用的就是父類版本; 此時要注意: 子專案依賴父類沒有申明的依賴,一定要加版本號,此時子類會倉庫自行找對應的依賴資訊;子專案依賴父類申明的依賴,version版本和父類不同,此時子類也會在倉庫中找對應自己的依賴資訊;

maven依賴的版本控制

通過對dependencyManagement認識,它的功能其實是在父模組中對引入依賴的版本進行統一控制,有“總體把控”多模組專案的依賴版本的特別;

下面介紹另一種版本控控制的思維:

  <properties>
   <spring-framework-version>4.3.7.REALEASE</spring-framework-version>
   </properties>
 
    <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring-framework-version}</version>
      <scope>compile</scope>
    </dependency>
    </dependencies> 

properties標籤是project根標籤下的一級子標籤,通過它可以配置不同依賴的版本號,然後用類似EL表示式的取值;

當一個pom引入大量的依賴資訊時,這種方式,把版本號當作作為一種“變數”,然後取值,方便我們統一管理依賴版本

不同環境下打包專案,選取不同配置檔案,對應的打包指令

對於企業級專案,無論是進行本地測試,還是測試環境測試以及最終的專案上線,都會涉及專案的打包操作,對於每個環境下專案打包時,對應的專案所有要的配置資源就會有所區別,實現打包的方式有很多種,可以通過ant,獲取通過ide 自帶的打包功能實現專案打包,但當專案很大並且需要的外界配置很多時,此時打包的配置就會異常複雜,對於maven 專案,我們可以用過pom.xml 配置的方式來實現打包時的環境選擇,相比較其他形式打包工具,通過maven 只需要通過簡單的配置,就可以輕鬆完成不同環境先專案的整體打包。 在這裡插入圖片描述

可以借肋eclipse中的m2e外掛,選中專案,右鍵 run as -maven builde-輸入命令

clean compile package -Dmaven.test.skip=true 打包預設環境(開發環境)並且跳過maven 測試操作 clean compile package -Ptest -Dmaven.test.skip=true 打包測試環境並且跳過maven 測試操作 clean compile package -Pproduct -Dmaven.test.skip=true 打包生產環境並且跳過maven 測試操作

在這裡插入圖片描述