When packaging Maven project, if you need to add local jar package dependency, you can choose two methods:
1. Install to local warehouse
The first method is more conventional, which is applicable to the jar package to be added when it is exported by maven project and contains pom files. Just install the jar package into the local maven repository and add the dependency.
(1) Install to the local warehouse and execute the following command (the - Dfile/-DgroupId/-DartifactId/-Dversion item is filled in according to the pom file content):
mvn install:install-file -Dfile=xxxxx.jar -DgroupId=xxx.xxx.xxx -DartifactId=xxxxx -Dversion=1.0.0 -Dpackaging=jar
(2) After installation, you can find the corresponding jar package in the local repository. Then insert the corresponding dependency information into the pom file of the project:
<dependency> <groupId>xxx.xxx.xxx</groupId> <artifactId>xxxxx</artifactId> <version>1.0.0</version> </dependency>
2. Specify scope="system" and local jar package path in dependency
This method is applicable to jar packages exported by other methods. The jar package does not contain pom information, so it cannot be installed into the local warehouse. The method is: first configure the local jar package dependency, then export the jar package and configure the manifest when build ing.
(1) Configure the local jar package dependency (systemPath points to the local jar package path):
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.11.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/xxx.jar</systemPath> </dependency>
(2) In the spring boot Maven plugin of < build > set to export the local jar package to the final dependency Library of the project:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin>
(3) If the project is packaged with the Maven jar plugin plug-in, you need to add the corresponding jar package information in the manifest entries; otherwise, although the jar package is exported, the MANIFEST.MF file generated by the project does not have the corresponding dependency information, which will cause the runtime to find no corresponding class.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>XXXX</mainClass> </manifest> <manifestEntries> <Class-Path>./ lib/xxxxx.jar</Class-Path> </manifestEntries> </archive> <outputDirectory> ${project.build.directory}/XXXXX </outputDirectory> </configuration> </plugin>
(4) Finally, a complete < build > configuration of the project is attached (this configuration can separate the final generated jar package from the dependency library and configuration file).
<build> <finalName>XXXXX</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/XXXXX</targetPath> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <testSourceDirectory>src/test/java</testSourceDirectory> <testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}XXXXX/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>xxx.xxx.XXXXX</mainClass> </manifest> <manifestEntries> <Class-Path>./ lib/xxxxx.jar</Class-Path> </manifestEntries> </archive> <outputDirectory> ${project.build.directory}/XXXXX </outputDirectory> </configuration> </plugin> </plugins> </build>