Use maven to introduce third-party jar packages and package them

We know that Maven manages dependencies through warehouses. When a Maven project needs a certain dependency, as long as the coordinate information of the dependency is declared in its POM, Maven will automatically download the component from the warehouse for use. But in the actual development process, we often encounter a situation: docking third-party manufacturers, they give a jar package, and then the project needs to use this jar package, but this jar package has not been uploaded to the remote warehouse, at this time Simple pom dependency references cannot be used, so there are two problems:

  1. How to reference the jar to the project, and make the project call the method of the jar package normally
  2. How to package the externally referenced jar into the project?

Of course, if the company has a private server, it can directly upload the jar package to the private server. Then directly rely on simple declarations through pom.

Therefore, this article focuses on explaining these two points

Table of contents

The following is a maven project, java-demo.jar is the jar package we want to import.

<!--外部依赖-->
<dependency>
  <groupId>com.gzl.cn</groupId>
  <artifactId>java-demo</artifactId>
  <!--依赖范围-->
  <scope>system</scope>
  <version>1.0-SNAPSHOT</version>
  <!--依赖所在位置-->
  <systemPath>${project.basedir}/src/main/resources/lib/java-demo.jar</systemPath>
</dependency>

In the above configuration, in addition to the coordinate information of the dependency, the external dependency also uses two elements of scope and systemPath.

  • scope indicates the scope of dependencies, and the value here must be system, that is, the system.
  • systemPath indicates the location of dependent local artifacts.

Packaging plug-in: <includeSystemScope>true</includeSystemScope>This is very important, it is to package the external dependencies. If there is no such configuration, the jar package will not contain the external jar package, which may cause abnormal operation!

<build>
      <!-- 打出来的jar包名 -->
    <finalName>maven-demo</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.4.2</version>
            <configuration>
                <!-- 主启动类全类名 -->
                <mainClass>com.gzl.cn.App</mainClass>
                <!-- 这个配置很重要,是将外部依赖打包进来的,如果没有该配置,打出来的jar包是不包含外部jar包的 -->
                <includeSystemScope>true</includeSystemScope>
            </configuration>
            <executions>
                <execution>
                  <!--自定义 id -->
                  <id>repackage</id>
                    <!--插件目标 -->
                  <goals>
                    <goal>repackage</goal>
                  </goals>
            </execution>
          </executions>
        </plugin>
    </plugins>
  </build>

But now there is a problem, the typed jar package can be used, but it is very large.

The following is the directory after decompressing the jar package:

We can unzip the jar and find that there is a jar referenced by a third party in the lib directory of BOOT-INF.

Then there is also this jar package in the classes/lib directory of BOOT-INF. This is the jar package in the source code, and the above is the jar package that the typed jar package depends on when it runs. Normally, we can remove the jar package in the source code, so that the size of the jar package can be reduced.

Add the following dependencies to exclude jar packaging

<build>
	<resources>
	  <resource>
	    <directory>src/main/resources</directory>
	    <filtering>true</filtering>
	    <excludes>
	        <exclude>**/lib/**</exclude>
	    </excludes>
	  </resource>
	</resources>
	...以下省略
</build>

After repackaging, the jar will be decompressed to observe that there is no lib directory in the source code.

java -jar can still be run

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/130611728