📜 ⬆️ ⬇️

Maven, where are my artifacts? Another article about dependency management

It's easy to live with maven when you have access to the central repository, or the company has one corporate repository. Everything changes if you work in a closed loop, and the number of repositories is closer to a hundred. Under the cut there is a story about where to look for a lost artifact and how to prepare a maven for this.

What will happen?

The article will have examples of settings, pom, description of the search in the repositories. There will be no nexus \ artifactory settings, problems associated with them.

Start simple


Add a spring dependency in your pom.
')
<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> 


Suppose we just installed maven and did not change any settings. After initializing the search for dependencies through the maven build, or via ide, maven first checks for the presence of the artifact in the local repository in the ${user.home}/.m2/repository/ directory.

If there is no artifact in the local repository, the request will go to the central repository.


We did not specify this repository, but it is always added by maven when building.

Configuring 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> 


Why do I need settings.xml?
You can configure repositories, mirrors, proxies, environment variables, etc. Register once and use in all projects (in an ideal world)
Learn more about settings.xml

With this configuration, the search order will be similar to the default configuration, but an additional step will appear. Before going to central , maven will try to download an artifact from the nexus-corp repository.


Mirror


Add a mirror for the nexus-corp repository.

 <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> 

When do you need a mirror?
  • external repository is not available
  • to save external traffic
  • to override the repository from settings \ pom

More about mirrors

When maven reaches the search step in the nexus-corp repository, it will try to find an artifact in the nexus-corp-mirror repository instead.

Moreover, if he does not find it in nexus-corp-mirror , then there will be no request in nexus-corp .


And if you add a mirror with a wildcard, then all requests will be sent to it, unless other mirrors are indicated.

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

Search order


In general, the search scheme will be:

1. Search local repo
2. Search in repositories in the order of announcement, taking into account the priority (or in their mirrors)

At the end is added central . He is always the last, if not overwritten.


Merge configurations


Before performing the build, maven sticks together configs:


With a configuration priority, I had a slight embarrassment. The documentation says:
Global settings, the latter settings.xml. If both files exist, their contents will be dominant.
In fact, if there were no conflicts during gluing, the repository priority is as follows, in decreasing order:


From the examples below the order will be as follows:


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> 


With the resolution of conflicts, even more misunderstanding appeared. If you declare a repository with the same id in different profiles, then the global configuration takes precedence, but if you declare profiles with the same id, then the user has priority. I did not begin to experiment further.

Search for missing


So what to do with the error 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. Check the correctness of the name and version of the artifact.
2. View which repositories \ mirrors are listed in your settings \ pom.
3. Check for artifact in these repositories.

Usually, the problem is solved in the second step, but in addition to these points there are problems due to the proxy, it also happens that the downloaded jar is damaged (although I myself have encountered this only once).

For the snapshot versions, the -U command is useful - enforcing a snapshot update of dependencies. By default, maven updates them only after the timeout expires once a day (updatePolicy parameter)

Cast


maven 3.5.0
jdk 1.8
debug maven

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


All Articles