1. 程式人生 > >利用mvn deploy命令上傳包

利用mvn deploy命令上傳包

在PATH里加入maven的bin的路徑

配置完畢後,在Windows命令提示符下,輸入mvn -v測試一下。是否安裝成功。

mvn:deploy在整合或者釋出環境下執行,將最終版本的包拷貝到遠端的repository,使得其他的開發者或者工程可以共享。

以將ojdbc14傳到nexus中的thirdparty為例

一 配置settings.xml

因為nexus是需要登陸操作,當然可以通過配置免登陸,這是後話。
在settings.xml的

<server>   
<id>thirdparty</id>   
<username>
admin</username> <password>admin123</password> </server>

當然如果你要上傳包去其他倉庫,可依照此例,如

<server>   
<id>central</id>   
<username>admin</username>   
<password>admin123</password>   
</server>

如果進行deploy時返回Return code is: 401錯誤,則需要進行使用者驗證或者你已經驗證的資訊有誤。

二 cmd輸入命令

mvn deploy:deploy-file -DgroupId=com.vip.vfc -DartifactId=common -Dversion=1.0.0 -Dpackaging=jar -Dfile=E:\common.jar -Durl=http://localhost:8081/nexus/content/repositories/releases -DrepositoryId=releases

DgroupId和DartifactId構成了該jar包在pom.xml的座標,專案就是依靠這兩個屬性定位。自己起名字也行。
Dfile表示需要上傳的jar包的絕對路徑。
Durl私服上倉庫的位置,開啟nexus——>repositories選單,可以看到該路徑。
DrepositoryId伺服器的表示id,在nexus的configuration可以看到。
Dversion表示版本資訊,怎樣得到一個jar包準確的版本呢?
解壓該包,會發現一個叫MANIFEST.MF的檔案,這個檔案就有描述該包的版本資訊。
比如Manifest-Version: 1.0可以知道該包的版本了。

上傳成功後,在nexus介面點選3rd party倉庫可以看到這包。

首先在Nexus中建立一個自己私有的倉庫,步驟為Repositories –> Add –> Hosted Repository,在頁面的下半部分輸入框中填入Repository ID和Repository Name即可,比如分別填入myrepo和 my repository,點選save就建立完成了。

如果要把構建部署至私服中,需要在構建的 pom.xml 檔案增加 distributionManagement 配置項,有多種協議可以用來部署構建,這裡主要講兩種。

第一種配置如下: 
<project> 
[...] 
<distributionManagement> 
<repository> 
<id>myrep</id> 
<name>my repository</name> 
<url>file:/usr/local/mvn-private-server/nexus-oss-webapp-1.9.2.3/./../sonatype-work/nexus/storage/myrep/</url>
</repository> 
</distributionManagement> 
[...] 
</project> 

說明:其中id就是需要部署構建的倉庫Id,name似乎不重要,可以隨便輸入,url就是倉庫的configuration中Default Local Storage Location項的對應的資訊。採用此種方法,伺服器不會對部署操作進行認證,同時配置也把倉庫的物理儲存地址完全暴露,感覺不是太好。

第二種配置如下: 
<project> 
[...] 
<distributionManagement> 
  <repository> 
<id>tagphi</id> 
<url>http://nexus-server-ip:8081/nexus/content/repositories/myrep</url> 
  </repository> 
</distributionManagement> 
[...] 
</project> 

說明:上述的distributionManagement資訊可以在對於的倉庫的Summary中找到。

配置好後,此時執行mvn deploy,部署應該不會成功,根據提示資訊,重新執行 mvn deploy -e 或 mvn deploy -X,此時能看到具體的錯誤資訊,報Return code is: 401錯,這個是因為釋出者沒有許可權,需要把使用者認證資訊配置在maven的settings.xml中,該檔案在mavne安裝包下的conf目錄下,這是一個全域性配置,同時可以把該檔案複製到 userdir/.m2 目錄下,這樣配置就只對當前使用者生效。在settings.xml檔案中servers段中新增如下資訊

  <server> 
      <id>myrep</id> 
      <username>deployment</username> 
      <password>password</password> 
    </server> 

其中id就是部署倉庫的id,username是作為部署使用者,Nexus系統預設的deployment,密碼為deployment123,可以在security — users中找到,如果需要設定密碼,在使用者列表中找到該使用者,在該使用者上點選右鍵,會出來一個選單供重置密碼或修改密碼。此時在執行 mvn deploy,應該可以看到成功釋出了。當第二次執行 mvn deploy 又失敗,這次失敗原因是Return code is: 400,該錯誤的原因是在建立倉庫時在configuration中的Deployment Policy設定為了Disable Redeploy,修改為Allow Redeploy即可。

私服搭建
1.確定已安裝maven和jdk環境
2.下載nexus http://www.sonatype.org/nexus/go
3.解壓後進入/bin/jsw目錄
4.根據本機作業系統選擇相應版本,此處以64位windows為例
5.雙擊install-nexus.bat安裝nexus服務,然後雙擊start-nexus.bat啟動服務
6,訪問http://localhost:8081/nexus/,出現下面頁面表示安裝成功
7.點選右上角的Log in,輸入預設賬號,密碼
8.點選Server配置伺服器資訊,勾選Http Proxy Settings,並設定代理伺服器
9.新增第三方jar包,nexus提供了3rd party、Snapshots、Releases這三個目錄存放第三方jar包
10.上傳第三方jar包
11.確定填寫無誤後,點選Add Artifact(可新增多個),然後點選Upload Artifact上傳所有jar包
12.上傳成功後在可在Releases中檢視
13.在其他專案的pom.xml中引用

<repositories>
    <repository>
        <id>nexus</id>
        <name>Team Nexus Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
</repositories>
<!-- 外掛地址 -->
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Team Nexus Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </pluginRepository>
</pluginRepositories>
<!-- 引用的jar包 -->
<dependencies>
    <dependency>
        <groupId>com.fibre.hello</groupId>
        <artifactId>helloworld</artifactId>
        <version>0.0.1-Releases</version>
    </dependency>
</dependencies>

從Maven專案中匯出專案依賴的jar包
  一、匯出到預設目錄 targed/dependency
  從Maven專案中匯出專案依賴的jar包:進入工程pom.xml 所在的目錄下,執行如下命令:
  1、mvn dependency:copy-dependencies或在eclipse中,選擇專案的pom.xml檔案,點選右鍵選單中的Run As,見下圖紅框中,在彈出的Configuration視窗中,輸入 dependency:copy-dependencies後,點選執行;
  2、maven專案所依賴的jar包會匯出到targed/dependency目錄中。
  二、匯出到自定義目錄中
  在maven專案下建立lib資料夾,輸入以下命令:
  1、mvn dependency:copy-dependencies -DoutputDirectory=lib;
  2、maven專案所依賴的jar包都會複製到專案目錄下的lib目錄下。
  三、設定依賴級別
  同時可以設定依賴級別,通常使用compile級別
  mvn dependency:copy-dependencies -DoutputDirectory=lib -DincludeScope=compile