📜 ⬆️ ⬇️

How to connect a library to a project using maven

Ask someone if Maven is needed at all - 90 percent of the programmers' livestock will answer that it is for this reason that they will be largely right.


If in the case of, for example, C ++, connecting the library to your project is a serious step that will surely complicate the assembly to such an extent that you have to include instructions for connecting this particular library in the readme, in the case of Java this is done easily and naturally - not Lastly thanks to Maven.


I want to note that the article is intended for those who began to study java relatively recently and although it already means from the previous article what maven is about - what libraries are not well known, and does not know how to connect them at all.


Let's start from afar.


How to add a new class to the project


In order to use in your code any class other than the standard one, you need to create this class, that is, make the corresponding file and write the corresponding code inside this file.


The newly written class should be located in the same place as all other classes, otherwise the compiler will not see it. Now, in order to use it, it is enough to add the corresponding import directive to the class using it.


I suggest those present to spend a minute or two on expressing gratitude to Captain Obvious, and then continue reading.


How to add a class to a project written by someone else


The answer, generally speaking, is obvious - you need to copy this class into your code.


In addition to being a little less obvious, you need to copy into your project all the classes that are used by the class for which everything was started.


Classes used by some other class, by the way, are called dependencies (dependencies) of this class.


This approach has an obvious disadvantage. For each class, you need to look for dependencies, and then copy them to yourself with your hands. If there is any bug later in this whole heap of code, then after the fix appears, you will have to repeat the process again.


How to add a class written by someone to the project without copying it into your code


In all more or less modern programming languages, this problem has already been solved.


You can always tell the compiler that classes need to be taken not only from the project directory, but also from other directories specified by the programmer. In Java, this is done using the classpath parameter.


If you work with Java, it is strictly necessary to understand what a classpath is, but it is not necessary to specifically add a library to a project using maven. In the context of our theme, the classpath can be considered an entity in which all the places where the compiler will look for the classes needed by the application are listed. If any of these classes are not in any of these places, the program will not work.


Now, in order to use another class, it is enough to download the code with this class, and add to the classpath the directory into which this code is downloaded.


Compared with the previous method, this is a noticeable progress. Now there is no need to manually track all dependencies of the only class that is needed for the operation of your application. To add bug fixes and all this, simply download the new version of the code.


That's actually we came to the answer to the question, what is a library.


What is a library


The library is called the same code, the root directory of which must be added to the classpath. That is, the totality of the class we need and its dependencies, as well as classes that are not dependencies of the class we need, but are contained in the code.


In the case of Java, the code that we add to the classpath can be compiled and the directory with the compiled code can be added to the classpath.


You can also archive the compiled code in zip format, change the file extension with the archive to jar and add this file to the classpath file. In the context of Java development, the jar file is also called a library.


jar files do not solve any of the above problems, but their use greatly facilitates the transfer of libraries between programmers.


But, there is one significant problem that jar files do not help either.


The classes that the library uses may not be contained in it, but in some other library, which, by analogy with the previous case, is also called dependency.


How to add a library to a project using another library


In order to add a library to the project with all its dependencies, you have to download these dependencies and add one to the project one by one. When updating each library, dependencies will have to be downloaded again.


It may happen that when updating any dependencies, the library from which we are going to use the class in our project will stop working correctly - we will have to carefully monitor that this does not happen and be responsible for each update, and better, update the libraries. only if the update can not do at all.


How to make sure the dependency update doesn't break anything


Storogo speaking - no way.


It is necessary, of course, to update the dependency and run unit tests. If they do not pass, then you can definitely say that something is broken.


If not, this is weak evidence that there is no failure, but it does not prove anything.


But suppose that unit tests can be trusted. In this case, when adding a library, you will have to manually check its compatibility with dependencies by running the attached unit tests and do this with each update. Believe me, this is hard.


In order to avoid this tedious and difficult work, there is an unspoken agreement between programmers from around the world. Libraries are made to version.


When creating a library, the programmer writes its code, makes sure that it works correctly, and then assigns an identifier, for example a number, to this code, and then makes the code available to other programmers so that they can use this library in their code.


If other programmers find an error in the library, the creator of the library does not just silently fix it, but after the correction changes the library identifier and now the world has two libraries available with the same name but different identifiers.


This identifier is called the version of the library.


The programmer updates the version of the library not only when correcting errors, but in general with any change.


Programmers who use this third-party library to create their own libraries, or simply programs, indicate in the documentation not only the name, but also the version with which their library is guaranteed to work correctly.


That is, the job of verifying which versions of dependencies to use is taken by one person, the rest simply enjoy the fruits of his labors.


This greatly simplifies the situation, but does not eliminate the need to look through all the dependencies of your code and dependencies in order to get a complete list of libraries that need to be connected to the project.


When updating any of the libraries, the process must be repeated. This, believe me, is painful.


How to get a list of all libraries needed by the project and add them to the project automatically


And here comes Maven. It formalizes the tacit agreement between programmers and makes it a vowel.


If for each library there is a list of dependencies with clearly defined versions, then it’s not difficult to compile a list of all libraries needed by the project.


Copying these libraries to the programmer's machine and adding them to the classpath is also not the highest mathematics. You only need to know where to download the libraries and descriptions of their dependencies, as well as in what format these dependencies are described.


These issues in Maven have been resolved successfully.


How to connect the library to the Maven project


It's all in general trite. The library in understanding of Maven, is the artifact necessary for assembly of the program.


Each artifact, as we remember, has a groupId, artifactId and version. You only need to specify maven that this artifact is a project dependency.


The dependency list must be wrapped with the dependencies tag. Each individual dependency must be wrapped with the dependency tag. Inside the dependency tag in the groupId, artifactId and version tags you need to specify the values ​​of the corresponding parameters.


There probably need to give an example of pom.xml with the added library. Here is an example:


<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>com</groupId> <artifactId>hello-world</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> </dependencies> </project> 

Here we connected the library to the project, with a bunch of static methods that perform standard tasks. One of these tasks is to expand the array and add an additional element to it.


Here, for example:


 package com; import static org.apache.commons.lang3.ArrayUtils.*; public class Application { public static void main(String[] argv) { String[] phrase = {"Hello"}; phrase = add(phrase, " "); phrase = add(phrase, "world"); for (String word : phrase) { System.out.print(word); } System.out.println(); } } 

To compile the code you need to write


 mvn compile 

To start it


 mvn exec:java -Dexec.mainClass="com.Application" 

This code will output Hello world, but, in difference from the previous article, by a new progressive method. With the help of the library.


That's all.


The dependencies of the artifact connected to the project will be found by maven himself, he will compile a list of all the libraries needed by the project, including dependencies dependencies and dependencies dependencies dependencies, download them himself to the machine on which the build takes place and add them to the classpath.


This, believe me, is wonderful.


Total


  1. Classes used by another class are called dependencies of this class.
  2. The library is called the set of classes designed to solve a particular task, and their dependencies.
  3. The dependencies of classes from the library can be in other libraries and then these libraries are called its dependencies.
  4. In Java, the compiled classes from the library can be packaged in a zip archive with the jar extension. jar files are also called a library.
  5. The libraries used by the project are called project dependencies. To connect dependencies in maven, the dependency tag is used. The dependency tags must be inside the dependencies tag.
  6. In Maven, the library is an artifact, which means it has a groupId, artifactId, and version.
  7. To connect the library to the project using maven, it is enough to specify its groupId, artifactId and version.

')

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


All Articles