LoginSignup
3
2

More than 3 years have passed since last update.

mvn deployで認証エラー(Unauthorized)を解消する ~ デプロイ例と共に

Last updated at Posted at 2019-08-14

はじめに

ローカルのサードパーティライブラリーを、自分で構築したNexusにdeployしたときに認証エラーにひっかかったので、参考になればとデプロイの過程を説明しながら記載しておきます。

SpringFrameworkのライブラリをデプロイする例

例えば、SpringFrameworkを利用していて、以下のSpringFrameworkのjarを自分のNexusにDeployしたいケースです。

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.1.9.RELEASE</version>
</dependency>

アップロード先のnexusリポジトリを確認

今回、例示用のNexusはdockerを利用してローカルに構築しました(使い捨てって便利)。
https://hub.docker.com/r/sonatype/nexus/

サードパーティーのライブラリなので、thirdpartyのリポジトリに上げることにします。
Selection_918.png

ローカルにjarを用意

spring-web-5.1.9.RELEASE.jarをローカルに用意します。
好きな場所からダウンロードしてきましょう。
今回はこちらからダウンロードしました。
https://search.maven.org/artifact/org.springframework/spring-web/5.1.9.RELEASE/jar

settings.xmlを用意

Nexusをmvnコマンドで操作するためには認証が必要です。でないと誰でもアップロードしたり、削除したりできてしまいすから。
そのために以下のようなsettings.xmlファイルを用意します。場所はホームディレクトリ配下にある.m2ディレクトリ以下です(任意の場所に置いたとしてもmvn実行時に指定できます)。

このserversのところで認証情報を指定します。idはNexusのリポジトリIDです。今回はthirdpartyというIDのリポジトリにアップロードするので、idを合わせてあります(ここが一番ひっかかりました)。
usernameとpasswordですが、deployment/deployment123というのはNexusにデフォルトで用意されているデプロイ用のユーザーです。本運用する際には、パスワードの変更などをしておきましょう。

settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <servers>
    <server>
     <id>thirdparty</id>
     <username>deployment</username>
     <password>deployment123</password>
    </server>
  </servers>
</settings>

この際にidが異なっていたり、アカウントが間違えていたりすると以下のように認証エラーが発生します。

Return code is: 401, ReasonPhrase: Unauthorized.
エラー文を改行して例示
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:
deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: 
Could not transfer artifact org.springframework:spring-web:jar:5.1.9.RELEASE 
from/to thirdparty (http://localhost:8081/nexus/content/repositories/thirdparty/): 
Failed to transfer file: http://localhost:8081/nexus/content/repositories/thirdparty/org/springframework/spring-web/5.1.9.RELEASE/spring-web-5.1.9.RELEASE.jar.
Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]

デプロイしてみる

mvnのバージョンは3.3.1を利用しています。

Deployの仕方の詳細は公式をご覧下さい。
https://maven.apache.org/plugins/maven-deploy-plugin/usage.html

mvn -s ~/.m2/settings.xml deploy:deploy-file \
  -DgroupId=org.springframework -DartifactId=spring-web \
  -Dversion=5.1.9.RELEASE -Dpackaging=jar 
  -Dfile=/path/to/spring-web-5.1.9.RELEASE.jar -DrepositoryId=thirdparty \
  -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/

実行結果
Selection_919.png

Selection_920.png

このようにNexusにデプロイすることができました。

最後に

権限を持っていればNexusの画面からも手動でアップロードは可能ですが、オペミスが発生することを考えるとオススメできません。
またsettings.xmlにパスワードの平文を記載したままでの本運用もオススメできないので、mavenが用意している暗号化も導入しましょう。
http://maven.apache.org/guides/mini/guide-encryption.html

以上

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2