4.1 dependency

Now let's look at another thing that made maven so popular - dependency management.

If you want to add some library to your Maven project, you just need to add it to the pom file, in the dependencies section . It looks to the point of being simple.

Let's add the latest version of Spring and Hibernate to our project. Here's what it will look like:

<dependencies>
 
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
	<version>5.3.18</version> 
  </dependency>

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.0.0.Final</version>
  </dependency>

</dependencies>

That's it, you don't need to do anything else . If you add these lines to your project, IDEA will immediately download the required libraries. A few seconds after that, you can use their classes in your code.

An important point: if you upload the project to GitHub or send it to someone as an archive, this person is guaranteed to be able to build it. All information on libraries, dependencies and build scripts is already hardwired into the project.

4.2 How to search for libraries in the Maven Repository

By the way, I added the XML of these two libraries to my pom.xml in less than a minute. Not bad, right? Now I will teach you how to quickly add any library to the project.

First, there is a central public Maven repository on the Internet , which stores millions of libraries. It is located at the link https://mvnrepository.com/ , you can search for the library you need directly in it.

Maven

Secondly, it can be even simpler - immediately write to Google "maven hibernate" , follow the first link and you will get:

Maven 2

Select the desired version and click on it. Sometimes the latest version contains the Beta suffix, then go for something older.

I chose version 6.0.0.Final and went to the last page.

The green box here is the code that you need to copy into your pom.xml. All.

4.3 dependency repository

When building a project, your Maven will first look for the specified library (artifact) in your local repository. If he does not find it there, then he will look in the global Maven repository. And then upload it to your local repository - to speed up the next build.

But besides these two repositories, there are others.

First, many large companies have maven repositories with their own libraries.

Secondly, before the invention of Docker, many projects were simply put into the corporate Maven repository after being built. And what? Great place to store everything. And versioning is again supported.

In general, if you suddenly decide to connect a third-party repository to your project, then this can be done as simply as adding dependencies:

<repositories>
 
  <repository>
  	<id>public-codegym-repo</id>
  	<name>Public CodeGym Repository</name>
  	<url>http://maven.codegym.cc</url>
  </repository>
 
  <repository>
  	<id>private-codegym-repo</id>
  	<name>Private CodeGym Repository</name>
  	<url>http://maven2.codegym.cc</url>
  </repository>
 
</repositories>

Each repository has 3 things: Key/ID, Name and URL . You can specify any name - it is for your convenience, ID is also for your internal needs, in fact, you only need to specify the URL.

If this is a public repository, then this information is easily googled, if it is a corporate one, then they will give it to you when they give access to such a repository.

The creators of Maven know how to standardize, you can't refuse them.