SoFunction
Updated on 2025-05-22

maven  Several ways to import jars in resource lib folder

In Maven project, if needed, import is located atsrc/main/resources/libOr JAR files in other directories, it is usually not recommended to place JAR files directly in the resource directory. Maven has a more standardized way to manage dependencies, usually byDependencies are declared in the file to implement it. However, if you do need to use a local JAR file.

Method 1: Install the JAR file to the local Maven repository

1.Install JAR files to local Maven repositoryUse the mvn install:install-file command to install the JAR file to the local Maven repository.

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

like:

mvn install:install-file -Dfile=E:\DownloadWorkSpaces\mysql-connector-java-8.0. -DgroupId= -DartifactId=mysql-connector-j -Dversion=8.0.27 -Dpackaging=jar

2. InDependency in

existAdd corresponding item to the file.

	  <dependency>
		  <groupId></groupId>
		  <artifactId>mysql-connector-j</artifactId>
		  <version>8.0.27</version>
	  </dependency>

Method 2: Use system scope dependencies

Although not recommended, you can usesystemScope dependencies to reference local JAR files. This approach has some disadvantages, such as not being able to handle dependency delivery and version control well.

existDeclare system-wide dependencies

<dependency>
    <groupId></groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.27</version>
    <scope>system</scope>
    <systemPath>${}/src/main/resources/lib/mysql-connector-java-8.0.</systemPath>
</dependency>

<systemPath>is relative to the root directory of the project (that is, contains) path to directory.

Method 3: Use the maven-install-plugin plugin

You can configure the maven-install-plugin plugin in to automatically install JAR files to the local Maven repository during the build process.

1. Configure the plug-in in

When adding one:

<build>
    <plugins>
        <plugin>
            <groupId></groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-external-jar</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${}/src/main/resources/lib/mysql-connector-java-8.0.27</file>
                        <groupId></groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.27</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Multiple times

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-install-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.5.2&lt;/version&gt;
            &lt;executions&gt;
&lt;!-- The first JAR document --&gt;
                &lt;execution&gt;
                    &lt;id&gt;install-external-jar&lt;/id&gt;
                    &lt;phase&gt;validate&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;install-file&lt;/goal&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;file&gt;${}/src/main/resources/lib/mysql-connector-java-8.0.27&lt;/file&gt;
                        &lt;groupId&gt;&lt;/groupId&gt;
                        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                        &lt;version&gt;8.0.27&lt;/version&gt;
                        &lt;packaging&gt;jar&lt;/packaging&gt;
                    &lt;/configuration&gt;
                &lt;/execution&gt;
&lt;!-- The second one JAR document --&gt;
  &lt;execution&gt;
                    &lt;id&gt;install-second-jar&lt;/id&gt;
                    &lt;phase&gt;validate&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;install-file&lt;/goal&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;file&gt;${}/src/main/resources/lib/hibernate-core-5.6.&lt;/file&gt;
                        &lt;groupId&gt;&lt;/groupId&gt;
                        &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
                        &lt;version&gt;8.0.27&lt;/version&gt;
                        &lt;packaging&gt;jar&lt;/packaging&gt;
                    &lt;/configuration&gt;
             &lt;/execution&gt;
 &lt;!-- You can continue to add more execution targets to install more JAR document --&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
  • Plugin configuration:

  • The <plugin> element defines the maven-install-plugin plugin.
  • The <executions> element contains multiple <execution> sub-elements, each child element represents the installation process of a JAR file.
  • Each execution target

  • <id>: Provides a unique identifier for each execution target.
  • <phase>: Specifies the build lifecycle phase to which the execution target is bound. Select the validate stage here to ensure that the JAR file is installed before compilation.
  • <goals>: Specifies the target to be executed, here is install-file.
  • <configuration>: Provides detailed information required to install JAR files, including file path, groupId, artifactId, and version.

 2.existDependency in

single:

	  <dependency>
		  <groupId></groupId>
		  <artifactId>mysql-connector-j</artifactId>
		  <version>8.0.27</version>
	  </dependency>

Multiple:

	  <dependency>
		  <groupId></groupId>
		  <artifactId>mysql-connector-j</artifactId>
		  <version>8.0.27</version>
	  </dependency>
	<dependency>
		<groupId></groupId>
		<artifactId>hibernate-core</artifactId>
		<version>5.6.</version>
     </dependency>

When running the Maven build, Maven will be invalidateThe configuration is automatically performed in the phase, installing the specified JAR file to the local repository, and the project will contain these dependencies.

Recommended method: Install the JAR file to the local Maven repository and declare the dependencies in .
Not recommended method: Use system-wide dependencies or automatically install JAR files during build.

This is the end of this article about several methods of importing jars in the resource lib folder of maven. For more related content to import jars, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!