java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > maven 导入jar

maven 导入resource lib文件夹中的jar的几种方法

作者:月轩居士

本文主要介绍了maven 导入resource lib文件夹中的jar的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在Maven项目中,如果需要导入位于 src/main/resources/lib 或其他目录中的JAR文件,通常不推荐直接将JAR文件放在资源目录中。Maven有更规范的方式来管理依赖项,通常是通过在 pom.xml 文件中声明依赖项来实现的。然而,如果你确实需要使用本地的JAR文件。

方法一:将JAR文件安装到本地Maven仓库

1.将JAR文件安装到本地Maven仓库使用 mvn install:install-file 命令将JAR文件安装到本地Maven仓库。

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

如:

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

2.在 pom.xml 中声明依赖

在 pom.xml 文件中添加相应的依项。

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

方法二:使用 system 范围的依赖

虽然不推荐,但你可以使用 system 范围的依赖来引用本地的JAR文件。这种方法有一些缺点,比如不能很好地处理依赖传递和版本控制。

在 pom.xml 中声明系统范围的依赖

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

<systemPath> 是相对于项目的根目录(即包含 pom.xml 的目录)的路径。

方法三:使用 maven-install-plugin 插件

你可以在 pom.xml 中配置 maven-install-plugin 插件,在构建过程中自动安装JAR文件到本地Maven仓库。

1.在 pom.xml 中配置插件

 添加一个时:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</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>${project.basedir}/src/main/resources/lib/mysql-connector-java-8.0.27</file>
                        <groupId>com.mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.27</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 多个时

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
<!-- 第一个 JAR 文件 -->
                <execution>
                    <id>install-external-jar</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${project.basedir}/src/main/resources/lib/mysql-connector-java-8.0.27</file>
                        <groupId>com.mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.27</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
<!-- 第二个 JAR 文件 -->
  <execution>
                    <id>install-second-jar</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${project.basedir}/src/main/resources/lib/hibernate-core-5.6.7.Final</file>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>8.0.27</version>
                        <packaging>jar</packaging>
                    </configuration>
             </execution>
 <!-- 可以继续添加更多的执行目标来安装更多的 JAR 文件 -->
            </executions>
        </plugin>
    </plugins>
</build>

 2.在 pom.xml 中声明依赖

单个:

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

 多个:

	  <dependency>
		  <groupId>com.mysql</groupId>
		  <artifactId>mysql-connector-j</artifactId>
		  <version>8.0.27</version>
	  </dependency>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-core</artifactId>
		<version>5.6.7.Final</version>
     </dependency>

运行 Maven 构建时,Maven 会在 validate 阶段自动执行这些配置,将指定的 JAR 文件安装到本地仓库,并且项目会包含这些依赖项。 

推荐的方法:将JAR文件安装到本地Maven仓库,并在 pom.xml 中声明依赖。
不推荐的方法:使用 system 范围的依赖或在构建过程中自动安装JAR文件。

到此这篇关于maven 导入resource lib文件夹中的jar的几种方法的文章就介绍到这了,更多相关maven 导入jar内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文