版权声明:本文为博主原创文章,未经博主允许不得转载。
组织工程
子 pom 中引用,不用指定版本
需要打包部署为独立 java 进程的子模块 pom 中配置引用
在 assembly.xml 指定具体的打包方式
:
2) 自动部署到 maven 仓库配置
在 maven 安装目录下
conf/settings.xml 中配置仓库访问用户名、密码
3) 执行发布,常用发布命令如下
一切配置妥当后,通常只需执行如下两条命令即可完成发布过程
通常采用多模块(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
参考博客:http://blog.youkuaiyun.com/mindfloating/article/details/8519938