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文件。这种方法有一些缺点,比如不能很好地处理依赖传递和版本控制。

  1. 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>
  1. 插件配置:

  • <plugin>元素定义了maven-install-plugin插件。
  • <executions>元素包含了多个<execution>子元素,每个子元素代表一个JAR文件的安装过程。
  1. 每个执行目标

  •  <id>:为每个执行目标提供一个唯一的标识符。
  • <phase>:指定了该执行目标绑定到的构建生命周期阶段。这里选择validate阶段,确保在编译之前安装 JAR 文件。
  • <goals>:指定了要执行的目标,这里是install-file。
  • <configuration>:提供了安装 JAR 文件所需的详细信息,包括文件路径、groupId、artifactId 和版本等。

 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项目中的libresource单独打,你可以使用Maven的插件来完成这个任务。以下是一种常见的做法: 1. 使用Maven的`maven-jar-plugin`插件配置来打lib: 在项目的`pom.xml`文件中,添加以下插件配置: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.MainClass</mainClass> <!-- 替换为你的主类 --> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` 这样配置后,当你运行`mvn package`命令时,Maven会将项目的依赖库打到一个独立的lib目录中。 2. 使用Maven的`maven-resources-plugin`插件配置来打resource: 在项目的`pom.xml`文件中,添加以下插件配置: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/resources</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这样配置后,当你运行`mvn package`命令时,Maven会将项目的资源文件打到一个独立的resources目录中。 最后,运行`mvn package`命令即可将libresource分别打到独立的目录中。注意查看Maven的输出目录以获取生成的目标文件。 希望这能帮到你!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值