How to add dependencies to a locally infrequently used jar package:
First we need to understand the two concepts of the MAVEN repository: local warehouses and remote warehouses
A local repository is a buffer and a subset of remote repositories, and when you build a MAVEN project, you first look for resources from the local repository, and if not, MAVEN downloads from the remote repository to your local repository. This will not need to be downloaded from a remote location the next time you use it.
For example, if we put some of the jar packages we need to rely on in the project's Lib folder, we need to add the dependencies to the Pom.xml file. However, most jar packages are informal jar packages that are difficult or impossible to download from a remote repository.
There are several more common practices on the Web:
Installed directly in the local repository, however, this practice only works on your local machine, and the other machines still need to execute the installation commands repeatedly.
This practice sometimes creates problems by adding system scope tags, such as when a project is being packaged successfully while the project is in progress, but the package dependency error is reported during the run.
Build a local remote repository. First, the dependent jar package is passed the following command: GroupID This type is optional, but you need to ensure that it does not exist in the remote repository and is best to be unique, otherwise it will conflict with other dependencies.
$mvn install:install-file -Dfile=lib/gson-2.2.4.jar -DgroupId=localgroupid -DartifactId=gson -Dversion=2.2.4 -Dpackaging=jar -DlocalRepositoryPath=repo -DcreateChecksum=true
After executing the command, we look at the tree structure of the Repo folder, where the repo folder actually corresponds to the remote repository:
$tree -l.`-- localgroupid `-- gson |-- 2.2.4 | |-- gson-2.2.4.jar | |-- gson-2.2.4.jar.md5 | |-- gson-2.2.4.jar.sha1 | |-- gson-2.2.4.pom | |-- gson-2.2.4.pom.md5 | `-- gson-2.2.4.pom.sha1 |-- maven-metadata-local.xml |-- maven-metadata-local.xml.md5 `-- maven-metadata-local.xml.sha1
At this point, we need to add the following dependencies in Pom.xml:
<dependency> <groupId>localgroupid</groupId> <artifactId>gson</artifactId> <version>2.2.4</version></dependency>
In addition, you need to add the following in Pom.xml:
<repositories> <repository> <id>project</id> <url>file://${project.basedir}/repo</url> </repository></repositories>
We can compile, test, and package the project when we inject dependencies such as the jar packages we need to rely on to Pom.xml.
the process of downloading dependencies is as follows: depending on the dependencies in Pom.xml, first go to the remote repository to see if there is a dependency package, if there is a direct download. Otherwise, the dependency is checked from repo, and if there is a direct add to the local warehouse, an error is added. So when we add repo to the project, we can install it to the local repository on other machines without having to repeat the manual or Run command.