Maven、私のアーティファクトはどこにありますか? 依存関係管理に関する別の記事

中央リポジトリにアクセスできる場合、または企業が1つの企業リポジトリを持っている場合、Mavenと一緒に暮らすのは簡単です。 閉ループで作業するとすべてが変わり、リポジトリの数は100に近くなります。 カットの下には、失われたアーティファクトを探す場所と、このためにMavenを準備する方法に関するストーリーがあります。

どうなるの?

この記事には、設定の設定、pom、リポジトリでの検索の説明の例が含まれます。 ネクサス\アーティファクトの構成、それらに関連する問題はありません。

簡単なことから始めましょう


pomにspringの依存関係を追加します。

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.0.RELEASE</version> </dependency> 

pom.xml
 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>dependency-testing</artifactId> <version>1.0.0</version> <name>Dependency test</name> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.0.RELEASE</version> </dependency> </dependencies> </project> 


Mavenをインストールしたばかりで、設定を変更しなかったとします。 Mavenアセンブリまたはideを介して依存関係検索を初期化した後、mavenは最初に${user.home}/.m2/repository/ディレクトリのローカルリポジトリにアーティファクトの存在を確認し${user.home}/.m2/repository/

ローカルリポジトリにアーティファクトがない場合、リクエストは中央リポジトリに送られます。


このリポジトリは指定しませんでしたが、アセンブリ中にMavenによって常に追加されます。

settings.xmlを構成する

settings.xml
 <?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <profiles> <profile> <repositories> <repository> <id>nexus-corp</id> <name>nexus-corp-repo</name> <url>http://nexus.mycompany.com:8081/nexus/central/</url> </repository> </repositories> <id>nexus</id> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings> 


settings.xmlが必要なのはなぜですか?
その中で、リポジトリ、ミラー、プロキシ、環境変数などを設定できます。 一度処方して、すべてのプロジェクトで使用する(理想的な世界)
settings.xmlの詳細

この構成では、検索順序はデフォルトの構成に似ていますが、追加の手順が表示されます。 centralに進む前に、Mavenはnexus-corpリポジトリからアーティファクトをダウンロードしようとします。



nexus-corpリポジトリのミラーを追加します。

 <mirror> <id>nexus-corp-mirror</id> <name>nexus-corp-mirror</name> <url>http://nexus.mirror.mycompany.com:8081/nexus/central/</url> <mirrorOf>nexus-corp</mirrorOf> </mirror> 

いつミラーが必要ですか?
  • 利用できない外部リポジトリ
  • 外部トラフィックを節約する
  • 設定からリポジトリを上書きするには\ pom

ミラーの詳細

Mavenがnexus-corpリポジトリの検索ステップに到達すると、代わりにnexus-corp-mirrorリポジトリでアーティファクトを見つけようとします。

さらに、もし彼がnexus-corp-mirrorでそれを見つけなければ、 nexus-corpにはリクエストはありません。


また、ワイルドカードを使用してミラーを追加すると、他のミラーが指定されていない限り、すべての要求がそれに送信されます。

 <mirror> <id>all</id> <name>nexus-corp-mirror</name> <url>http://nexus.mirror.mycompany.com:8081/nexus/central/</url> <mirrorOf>*</mirrorOf> </mirror> 

検索順序


一般に、検索スキームは次のようになります。

1.ローカルリポジトリで検索する
2.優先順位を考慮して(またはそのミラーで)宣言の順にリポジトリを検索します

最後に、 セントラルが追加されます。 上書きされない限り、常に最後です。


構成のマージ


アセンブリを実行する前に、mavenは構成を「接着」します。


優先設定では、少し恥ずかしかった。 ドキュメントには、
前のsettings.xmlは、ユーザー設定とも呼ばれます。 両方のファイルが存在する場合、それらのコンテンツはマージされ、ユーザー固有のsettings.xmlが優先されます。
実際、接着時に競合がなかった場合、リポジトリの優先順位は次のとおりで、降順です。


以下の例から、順序は次のようになります。


global-settings.xml
 <?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <profiles> <profile> <repositories> <repository> <id>repo-global-setting1</id> <url>http://nexus.mycompany.com:8081/nexus/repo-global-setting1-url/</url> </repository> </repositories> <id>g-nexus</id> </profile> </profiles> <activeProfiles> <activeProfile>g-nexus</activeProfile> </activeProfiles> </settings> 


user-settings.xml
 <?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <profiles> <profile> <repositories> <repository> <id>repo-user-setting1</id> <url>http://nexus.mycompany.com:8081/nexus/repo-user-setting1-url/</url> </repository> <repository> <id>repo-user-setting2</id> <url>http://nexus.mycompany.com:8081/nexus/repo-user-setting2-url/</url> </repository> </repositories> <id>nexus</id> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings> 


pom.xml
 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>dependency-testing</artifactId> <version>1.0.0</version> <name>Dependency test</name> <repositories> <repository> <id>repo-pom1</id> <url>http://nexus.mycompany.com:8081/nexus/repo-pom1-url/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> </dependencies> </project> 


紛争の解決により、さらに大きな誤解が生じました。 異なるプロファイルで1つのIDを持つリポジトリを宣言する場合、グローバル構成が優先されますが、同じIDを持つプロファイルを宣言する場合、ユーザーが優先されます。 私はもう実験し始めませんでした。

不足しているものを検索


エラーをどうするかCould not resolve dependencies for project myproject:jar:1.0.0: Failed to collect dependencies at com.someproject:artifact-name:jar:1.0.0か?

1.成果物の正しい名前とバージョンを確認します。
2.設定\ pomにリストされているリポジトリ\ミラーを確認します。
3.これらのリポジトリでアーティファクトを確認します

通常、問題は2番目のステップで解決されますが、これらのポイントに加えてプロキシによる問題もありますが、ダウンロードしたjarが破損することもあります(私自身は一度しか経験していませんが)。

スナップショットバージョンの場合、 -Uコマンドが便利です-スナップショットの依存関係を強制的に更新します。 デフォルトでは、mavenは1日1回タイムアウトが経過した後にのみそれらを更新します(パラメーターupdatePolicy)

キャスト


maven 3.5.0
jdk 1.8
debug maven

Source: https://habr.com/ru/post/J339902/


All Articles