As you develop your project, you may encounter situations where you need to package local dependencies into your Maven project. Local dependencies can be added if you are using IntelliJ IDEA or Eclipse, so there is no problem in the local development environment. But if you need to package and deploy, you can’t find the class.

0x00 Copy dependency

Create the libraries folder under the SRC /main/resources/ directory and copy the local dependencies into it.

0x01 Local Development

Reference local dependencies, here using MySQL driver files as an example.

< the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 8.0.26 < / version > <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/libraries/mysql-connector-java.jar</systemPath> </dependency>Copy the code

Once this is done, local development is available, but deployment to the server still causes classloading failures. So we need to configure it further.

0x02 Package configuration

If SpringBoot is used, set includeSystemScope to true.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>
Copy the code

If it is not a SpringBoot project, set extdirs to a dependent folder.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArguments>
            <extdirs>${project.basedir}/src/main/resources/libraries</extdirs>
        </compilerArguments>
    </configuration>
</plugin>
Copy the code