Maven project packages jar to other projects pom reference and external reference
In the actual development process, a lot of code needs to be reused, but the amount of code is a lot. Such code can be put forward as public code or called tools. Usually such tools will be introduced by other project pom in the form of jar package Or introduced and used externally.
The first step is to create a maven project and complete the tool class packaging jar
The tool idea I use here is the same for creating maven projects in other ways.
Here we choose maven-archetype-quickstart. A Maven project does not require webapp.
After configuring your own maven, you can complete the creation. The first step is to simply create a maven project.
Then create a simple tool class CommonUtil
The second step is to package the tool jar
First, we use the method introduced by pom to reference the tool class in another project. Here we need to install the tool jar into the local warehouse.
To use the tool in another project, we have to add the tool to our local warehouse or private server first.
Method 1: The idea tool operates the
mvn install command, which is the maven installation command. After packaging the project, it will add the jar package in the target to the local warehouse you configured by default. (It is useless to add it manually, maven can't recognize it)
Check whether the local warehouse has been added:
This means that your tool class has been put into your local warehouse as a jar package.
Method 2: Add jar to the local warehouse without development tools, use the maven goal command.
Under the premise of configuring maven, open cmd and execute
install:install-file -Dfile=<Jar包的地址>
-DgroupId=<Jar包的GroupId>
-DartifactId=<Jar包的引用名称>
-Dversion=<Jar包的版本>
This can also complete the installation of the jar to the local warehouse.
If it is imported externally, only one jar package is needed. We can directly mvn package to print the jar package. The default path is in the target directory of your project.
Step 3: Import jar from other projects
Introduce tool jar by pom for other projects
Feel free to create another maven project (not going into details), and add a dependency to its pom file:
so that the tool class can be used in the project after the project is successfully imported: the
call is successful!
Import jar to use externally
Create a lib directory under the same level of src in the project, place the jar package just packaged, and
configure the external import pom
<dependency>
<groupId>com.testUtil</groupId> <!--自定义-->
<artifactId>testUtil</artifactId> <!--自定义-->
<scope>system</scope>
<systemPath>${basedir}/lib/testUtil-1.0-SNAPSHOT.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
This has been added, if not added, manually introduce it: the
external introduction is completed.