本地jar包如何添加到 Maven项目 中?有好多种方法、其中就有直接引用;上传到本地仓库等等方法,这里几种常用的方法

本地Jar包可以直接添依赖项
假设 JAR 位于<PROJECT_ROOT_FOLDER>/lib中。
如此,将依赖项添加到您的 pom.xml 文件中,如下所示:
1 2 3 4 5 6 7 |
<dependency> <groupId>com.sample</groupId> <artifactId>sample</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/yourJar.jar</systemPath> </dependency> |
在新版本中,此功能已被标记为已弃用但仍可以使用1(只是在 maven 启动期间在日志中看到警告)。参照https://issues.apache.org/jira/browse/MNG-6523
上传本地Jar包到本地Maven仓库
使用这个命令
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mvn install:install-file \ -Dfile=<path-to-file> \ -DgroupId=<group-id> \ -DartifactId=<artifact-id> \ -Dversion=<version> \ -Dpackaging=<packaging> \ -DgeneratePom=true <path-to-file>: 要加载的文件的路径,例如 →c:\kaptcha-2.3.jar <group-id>: 文件应注册的组,例如 →com.google.code <artifact-id>:文件的工件名称,例如 →kaptcha <version>: 文件的版本 例如 →2.3 <packaging>: 文件的打包方式 eg →jar |
例如
1 |
mvn install:install-file -DgroupId=net.zuze -DartifactId=costom-annotation -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=./custom-annotation-1.0-SNAPSHOT.jar |
现在,通过将这些行添加到您的 pom.xml 文件来将依赖项添加到您的 Maven 项目:
1 2 3 4 5 6 7 |
<dependency> <groupId>com.roufid.tutorials</groupId> <artifactId>example-app</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/yourJar.jar</systemPath> </dependency> |
另一种方法上传到本地Maven仓库
1 |
mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> |
部署 JAR 后,您需要在 pom.xml 文件中添加存储库:
1 2 3 4 5 6 |
<repositories> <span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <存储库></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <id>maven 仓库</id></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <url>文件:///${project.basedir}/maven-repository</url></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> </存储库></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> </存储库></span></span> |
在此之后将依赖项添加到您的 pom.xml
1 2 3 4 5 |
<span style="vertical-align: inherit;"><span style="vertical-align: inherit;"><依赖关系></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <groupId>com.roufid.tutorials</groupId></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <artifactId>示例应用</artifactId></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> <版本>1.0</版本></span></span><span style="vertical-align: inherit;"><span style="vertical-align: inherit;"> </依赖></span></span> |
参考
参考http://www.zuze.net/posts/1630415971.html 中添加本地jar包的部分