Preface

I chatted with a friend recently, he said that he encountered a problem, he quoted the private API package of a third-party company, he started the local project without any problem, but the packaged operation can not find the API package, so I asked him how to cite this jar .

The storage location of the third jar of his project is similar to the following
image.png
Make the following quote in pom

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

The pom packaging plug-in uses the plug-in that comes with springboot


    <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>

Seeing this plug-in, you probably know the problem. The default packaging plug-in of springboot will not enter the systemscope jar into the BOOT-INF/lib/ of the springboot project.

Note: The springboot project will reference the jar in BOOT-INF/lib/ by default

So I told my friend, don’t use systemscope, just build a maven private warehouse, then upload the third-party jar to the private warehouse, and the pom is quoted as follows

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

My friend replied that the company does not have a private position. I looked dazed. I asked him if it would be the case. I confirmed it again and got the same reply from him. The following options are provided for him to refer to and choose

How does springboot reference the third jar that has not been released to the private warehouse

Overall thinking : Because of the packaged plug-ins provided by springboot, by default the jar located in BOOT-INF/lib/ will be compiled into class files and then used by the project. So we only need to make sure that BOOT-INF/lib/ contains the third-party jar we want to reference.

Solution 1: Pom specifies the jar scope as system+springboot plug-in and adds the attribute of the includeSystemScope tag to true

example :

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
 <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>
Option 2: Pom specifies the jar range as the system+resources label to introduce the jar to be included
   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/demo-api.jar</systemPath>
        </dependency>
 <build>
        <resources>
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
        <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>
Solution 3: directly enter the third-party jar into the local warehouse to be released

maven-repositories.png
The above picture is the official warehouse flow chart of maven. In fact, friends who are more familiar with maven should know that maven will first find the jar from the local warehouse. If the local warehouse cannot find the jar, it will start from the private warehouse (if there is a private warehouse) Looking inside, the private warehouse did not look for it from the central warehouse again, and then the jar found was stored in the local warehouse.

Therefore, we can execute the following command to directly enter the third-party jar into the local warehouse

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

The pom of the project can directly introduce third-party jars as follows

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
Solution 4: Build a private maven warehouse and upload third-party jars to the private maven warehouse

Note: build a private warehouse is beyond the scope of this article, let’s talk about how to upload a third-party jar to the private warehouse

a, first configure the following in the servers tag of maven's settings.xml

<server>
        <id>nexus</id>
        <username>admin</username>
        <password>admin123</password>
    </server>

b, execute the release command line, as follows

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=你的私仓地址 -DrepositoryId=和settings.xml配置server标签id一致,如上的nexus 

Or you can use the visual interface that comes with the maven private warehouse to upload

to sum up

For the above schemes, I personally prefer scheme 3 and scheme 4, because maven is used to manage the jar, and the jar must be introduced in the project, and then the plug-in is modified. It feels a bit twisted when you look at it.


linyb极客之路
347 声望193 粉丝