maven 管理項目實踐指南
組織工程
通常采用多模塊(module)組織工程。
模塊劃分原則:
示例:
<modules> <module>xxx-protocol</module> <module>xxx-web</module> <module>xxx-config</module> </modules>
1. xxx-protocol 是按功能獨立正交性劃分 module
2. xxx-web 按部署劃分 module,部署為一個 web 應用
3. xxx-config 抽出共享的第三方 module,多個模塊需要共享配置
依賴管理
通常統一在父項目中定義所有依賴及其版本。
示例:
<properties> <project.encoding>utf-8</project.encoding> <v.plugin.assembly>2.3</v.plugin.assembly> <v.plugin.compiler>2.5.1</v.plugin.compiler> <v.plugin.resources>2.6</v.plugin.resources> <v.plugin.release>2.4</v.plugin.release> <v.jdk>1.6</v.jdk> <v.junit>4.8.2</v.junit> <v.spring>3.1.2.RELEASE</v.spring> </properties>
如上,統一定義整個項目依賴的 jdk、三方庫、 maven 自身依賴插件的版本。
如下,統一在父 pom 中配置所有的依賴庫和版本
父 pom
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${v.spring}</version> </dependency> </dependencies> </dependencyManagement>
子 pom 中引用,不用指定版本
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> </dependencies>
有些項目采用在父 pom 中配置所有依賴,子模塊繼承時所有模塊都將依賴所有依賴庫,不符合最優最小依賴原則。
發布管理
1. maven 發布 web 類項目
原生支持 packaging 為 war 包方式,不贅述
2. maven 發布非 web 類項目
發布為獨立 java 進程部署啟動
通常采用 maven-assembly-plugin 來打包和組織非 web 類項目
assembly 插件提供了一種比較簡單的 jar-with-dependencies 打包方式,將所有三方依賴打入一個大的 jar 中并指定 main 類做成一個可執行 jar 包。
這種方式有幾個明顯的缺點:
1)第三方 jar 包抽取沖突,比如 spring 3.x 就不支持這種方式,需要把 spring 3.x 的多個 jar 包抽取到一個中時需要通過其他插件配合進行配置文件合并,比較麻煩
2)不便于單獨升級第三方 jar 包
這里介紹另外一種方式:
1)抽取第三方依賴 jar 包,到獨立 lib 目錄中
2)提取項目配置文件、避免被打入 jar 包中(打入 jar 包中不便于部署和運維時修改)
最終打包完成后的目錄結構如下:
xxxxxxx-version-all/
|-- bin/
|-- start.sh
|-- stop.sh
|-- lib/
|-- xxx-1.0.2-jar
|-- cfg/
|-- xx.xml
|-- xx.properties
bin 目錄存放啟動和停止腳本
lib 目錄存放自身 jar 包和 第三方 jar 包
cfg 目錄存放項目配置文件
配置示例:
在父 pom 中配置插件管理,如下:
<build> <pluginManagement> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>${v.plugin.jar}</version> <configuration> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> </configuration> <executions> <phase>package</phase> <goals> <goal>jar</goal> </goals> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>${v.plugin.assembly}</version> <configuration> <descriptors> <descriptor> src/main/assembly/assembly.xml </descriptor> </descriptors> </configuration> <executions> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </executions> </plugin> </plugins> </pluginManagement> </build>
需要打包部署為獨立 java 進程的子模塊 pom 中配置引用
<build> <plugins> <plugin> <artifactId > maven-assembly-plugin</artifactId > </plugin > <plugin > <artifactId]] > maven-jar-plugin</artifactId > </plugin > </plugins > </build >
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"]] > <id > all</id > <formats > <format > dir</format > <!-- 其他可選格式 gzip/zip/tar.gz/ --> </formats > <includeBaseDirectory > false</includeBaseDirectory > <dependencySets > <dependencySet > <outputDirectory > /lib</outputDirectory > <useProjectArtifact > true</useProjectArtifact > <unpack > false</unpack > <scope > runtime</scope > </dependencySet > </dependencySets > <fileSets > <fileSet > <directory]] > src/main/scripts</directory > <outputDirectory]] > /bin</outputDirectory > </fileSet > <fileSet > <directory]] > src/main/resources</directory > <outputDirectory]] > /cfg</outputDirectory > </fileSet > </fileSets > </assembly >
3. maven 發布共享庫項目
發布一些獨立 jar 包給其他項目使用
此類項目的特點是版本迭代快,版本管理復雜,通常采用 maven-release-plugin 來管理發布
maven-release-plugin 典型發布過程如下:
1) tag 一個發布版本,并發布到版本管理庫
2) 更新本地所有模塊的 pom 文件中的版本號為下一個指定版本
3) 部署 tag 出來的發布版本到私有或公共的中央 maven 倉庫
要完成以上過程,需要在父 pom 做如下的配置:
1)maven-release-plugin 插件配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version > ${v.plugin.release}</version > <configuration > <!-- tag 發布項目的源碼倉庫位置 --> <tagBase > http://xxxxxx/tags/xxx</tagBase > <useReleaseProfile > false</useReleaseProfile > </configuration > </plugin > </plugins > </build > <scm> <!-- 待發布項目分支路徑 --> <developerConnection > scm:svn:http://xxxxxxxx/branches/xxx/</developerConnection > </scm>
2) 自動部署到 maven 倉庫配置
<distributionManagement > <snapshotRepository > <id > repository.snapshots</id > <name > repository.snapshots</name > <url > http://xxxxxx/libs-snapshots</url > </snapshotRepository > <repository > <id > repository.release</id > <name > repository.release</name > <url >http://xxxxxx/libs- releases</url > </repository > </distributionManagement >
<servers > <server > <id >repository.snapshots </id > <username>xxxx</username > <password >***** </password > </server > <server > <id > repository.release</id > <username>xxxx</username > <password***** > </password > </server > </servers >
3) 執行發布,常用發布命令如下
# 干跑一次,不改變任何東西 mvn release:prepare -DdryRun=true # 如果依賴第三方的 snapshot 包,release 會阻止發布,可以增加選項強制發布 mvn release:prepare -DignoreSnapshots=true # 更新所有模塊版本 mvn release:update-versions # 清理,發布中途若出錯,可以清理后重新發布 mvn release:clean # 發布準備 交互模式執行 mvn release:prepare # 發布準備 批量模式執行 mvn --batch-mode release:prepare # 發布到遠程倉庫 mvn release:perform一切配置妥當后,通常只需執行如下兩條命令即可完成發布過程
mvn release:prepare mvn release:perform
注意:在一些多模塊相互交叉依賴的項目,改變默認的 prepare goal,用 intsall 安裝最新發布版本到本地庫避免 prepare 過程失敗
mvn release:prepare -DpreparationGoals=clean install
本文由用戶 y8de 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!