preface

Recently, I was chatting with my friend. He said that he had a problem. He used a private API package from a third-party company.

The location of the third JAR of his project is similar to the followingMake the following reference in poM

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0 the SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
Copy the code

Pom package plug-in uses springBoot plug-in


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Copy the code

The default springboot package does not insert the SystemScope JAR into the boot-INF /lib/ directory of the Springboot project.

Note: The springboot project references the boot-INF /lib/ jar by default

So I told my friend, don’t use SystemScope, directly build maven private bin, and then upload third-party JAR to private bin, POM quoted below

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0 the SNAPSHOT</version>
        </dependency>
Copy the code

My friend replied that the company did not have a private warehouse, and I was confused. I asked him that he should not, and confirmed again, after getting the same reply from him. The following provides the following several schemes for his reference

How does SpringBoot reference a third JAR that is not published to a private vault

The whole idea: Because SpringBoot provides a package plug-in, by default the JAR located in boot-INF /lib/ is compiled into a class file for the project to reference. So we just need to make sure that boot-INF /lib/ contains the third-party JAR we want to reference

Solution 1: POM specify jar range for System + Springboot plug-in add includeSystemScope tag attribute is true

Example:

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0 the SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
Copy the code
 <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Copy the code

Scheme 2: POM specifies the jar scope to include for the System + Resources tag

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0 the SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
Copy the code
 <build>
        <resources>
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar    
       
        
        
         org.springframework.boot
         
        
         spring-boot-maven-plugin
         
         
          
           
           
            repackage
            
           
          
         
        
       Copy the code

Plan 3: Directly plug third-party jars into the local repository to be published

Maven will first search for jars from the local repository, and then search for jars from the private repository (if any). The private repository does not search for jars from the central repository, and then store the found jars to the local repository.

Therefore, we execute the following command to import the third-party JAR directly into the local repository

mvn install:install-file -DgroupId=org.example -DartifactId=demo-api -Dversion=1.0-SNAPSHOT -Dfile=F:\boot-thirdparty\lib\demo-api.jar -Dpackaging=jar
Copy the code

The project’s POM simply imports the third-party JAR as follows

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0 the SNAPSHOT</version>
        </dependency>
Copy the code

Solution 4: Set up maven private bin and upload third-party JAR to Maven private bin

Note: Setting up a private warehouse is beyond the scope of this article. How to upload a third-party JAR to a private warehouse

A. Configure the servers TAB in Maven settings. XML as follows

<server>
		<id>nexus</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
Copy the code

B. Run the following command

mvn deploy:deploy-file -DgroupId=org.example -DartifactId=demo-api -Dversion=1.0-snapshot-dpackaging = jar-dfile =F:\boot-thirdparty\lib\demo-api. jar-durl = your private address -DrepositoryId= The same as settings. XML configuration server tag ID as nexusCopy the code

Or you can upload using the visual interface that comes with Maven’s repository

conclusion

Personally, I prefer option 3 and option 4, because it is already using Maven to manage jars, and it is a bit twisted to introduce additional JARS in the project and then modify the plug-in