Maven addiction

I am really new to maven. I am a bit confused about the dependency function. I know that I can add a dependency in a pom file like this

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> 

What does it mean? Does this mean that I do not need to import jar slf4j files into my project? If so, how does my project access these libraries?

I read about dependency on maven site , but didn't help me much.

Can someone explain this in a simpler way.

thanks

+4
source share
4 answers

Brief description: this means that your project is dependent on slf4j, version 1.6.1.

Further

  • If you are creating your project using Maven (or your IDE is Maven-aware), you do not need to do anything to use slf4j. (Apart from the usual source codes, like a reasonable import statement, etc.).
  • slf4j v. 1.6.1 will be pulled from the default Maven repository to your local repository, which means ...
  • ... ~/.m2/repository is your repository. slf4j will be placed in $M2_HOME/org/slf4j/$(artifactId}/1.6.1 and will include (in general) a jar file, a pom file and a hash file.
  • Slf4j dependencies will also be uploaded to your local repository.
  • Dependencies of these dependencies will be loaded ad infinitum / ad nauseum. (The source for "first use of the library downloads the Internet" if there are many dependencies, not slf4j.) This "transitional dependency management" is one of Maven’s main goals.
+5
source

If you have not used maven, you must manually download and use the dependencies that you need for your project. You will probably place them in the lib folder and specify this location in your development environment, as well as in the build tool.

maven manages these dependencies for you. You specify the dependency of your project in the prescribed format, and maven downloads them for you from the Internet and manages them. When creating your project, maven knows where he placed these dependencies and uses them. Most IDEs also know where these dependencies are when they discover that this is a maven project.

Why is this a big deal? Typically, most open source libraries release new versions on a regular basis. If your project uses them, then every time a newer version is required, you will need to manually download it and manage it. More importantly, each dependency, in turn, can have other dependencies (the so-called transitive dependency). If you are not using maven, you will also need to identify, load and manage these transitive dependencies.

This gets more complicated the more dependencies your project uses. It is possible that two dependencies end up using different versions of a common dependency.

+2
source

There are many servers on the Internet that host artifacts (jars) that you can download as part of the maven build. You can add dependencies as shown above to describe which banks you need to create your code. When maven is about to build, it will contact one of these servers and download the jar to your computer and put it in a local repository, usually

 ${user_home}/.m2/repository 

The servers that must be connected to maven must be configured in your maven project pOM file, in a section like

 <repositories> <repository> </repository> </repositories> 

A prototype server can be seen at repo1.maven.org

The good thing about maven is that if you need a list that you need, it will pull not only this bank, but also any banks that this bank needs. Obviously, since you pull out the banks to your computer, they only download them when they cannot find them on your computer, so you do not slow down your assembly every time (only the first time).

+1
source

When compiling your project, Maven will download the corresponding .jar file from the repository, usually the central repository (you can configure different repositories, both for mirroring and for your own libraries that are not available in the central repositories).

If your IDE knows about Maven, it will analyze pom and either load the dependencies themselves or ask Maven to do this. Then it will open the dependency banks, and you will get autocomplete: the IDE "imports" the banks behind the scenes.

The repository contains not only the .jar file for the dependency, but also the ".pom" file that describes its dependencies. Thus, maven will recursively load its dependencies, and you will get all the banks necessary to compile your software.

Then, when you try to run your software, you will need to tell the JVM where to find these dependencies (i.e. you have to put them in the class path).

What I usually do is copy the dependencies to the target/lib/ directory, so it’s easy to deploy the software and run it. To do this, you can use the maven-dependency-plugin , which you specify in the <build> :

 <build> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </build> 
+1
source

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


All Articles