Spring Boot 多模組與 Maven 私人倉庫

來源:互聯網
上載者:User

標籤:rom   協議   oca   分享   4.0   根目錄   升級版   nas   att   

前言

系統複雜了,抽離單一職責的模組幾乎是必須的;若需維護多重專案,抽離公用包上傳私人倉庫管理也幾乎是必須的。其優點無需贅述,以下將記錄操作過程。

1. 多模組拆分

在.NET 中由於其統一性,實現上更自然一點。Spring Boot 通過 Maven 構建多模組工程也不麻煩,假如我的項目中包含以下幾個包:

我需要將他們分別拆分成獨立模組,首先要修改的是根目錄下的 pom.xml,packaging 類型改為 pom,並添加 modules 節點:

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

之後建立一個個 Module,將對應的代碼移植過去:

需要注意的是在啟動模組的 pom.xml 中需要指定啟動類:

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

統一升級版本命令: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 時代一切都變得異常簡單,Compose 配置如下:

version: '3.5'services:  nexus:    image: sonatype/nexus3:3.10.0    networks:      - proxy      - youclk    volumes:      - /mnt/nas/db/nexus-data:/nexus-data    deploy:      mode: replicated      labels:        - com.df.notify=true        - com.df.port=8081        - com.df.serviceDomain=nexus.youclk.com      restart_policy:        condition: any        max_attempts: 3      update_config:        delay: 5s        order: start-first      resources:        limits:          cpus: '0.50'          memory: 1gnetworks:  proxy:    external: true  youclk:    external: true

啟動過程需要一分鐘左右:

需要注意的是如果你的 ssl 是在負載平衡或者其他的反向 Proxy之上,那麼必須在 HTTP 頭中指定 X-Forwarded-Proto 傳輸協議為 HTTPS,然後,就可以愉快地玩耍了。

3. 上傳與引用3.1 上傳

首先需要在 Nexus 建立私人倉庫,例如我的:

其次在本地 maven 設定中添加 server 節點,預設在 ~/.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>

最後執行 mvn clean deploy 便會上傳至私人倉庫,單獨傳包命令如下:

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 應該儘可能保持簡潔,精簡配置,此處留下代理和許可權認證即可,其餘的可以移植到 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>
小結

最近開了個訂閱號,都已經看到這兒了,再順便關注一下唄~ 有刻,我們共同成長,祝近安:)

Spring Boot 多模組與 Maven 私人倉庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.