赞
踩
本文提供三种将一个自定义的JAR文件添加到你的Maven项目中的方法。
涉及到的命令
mvn install:install-file -Dfile=<path-to-file>
这里没指定JAR 文件的 groupId, artifactId, version 和packaging信息。
因为Maven-install-plugin 2.5版本以后这些信息可以从指定的pom文件中提取。
当然也可以指定这些信息:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>
如:
mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0
在pom.xml中添加如下依赖,就可以在maven项目中使用了
- <dependency>
- <groupId>com.roufid.tutorials</groupId>
- <artifactId>example-app</artifactId>
- <version>1.0</version>
- </dependency>
这种方案的代价非常大。
因为你如果修改了本地maven仓库的地址,还得重新安装这个jar文件。
如果有多个人一起开发,每个人都得这么搞一次。
项目的可移植性也是一个需要重点考虑的问题。
另外一种方案是,在pom.xml文件中使用 maven-install-plugin插件,在初始化阶段安装jar包。
要想这么搞,你先得在pom文件中定义jar的位置。
最佳的实践是将jar包和pom.xml文件放在同一级目录(项目根目录)。
假设你放在了<PROJECT_ROOT_FOLDER>/lib/app.jar这里。
则pom.xml文件插件则可以这么写
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5</version>
- <executions>
- <execution>
- <phase>initialize</phase>
- <goals>
- <goal>install-file</goal>
- </goals>
- <configuration>
- <groupId>com.roufid.tutorials</groupId>
- <artifactId>example-app</artifactId>
- <version>1.0</version>
- <packaging>jar</packaging>
- <file>${basedir}/lib/app.jar</file>
- </configuration>
- </execution>
- </executions>
- </plugin>
${basedir} 表示包含 pom.xml的目录
另外一种方案是直接采用system 的范围,指定本地jar包的绝对路径。
如果jar报放在 <PROJECT_ROOT_FOLDER>/lib这里
- <dependency>
- <groupId>com.roufid.tutorials</groupId>
- <artifactId>example-app</artifactId>
- <version>1.0</version>
- <scope>system</scope>
- <systemPath>${basedir}/lib/yourJar.jar</systemPath>
- </dependency>
${basedir} 表示包含 pom.xml的目录
第三个方案和第一个很像,不同点在于JAR包安装到另外一个本地maven仓库中。
假设本地仓库名称为:maven-repository。位于${basedir}目录.
先将本地JAR包发布到新的本地仓库中
vn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar
然后在pom.xml文件中添加仓库
- <repositories>
- <repository>
- <id>maven-repository</id>
- <url>file:///${project.basedir}/maven-repository</url>
- </repository>
- </repositories>
然后就可以在pom.xml文件中添加依赖了
-
- <dependency>
- <groupId>com.roufid.tutorials</groupId>
- <artifactId>example-app</artifactId>
- <version>1.0</version>
- </dependency>
最好的方法是使用包含你自定义JAR包的Nexus仓库管理器,可将其用下载依赖的远程仓库。
具体用法这里就不详细展开了,自行查询。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。