mavenの依存関係の連携先をNEXUSに変更した時のエラー

mavenの依存関係の連携先をNEXUSに変更した時のエラー

mavenの依存関係の連携先をNEXUSに変更した時のエラー

調べてみたら超簡単なこと・・・というより知識不足が招いたようなものだったのですけど、ハマったので書いておきます。

事象

mavenプロジェクトのpom.xmlで設定した依存関係の連携先を、いつものセントラルリポジトリ(http://repo1.maven.org/maven2/)から、社内用のプライベートリポジトリ(NEXUSで管理してます)に変更した。

NEXUSとは・・・
Sonatype Nexus。プライベートリポジトリを構築・管理するためのソフトウェア
公式(http://www.sonatype.org/nexus/
詳しい話はこちらの方を見てもらうと分かるかもしれません。
Sonatype Nexus で Maven リポジトリを構築しよう (1.概要編)

変更したのはmavenのsettings.xmlだけ。
C:\Users\【ユーザー】\.m2\settings.xml

修正前(社内のネットワークなのでプロキシ設定が入ってます)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">

~omittied~

 <proxies>
   <proxy>
     <id>myproxy</id>
     <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

修正後
NEXUSのサーバーのアクセス設定と、ミラー設定を行いました。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
 
~omittied~

 <proxies>
   <proxy>
     <id>myproxy</id>
     <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>

  <servers>
    <server>
        <id>nexus</id>
        <username>username</username>
        <password>password</password>
    </server>
  </servers>

  <mirrors>
    <mirror>        
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>https://puge.hogehoge.jp/nexus/repository/</url>
    </mirror>
  </mirrors>
</settings>

エラー内容

Spring tool suite(eclipse)でmavenプロジェクトを起動してみる。
しかし、リポジトリからjarファイルが取得できていない。何故だ。。。

jarファイルが取得されるはずのディレクトリにいって見てみると、jarファイルが存在しない。
代わりに?以下のファイルがいた。
hogehoge-puge.pom.lastUpdated
なんだこいつは!とりあえず中身を見てみる。

#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice.
#Mon Sep 04 15:03:32 JST 2017
cd1ab7e1b294b0fdf3ccfebd1aca8ecb843d92cb@proxy.somewhere.com\:8080>0fbad087be4a40bcae4645dabbf9387e30d44792@default-nexus-https://hogehoge.jp/nexus/repository/.lastUpdated=1504505012627
https\://puge.hogehoge.jp/nexus/repository/.error=Could not transfer artifact jp.hogehoge\:hogehoge-puge\:pom\:1.0.0 from/to nexus (https\://puge.hogehoge.jp/nexus/repository/)\: Connection reset

なんかerrorってよく見たら書いてあった!
error=Could not transfer artifact
うむ・・?とりあえずググる。

原因

ヒットした記事が。
pom.xmlエラー発生時の解決方法
この記事によると、

解決方法

1.eclipseをいったん落とす

2.{ユーザ名}/.m2/repository/org/apache フォルダーをエクスプローラで開く

3.maven フォルダーを削除する(一応バックアップ等とっておいた方が無難かと)

4.eclipseを再起動する

5.エラーを吐いていたxmlを持つプロジェクトを選択して右クリック
Maven>プロジェクトの更新 をクリック

なるほど~?
とりあえず言われた通りにやってみるが解決せず・・・
追記には「settings.xml見直せや!」と。
はい・・・

そして数日間ハマり続け、ついに解決!
原因はプロキシ設定とNEXUSの設定の競合?ぽい感じでした。

解決策

setting.xmlの設定を以下に変更

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                     https://maven.apache.org/xsd/settings-1.0.0.xsd">
 
    <server>
        <id>nexus</id>
        <username>username</username>
        <password>password</password>
    </server>
  </servers>

  <mirrors>
    <mirror>        
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>https://puge.hogehoge.jp/nexus/repository/</url>
    </mirror>
  </mirrors>
</settings>

つまりプロキシ設定を削除しました。これで実行してみると、無事jarファイルを取ってこれました。
しかし、セントラルリポジトリにも繋ぎにいきたいんじゃー。
ということで、プロキシ設定のオプションとして【nonProxyHosts】なるものが。
そして修正。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
 
~omittied~

 <proxies>
   <proxy>
     <id>myproxy</id>
     <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org|*.hogehoge.jp</nonProxyHosts>
    </proxy>
  </proxies>

  <servers>
    <server>
        <id>nexus</id>
        <username>username</username>
        <password>password</password>
    </server>
  </servers>

  <mirrors>
    <mirror>        
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>https://puge.hogehoge.jp/nexus/repository/</url>
    </mirror>
  </mirrors>
</settings>

無事通りました。

mavenカテゴリの最新記事