1. 程式人生 > 程式設計 >Maven deploy配置方法詳解

Maven deploy配置方法詳解

作用

在本地的pom檔案配置好之後,執行deploy命令,可以將maven所打的jar包上傳到遠端的repository,便於其他開發者和工程共享。

pom.xml配置

首選,在pom檔案中project標籤下新增如下程式碼:

<distributionManagement> 
  <repository> 
   <id>releases</id> 
   <name>Internal Releases</name> 
   <url>http://localhost:8081/nexus/content/repositories/thirdparty</url> 
  </repository> 
  <snapshotRepository> 
   <id>releases</id> 
   <name>Internal Releases</name> 
   <url>http://localhost:8081/nexus/content/repositories/thirdparty</url> 
  </snapshotRepository>
 </distributionManagement> 

此時,執行deploy命令,會返回401錯誤,則需要使用者驗證或驗證的資訊有誤。

setting.xml檔案配置

在setting配置檔案的servers標籤下新增如下程式碼:

  <server> 
    <id>releases</id> 
    <username>admin</username> 
    <password>admin</password> 
   </server> 

PS:其中此處的id,必須為pom檔案中配置的repository的id。

注意事項

一般繼承 parent 的version會按照如下格式寫:

  <parent> 
    <groupId>module.some</groupId> 
    <artifactId>module_parent</artifactId> 
    <version>${parent.version}</version> 
  </parent> 

這樣寫方便統一管理版本資訊,但釋出到maven私服之後,maven 會試圖下載 module_parent 的 ${parent.version} 的 jar。顯然,這個jar是不存在的。那麼為什麼已經指定了 parent.version 的值了卻沒有解析呢?這是因為deploy 的過程中,parent 標籤裡的變數是不會解析的,必須是一個常量。

結果

執行maven deploy命令成功之後,登入私服進行查詢,即可看到對應的jar包。

maven deploy命令打包到私服

<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.zeelan.app</groupId>
  <artifactId>seller-auth</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!-- 專案編碼 -->
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <lombok.version>1.16.10</lombok.version>
    <mybatis.paginator.version>1.2.17.2</mybatis.paginator.version>
    <hibernate.validator.version>5.1.2.Final</hibernate.validator.version>
    <validation.api.version>1.1.0.Final</validation.api.version> 
  </properties>

  <!-- 遠端倉庫地址 -->
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <name>Team Nexus Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/groups/public</url>
    </pluginRepository>
  </pluginRepositories>

  <!-- 配置遠端釋出到私服,mvn deploy -->
  <distributionManagement>
    <!-- 定義releases庫的座標 -->
    <repository>
      <id>releases</id>
      <name>Nexus Release Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/repositories/releases/</url>
    </repository>
    <!-- 定義snapshots庫 -->
    <snapshotRepository>
      <id>snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

  <!-- 專案有依賴的所有jar座標配置 -->
  <dependencies>
    <!-- lombok jar -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
    </dependency>

    <!-- mybatis分頁外掛jar -->
    <dependency>
      <groupId>com.github.miemiedev</groupId>
      <artifactId>mybatis-paginator</artifactId>
      <version>${mybatis.paginator.version}</version>
    </dependency>
    
    <!-- hibernate validator驗證jar -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>${hibernate.validator.version}</version>
    </dependency>
    <!-- hibernate validtor需要的依賴 jar -->
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>${validation.api.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>seller-auth</finalName>
    <!-- 外掛庫宣告 -->
    <plugins>
      <!-- 配置執行環境JDK版本 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <!-- 將原始碼打包外掛 -->
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <attach>true</attach>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- deploy時只上傳jar包到遠端倉庫的配置 -->
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <!-- skip預設deploy外掛的執行 -->
            <configuration>
              <skip>true</skip>
            </configuration>
          </execution>
          <execution>
            <id>deploy-file</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy-file</goal>
            </goals>
            <configuration>
              <!-- 開發階段上傳到snapshot倉庫,上線階段上傳到release倉庫 -->
              <repositoryId>${project.distributionManagement.snapshotRepository.id}</repositoryId>
              <url>${project.distributionManagement.snapshotRepository.url}</url>
              <file>${project.build.directory}/${project.artifactId}.jar</file>
              <groupId>${project.groupId}</groupId>
              <artifactId>${project.artifactId}</artifactId>
              <version>${project.version}</version>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

執行 mvn deploy就能打包到私服上了!

mvn -clean配置清除外掛,然後在執行命令可以清除target下的檔案

mvn-clean package 本地打包,jar/war/等根據<packaging>jar/war</packaging>控制

mvn -e 檢視打包過程的錯誤資訊

mvn -v檢視mavne版本資訊等等

到此這篇關於Maven deploy配置方法詳解的文章就介紹到這了,更多相關Maven deploy配置內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!