Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Maven 專案管理實戰:多模組組織、依賴與發布全流程

編輯:關於 Android 程式設計

Maven 是 Java 生態系統中不可或缺的建構與依賴管理工具。本文從多模組組織、依賴管理到分發發布,梳理一套可直接落地的實踐方案。

一、以多模組組織工程

大型專案通常採用多模組(module)的方式拆分,讓職責更清晰、維護更便利。常見的拆分原則如下:

  • xxx-protocol:依功能的正交性獨立拆分。
  • xxx-web:依部署形態拆分,最終打包為一個 Web 應用。
  • xxx-config:抽出多個模組共享的第三方設定。
<modules>
    <module>xxx-protocol</module>
    <module>xxx-web</module>
    <module>xxx-config</module>
</modules>

二、統一管理依賴

最佳实践是將所有依賴及其版本集中定義在父專案(parent POM)中,透過 <properties> 統一管理版本號:

<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>

接著在父 POM 的 dependencyManagement 中集中聲明依賴與版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${v.spring}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子模組引用時則無需再指定版本:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
</dependencies>

值得提醒的是,若在父 POM 中直接配置所有依賴,子模組繼承時將被迫依賴全部庫,違反「最小依賴」原則,因此更推薦使用 dependencyManagement 的方式。

三、發布管理

1. Web 類專案

Maven 原生支援 packaging 為 war,直接打包即可,此處不再贅述。

2. 非 Web 類專案(獨立 Java 進程)

這類專案通常使用 maven-assembly-plugin 打包。它提供了簡單的 jar-with-dependencies 方式,將所有相依庫打入一個大 jar 並指定主類,但有兩個明顯缺點:

  1. 第三方 jar 抽取容易衝突,例如 Spring 3.x 就不支援這種方式,需要其他插件配合合併配置檔,較為麻煩。
  2. 不便於單獨升級某個第三方 jar。

因此更推薦另一種做法:將第三方相依 jar 抽到獨立的 lib 目錄,並把專案配置檔抽出、避免被打入 jar(方便部署與運維時修改)。最終打包目錄結構如下:

xxxxxxx-version-all/
├── bin/
│   ├── start.sh
│   └── stop.sh
├── lib/
│   └── xxx-1.0.2.jar
└── cfg/
    ├── xx.xml
    └── xx.properties

其中 bin 存放啟動與停止腳本,lib 存放自身與第三方 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>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${v.plugin.assembly}</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

在 assembly.xml 中指定具體的打包方式:

<assembly>
    <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. 共享庫專案

發布供其他專案使用的獨立 jar,其特點是版本迭代快、管理複雜,通常用 maven-release-plugin 管理。典型流程為:

  1. 為發布版本打 tag 並提交到版本庫。
  2. 更新所有模組 POM 的版本號為下一個指定版本。
  3. 將 tag 出來的版本部署到私有或公共 Maven 倉庫。

父 POM 的插件與 SCM 配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>${v.plugin.release}</version>
            <configuration>
                <tagBase>https://xxxxxx/tags/xxx</tagBase>
                <useReleaseProfile>false</useReleaseProfile>
            </configuration>
        </plugin>
    </plugins>
</build>
<scm>
    <developerConnection>scm:svn:https://xxxxxxxx/branches/xxx/</developerConnection>
</scm>

自動部署到 Maven 倉庫的配置:

<distributionManagement>
    <snapshotRepository>
        <id>repository.snapshots</id>
        <name>repository.snapshots</name>
        <url>https://xxxxxx/libs-snapshots</url>
    </snapshotRepository>
    <repository>
        <id>repository.release</id>
        <name>repository.release</name>
        <url>https://xxxxxx/libs-releases</url>
    </repository>
</distributionManagement>

並在 Maven 安裝目錄下的 conf/settings.xml 中配置倉庫帳號密碼:

<servers>
    <server>
        <id>repository.snapshots</id>
        <username>xxxx</username>
        <password>*****</password>
    </server>
    <server>
        <id>repository.release</id>
        <username>xxxx</username>
        <password>*****</password>
    </server>
</servers>

常用發布命令:

# 空跑演練,不改動任何內容
mvn release:prepare -DdryRun=true

# 若相依第三方 snapshot 會阻止發布,可強制發布
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 兩條命令即可完成整個發布流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved