maven resolves third-party jar dependencies

In a maven project, for those third-party jars that do not exist in the maven repository, the dependency resolution usually has the following solutions:

Method 1: Copy the jar package directly to the specified directory of the project, and then specify the dependency type as system in the pom file, such as:

<dependencies>
2     <dependency>
3         <groupId>xxx</groupId>
4         <artifactId>xxx</artifactId>
5         <version>6.0</version>
6         <scope>system</scope>
7         <systemPath>${project.basedir}/libs/xxx-1.0.jar</systemPath>
8     </dependency>
9 </dependencies>
copy code

Please note: scope is a system dependency and will not be automatically packaged into the final jar package during packaging. The resources that need to be packaged together must be clearly specified in the resources node:

copy code
1 <resources>
2     <resource>
3         <targetPath>lib/</targetPath>
4         <directory>lib/</directory>
5         <includes>
6             <include>**/my-jar.jar</include>
<include>**/my-jar.jar</include>
 7 </includes> 8 </resource> 9 </resources>
copy code

This method is fine for resolving single project dependencies.

However, if there are multiple modules in the project, and multiple modules need to rely on the specified third-party jar package, it is inappropriate to perform such a configuration in different modules, and the jar package needs to be copied back and forth multiple times.

 

Method 2: Create a new maven module project and use this project exclusively to solve the problem of relying on third-party jar packages (premise: you need to install the dependent third-party jar packages to the local warehouse)

For example:
(1) Create a new xxx-3rd module, which is used to configure the third-party jar package to be relied on. The way to configure dependencies is the same as that of dealing with single project dependencies. Refer to method 1.
(2) Introduce dependencies on xxx-3rd modules in other modules that need to rely on third-party jar packages, so that according to the characteristics of maven transitive dependencies, the problem of simultaneously relying on third-party jar packages in multiple modules can be well solved .

copy code
1 <dependencies>
2     <dependency>
3         <groupId>xxx</groupId>
4         <artifactId>xxx-3rd</artifactId>
5         <version>1.0.0</version>
6     </dependency>
7 </dependencies>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443349&siteId=291194637