当前位置:操作系统 > 安卓/Android >>

maven 管理项目实践指南

组织工程
 
通常采用多模块(module)组织工程。
模块划分原则:
示例:
[html]  
<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,多个模块需要共享配置
 
依赖管理
 
通常统一在父项目中定义所有依赖及其版本。
示例:
[html]  
<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
[html]  
<dependencyManagement>  
    <dependencies>  
       <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>${v.spring}</version>  
        </dependency>  
    </dependencies>  
</dependencyManagement>  
 
子 pom 中引用,不用指定版本
[html] 
<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 中配置插件管理,如下:
[html] 
<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>  
               
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,