Maven project packages jar to other projects pom reference and external reference

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.
Insert picture description hereInsert picture description here
Here we choose maven-archetype-quickstart. A Maven project does not require webapp.
Insert picture description here
Insert picture description here
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
Insert picture description here

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)
Insert picture description here
Check whether the local warehouse has been added:
Insert picture description here
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包的版本> 

Insert picture description here
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.

Insert picture description here

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:
Insert picture description hereso that the tool class can be used in the project after the project is successfully imported: the
Insert picture description here
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
Insert picture description here
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>

Insert picture description here
Insert picture description here
This has been added, if not added, manually introduce it: the
Insert picture description here
external introduction is completed.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_31142237/article/details/89509780