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怎麼配置的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。