javascript
Spring Boot 多模块与 Maven 私有仓库
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
前言
系統(tǒng)復(fù)雜了,抽離單一職責(zé)的模塊幾乎是必須的;若需維護(hù)多個(gè)項(xiàng)目,抽離公用包上傳私有倉庫管理也幾乎是必須的。其優(yōu)點(diǎn)無需贅述,以下將記錄操作過程。
1. 多模塊拆分
在.NET 中由于其統(tǒng)一性,實(shí)現(xiàn)上更自然一點(diǎn)。Spring Boot 通過 Maven 構(gòu)建多模塊工程也不麻煩,假如我的項(xiàng)目中包含以下幾個(gè)包:
我需要將他們分別拆分成獨(dú)立模塊,首先要修改的是根目錄下的 pom.xml,packaging 類型改為 pom,并添加 modules 節(jié)點(diǎn):
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.youclk.multi-package</groupId><artifactId>parent</artifactId><version>0.0.1-SNAPSHOT</version><modules><module>api</module><module>service</module><module>dao</module></modules><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><lombok>1.16.20</lombok></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok}</version></dependency></dependencies></project>之后新建一個(gè)個(gè) Module,將對(duì)應(yīng)的代碼移植過去:
需要注意的是在啟動(dòng)模塊的 pom.xml 中需要指定啟動(dòng)類:
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.youclk.multipackage.api.MultiApplication</mainClass><layout>ZIP</layout></configuration></plugin></plugins> </build>統(tǒng)一升級(jí)版本命令:mvn versions:set -DnewVersion=0.0.1-SNAPSHOT,到此差不多完成了,引用方式與普通的依賴包一致:
<dependency><groupId>com.youclk.multi-package</groupId><artifactId>service</artifactId><version>0.0.1-SNAPSHOT</version> </dependency>2. Nexus3 私有倉庫搭建
Docker 時(shí)代一切都變得異常簡單,Compose 配置如下:
version: '3.5'services:nexus:image: sonatype/nexus3:3.10.0networks:- proxy- youclkvolumes:- /mnt/nas/db/nexus-data:/nexus-datadeploy:mode: replicatedlabels:- com.df.notify=true- com.df.port=8081- com.df.serviceDomain=nexus.youclk.comrestart_policy:condition: anymax_attempts: 3update_config:delay: 5sorder: start-firstresources:limits:cpus: '0.50'memory: 1gnetworks:proxy:external: trueyouclk:external: true啟動(dòng)過程需要一分鐘左右:
需要注意的是如果你的 ssl 是在負(fù)載均衡或者其他的反向代理之上,那么必須在 HTTP 頭中指定 X-Forwarded-Proto 傳輸協(xié)議為 HTTPS,然后,就可以愉快地玩耍了。
3. 上傳與引用
3.1 上傳
首先需要在 Nexus 創(chuàng)建私有倉庫,例如我的:
其次在本地 maven 設(shè)置中添加 server 節(jié)點(diǎn),默認(rèn)在 ~/.m2/settings.xml:
<servers><server> <id>youclk</id> <username>admin</username><password>youclk</password> </server> </servers>pom.xml 中添加上傳地址:
<distributionManagement><repository><id>nexus</id><name>Releases Repository</name><url>https://nexus.youclk.com/repository/youclk-releases/</url></repository><snapshotRepository><id>nexus</id><name>Snapshot Repository</name><url>https://nexus.youclk.com/repository/youclk-snapshots/</url></snapshotRepository> </distributionManagement>最后執(zhí)行 mvn clean deploy 便會(huì)上傳至私有倉庫,單獨(dú)傳包命令如下:
mvn deploy:deploy-file -DgroupId=com.youclk -DartifactId=utils -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=target/utils-0.0.1-SNAPSHOT.jar -Durl=https://nexus.youclk.com/repository/youclk/ -DrepositoryId=youclk管理和查看:
3.1 引用
Finally,最后的最后就是怎么使用啦~ 如果需要全局引用的話需要在 settings.xml 添加和激活倉庫:
<?xml version="1.0" encoding="UTF-8"?> <settings><mirrors> <mirror><id>aliyun</id><mirrorOf>central</mirrorOf><name>central mirror</name><url>http://maven.aliyun.com/mvn/repository</url></mirror><mirror><id>nexus</id><mirrorOf>maven-public</mirrorOf><name>private mirror</name><url>http://local-nexus.youclk.com/repository/maven-public/</url></mirror></mirrors> <servers><server> <id>nexus</id> <username>admin</username><password>youclk</password> </server></servers><profiles> <profile> <id>nexus</id> <repositories> <repository> <id>maven</id> <name>local private nexus</name> <url>http://local-nexus.youclk.com/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>maven</id> <name>local private nexus</name> <url>http://local-nexus.youclk.com/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile></profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>不過一般不推薦這么寫,settings.xml 應(yīng)該盡可能保持簡潔,精簡配置,此處留下代理和權(quán)限認(rèn)證即可,其余的可以移植到 pom.xml 中:
<repositories><repository><id>aliyun</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository><repository><id>nexus</id><url>http://local-nexus.youclk.com/repository/maven-public/</url></repository> </repositories> <pluginRepositories><pluginRepository><id>central</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url></pluginRepository><pluginRepository><id>maven-public</id><url>http://local-nexus.youclk.com/repository/maven-public/</url></pluginRepository> </pluginRepositories>轉(zhuǎn)載于:https://my.oschina.net/u/3721254/blog/1806857
總結(jié)
以上是生活随笔為你收集整理的Spring Boot 多模块与 Maven 私有仓库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 中strong,weak,cop
- 下一篇: Java环境变量的配置 (Win10环境