maven引入自己的jar包
时间: 2023-10-09 22:12:26 浏览: 57
要将自己的jar包引入maven项目中,可以使用以下几种方法:
1. 将jar包手动添加到本地maven仓库
首先在命令行中进入到jar包所在目录,然后使用以下命令将jar包添加到本地maven仓库中:
```
mvn install:install-file -DgroupId=<group-id> -DartifactId=<artifact-id> \
-Dversion=<version> -Dfile=<path-to-file> -Dpackaging=<packaging>
```
其中,<group-id>、<artifact-id>、<version>、<path-to-file>、<packaging> 分别代表自定义的groupId、artifactId、version、jar包的路径和打包方式。执行完这个命令后,jar包就被添加到本地maven仓库中了。
2. 使用maven插件将jar包打包到项目中
在pom.xml文件中添加以下配置:
```
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/lib</outputDirectory>
<resources>
<resource>
<directory>${basedir}/lib</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
这段代码配置了两个maven插件:maven-resources-plugin和maven-dependency-plugin。这两个插件可以将jar包打包到项目中。其中,maven-resources-plugin将jar包从lib目录复制到target/lib目录中,而maven-dependency-plugin将所有依赖项(包括jar包)复制到target/lib目录中。最终,target/lib目录中会包含所有的依赖项,可以在项目中直接使用。
3. 使用maven的dependency标签引入jar包
在pom.xml文件中添加以下配置:
```
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-artifact.jar</systemPath>
</dependency>
</dependencies>
```
这段代码使用了maven的dependency标签来引入jar包。其中,<groupId>、<artifactId>、<version> 分别代表jar包的groupId、artifactId、version。<scope>指定了依赖范围为system,表示这个依赖项由系统提供。而<systemPath>指定了jar包的路径。这种方式比较麻烦,不推荐使用。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)