編輯:關於 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 的方式。
Maven 原生支援 packaging 為 war,直接打包即可,此處不再贅述。
這類專案通常使用 maven-assembly-plugin 打包。它提供了簡單的 jar-with-dependencies 方式,將所有相依庫打入一個大 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>
發布供其他專案使用的獨立 jar,其特點是版本迭代快、管理複雜,通常用 maven-release-plugin 管理。典型流程為:
父 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 兩條命令即可完成整個發布流程。
Android Notification 通知欄訊息實作指南
在 Android 中,Notification 是 App 與使用者溝通的重要方式。無論是下載進度、新訊息提醒,還是常駐服務狀態,都可以透過通知欄呈現。本文整理常見通知
一加3怎麼刷入第三方Recovery?完整刷機步驟教學
一加3刷入第三方Recovery完整流程 刷入第三方Recovery前,需要先解鎖Bootloader,再刷入TWRP等工具。操作會清空數據,請提前備份。 準備工作 進
Mac 電腦如何進行 APK 反編譯——使用 apktool、dex2jar、jd-gui
最近群裡有人問如何在 Mac 下反編譯 APK。以前我都是在 Windows 下操作,那邊工具比較齊全(例如改之理這類集成工具),心想 Mac 下應該也差不多,於是趁著嗑
Android 自動化測試:Espresso 框架入門
單元測試與 UI 自動化測試能協助開發者在早期發現問題,減少人工回歸成本。Android 官方推薦的 Espresso 框架,提供語意化 API,適合驗證使用者操作與畫面