1. 程式人生 > 程式設計 >springboot~nexus專案打包要注意的地方示例程式碼詳解

springboot~nexus專案打包要注意的地方示例程式碼詳解

一個使用maven製作框架包時,會有一個主專案,然後它有多個子專案框架組成,很少一個工具包一個工程,像springboot,springcloud都是這種結構,主專案用來管理一些依賴包的版本,這對於框架型專案來說是很必要的,而對於業務專案來說,因為目前都是推薦使用微服務的輕量方式,所以不建議用多專案繫結一個大專案的方式,而都是一個服務一個專案。

springboot~nexus專案打包要注意的地方示例程式碼詳解

主pom檔案

主專案的pom檔案用來管理依賴包版本,一般在dependencyManagement節點去宣告它們的版本號,這樣在子專案裡可以不宣告相同包的版本資訊了

 <dependencyManagement>
    <dependencies>
      <!--spring boot 版本-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot-dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <!--阿里巴巴元件-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>${spring-cloud-alibaba-dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <!--spring cloud 版本-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <!-- Spring Boot Web 依賴 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot-dependencies.version}</version>
        <exclusions>
          <!-- 排除Tomcat 以使用 Undertow -->
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
          </exclusion>
        </exclusions>
      </dependency>


      <!-- Google 編碼助手 -->
      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
      </dependency>


      <!-- Mysql 驅動 -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.drive.version}</version>
      </dependency>


      <!-- HikariCP 連線池 -->
      <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>${HikariCP.version}</version>
      </dependency>

      <!-- MyBatis 增強工具 -->
      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis-plus-boot-starter.version}</version>
      </dependency>


      <!-- Alibaba json解析器 -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
      </dependency>


      <!-- 介面文件 -->
      <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-swagger2.version}</version>
      </dependency>


      <!-- 介面文件 UI -->
      <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-swagger2.version}</version>
      </dependency>

      <!-- HTTP 客戶端請求 -->
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
      </dependency>


      <!-- Feign 客戶端請求 -->
      <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
        <version>${feign-httpclient.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

如果專案希望進行釋出到nexus私服上,需要配置distributionManagement節點的資訊,它對應你的.m2/settings.xml裡的profile節點資訊

 <distributionManagement>
    <repository>
      <id>releases</id>
      <name>Nexus Release Repository</name>
      <url>http://192.168.0.203:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://192.168.0.203:8081/repository/maven-snapshots/</url>
    </snapshotRepository>

使用deploy釋出專案

第一次把工具包發到nexus時,需要在點選主專案的 deploy它會把主專案和其子專案同時發到nexus上面,後續可以只deploy修改的專案

springboot~nexus專案打包要注意的地方示例程式碼詳解

在具體專案裡使用它

直接在專案的pom裡,新增對應的工具包即可,工具包的專案依賴你不需要關心

 <dependency>
      <groupId>com.lind</groupId>
      <artifactId>lind-common</artifactId>
      <version>${lind-common.version}</version>
 </dependency>

注意:對於框架型專案,需要儲存你的工具包依賴的專案也在nexus上面,否則會導致載入失敗。

到此這篇關於springboot~nexus專案打包要注意的地方的文章就介紹到這了,更多相關springboot nexus專案打包內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!