maven 管理项目实践指南

本文详细介绍了组织工程中的模块划分原则、依赖管理、发布管理等最佳实践,包括如何利用Maven统一管理项目依赖、发布流程及共享库项目的发布策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组织工程


通常采用多模块(module)组织工程。
模块划分原则:
示例:
[html]  view plain copy
  1. <modules>  
  2.     <module>xxx-protocol</module>      
  3.     <module>xxx-web</module>  
  4.     <module>xxx-config</module>  
  5. </modules>  

1. xxx-protocol 是按功能独立正交性划分 module
2. xxx-web      按部署划分 module,部署为一个 web 应用
3. xxx-config   抽出共享的第三方 module,多个模块需要共享配置

依赖管理


通常统一在父项目中定义所有依赖及其版本。
示例:
[html]  view plain copy
  1. <properties>  
  2.     <project.encoding>utf-8</project.encoding>  
  3.     <v.plugin.assembly>2.3</v.plugin.assembly>  
  4.     <v.plugin.compiler>2.5.1</v.plugin.compiler>  
  5.     <v.plugin.resources>2.6</v.plugin.resources>  
  6.     <v.plugin.release>2.4</v.plugin.release>  
  7.     <v.jdk>1.6</v.jdk>  
  8.   
  9.     <v.junit>4.8.2</v.junit>  
  10.     <v.spring>3.1.2.RELEASE</v.spring>  
  11. </properties>  
如上,统一定义整个项目依赖的 jdk、三方库、 maven 自身依赖插件的版本。

如下,统一在父 pom 中配置所有的依赖库和版本
父 pom
[html]  view plain copy
  1. <dependencyManagement>  
  2.     <dependencies>  
  3.        <dependency>  
  4.             <groupId>org.springframework</groupId>  
  5.             <artifactId>spring-beans</artifactId>  
  6.             <version>${v.spring}</version>  
  7.         </dependency>  
  8.     </dependencies>  
  9. </dependencyManagement>  

子 pom 中引用,不用指定版本
[html]  view plain copy
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>org.springframework</groupId>  
  4.         <artifactId>spring-beans</artifactId>  
  5.     </dependency>  
  6. </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 中配置插件管理,如下:
[html]  view plain copy
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>  
  4.              <plugin>  
  5.                  <artifactId>maven-jar-plugin</artifactId>  
  6.                  <version>${v.plugin.jar}</version>  
  7.                  <configuration>  
  8.                      <excludes>  
  9.                          <exclude>**/*.properties</exclude>  
  10.                          <exclude>**/*.xml</exclude>  
  11.                      </excludes>  
  12.                  </configuration>  
  13.                  <executions>  
  14.                      <phase>package</phase>  
  15.                      <goals>                              
  16.                          <goal>jar</goal>  
  17.                      </goals>  
  18.                  </executions>  
  19.              </plugin>  
  20.              <plugin>  
  21.                  <artifactId>maven-assembly-plugin</artifactId>  
  22.                  <version>${v.plugin.assembly}</version>  
  23.                  <configuration>  
  24.                      <descriptors>  
  25.                          <descriptor>  
  26.                              src/main/assembly/assembly.xml  
  27.                          </descriptor>  
  28.                      </descriptors>  
  29.                  </configuration>  
  30.                  <executions>  
  31.                      <phase>package</phase>  
  32.                      <goals>  
  33.                          <goal>assembly</goal>  
  34.                      </goals>  
  35.                  </executions>  
  36.              </plugin>  
  37.         </plugins>  
  38.     </pluginManagement>  
  39. </build>  

需要打包部署为独立 java 进程的子模块 pom 中配置引用
[html]  view plain copy
  1. <build>  
  2.         <plugins>  
  3.             <plugin>  
  4.                 <artifactId > maven-assembly-plugin</artifactId >  
  5.             </plugin >  
  6.             <plugin >  
  7.                 <artifactId]] > maven-jar-plugin</artifactId >  
  8.             </plugin >  
  9.         </plugins >  
  10.     </build >  

在 assembly.xml 指定具体的打包方式
[html]  view plain copy
  1. <assembly  
  2.         xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"]] >  
  5.         <id > all</id >  
  6.         <formats >  
  7.             <format > dir</format >  <!-- 其他可选格式 gzip/zip/tar.gz/ -->  
  8.         </formats >  
  9.       
  10.         <includeBaseDirectory > false</includeBaseDirectory >  
  11.       
  12.         <dependencySets >  
  13.             <dependencySet >  
  14.                 <outputDirectory > /lib</outputDirectory >  
  15.                 <useProjectArtifact > true</useProjectArtifact >  
  16.                 <unpack > false</unpack >  
  17.                 <scope > runtime</scope >  
  18.             </dependencySet >  
  19.         </dependencySets >  
  20.    
  21.         <fileSets >  
  22.             <fileSet >  
  23.                 <directory]] > src/main/scripts</directory >  
  24.                 <outputDirectory]] > /bin</outputDirectory >  
  25.             </fileSet >  
  26.             <fileSet >  
  27.                 <directory]] > src/main/resources</directory >  
  28.                 <outputDirectory]] > /cfg</outputDirectory >  
  29.             </fileSet >  
  30.         </fileSets >  
  31.     </assembly >  

3. maven 发布共享库项目
    发布一些独立 jar 包给其他项目使用
    此类项目的特点是版本迭代快,版本管理复杂,通常采用 maven-release-plugin 来管理发布
    maven-release-plugin 典型发布过程如下:
    1) tag 一个发布版本,并发布到版本管理库
    2) 更新本地所有模块的 pom 文件中的版本号为下一个指定版本
    3) 部署 tag 出来的发布版本到私有或公共的中央 maven 仓库

    要完成以上过程,需要在父 pom 做如下的配置:
    1)maven-release-plugin 插件配置
[html]  view plain copy
  1. <build>  
  2.     <plugins>      
  3.         <plugin>  
  4.             <groupId>org.apache.maven.plugins</groupId>  
  5.             <artifactId>maven-release-plugin</artifactId>  
  6.             <version > ${v.plugin.release}</version >  
  7.             <configuration >  
  8.                 <!-- tag 发布项目的源码仓库位置 -->  
  9.                 <tagBase > http://xxxxxx/tags/xxx</tagBase >  
  10.                 <useReleaseProfile > false</useReleaseProfile >  
  11.             </configuration >  
  12.         </plugin >  
  13.     </plugins >  
  14. </build >  
  15.   
  16. <scm>  
  17.     <!-- 待发布项目分支路径 -->  
  18.     <developerConnection > scm:svn:http://xxxxxxxx/branches/xxx/</developerConnection >  
  19. </scm>  

     2) 自动部署到 maven 仓库配置 
[html]  view plain copy
  1. <distributionManagement >  
  2.     <snapshotRepository >  
  3.         <id > repository.snapshots</id >  
  4.         <name > repository.snapshots</name >  
  5.         <url > http://xxxxxx/libs-snapshots</url >  
  6.     </snapshotRepository >  
  7.     <repository >  
  8.         <id > repository.release</id >  
  9.         <name > repository.release</name >  
  10.         <url >http://xxxxxx/libs- releases</url >  
  11.     </repository >  
  12.  </distributionManagement >  

    在 maven 安装目录下  conf/settings.xml 中配置仓库访问用户名、密码    
[html]  view plain copy
  1. <servers >  
  2.     <server >  
  3.         <id >repository.snapshots </id >  
  4.         <username>xxxx</username >  
  5.         <password >***** </password >  
  6.     </server >  
  7.     <server >  
  8.         <id > repository.release</id >  
  9.         <username>xxxx</username >   
  10.         <password***** > </password >   
  11.     </server >  
  12. </servers >  

    3) 执行发布,常用发布命令如下
[plain]  view plain copy
  1. # 干跑一次,不改变任何东西  
  2.   mvn release:prepare -DdryRun=true  
  3.   
  4. # 如果依赖第三方的 snapshot 包,release 会阻止发布,可以增加选项强制发布  
  5.   mvn release:prepare -DignoreSnapshots=true  
  6.   
  7. # 更新所有模块版本  
  8.   mvn release:update-versions  
  9.   
  10. # 清理,发布中途若出错,可以清理后重新发布  
  11.   mvn release:clean  
  12.   
  13. # 发布准备 交互模式执行  
  14. mvn release:prepare  
  15.   
  16. # 发布准备 批量模式执行  
  17. mvn --batch-mode release:prepare  
  18.   
  19. # 发布到远程仓库  
  20. mvn release:perform  
     一切配置妥当后,通常只需执行如下两条命令即可完成发布过程
   
[plain]  view plain copy
  1. mvn release:prepare  
  2. mvn release:perform  

    注意:在一些多模块相互交叉依赖的项目,改变默认的 prepare goal,用 intsall 安装最新发布版本到本地库避免 prepare 过程失败 

    mvn release:prepare -DpreparationGoals=clean install



参考博客:http://blog.youkuaiyun.com/mindfloating/article/details/8519938

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值