最近在项目中用到了一个jar包,在maven中没有关于这个jar包的引用。于是就用Eclipse的Build Path>Add to Build Path把jar包添加到了项目中,但是在打包项目后,运行项目却报找不到对应类的异常。经过排查发现Build Path>Add to Build Path添加的jar包在打包项目时未被maven识别。本文将具体讲解如何处理这类问题。
假设将包htmlparser.jar放入了项目下的lib目录中:${project}/lib/htmlparser.jar
则pom.xml文件中依赖可以如下:
<dependency> <groupId>com.htmlparser</groupId> <artifactId>htmlparser</artifactId> <version>2.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/htmlparser.jar</systemPath> </dependency>
但是这种做法的问题是在用maven打包时,该引用jar包的不会打到最终的war包中。那么怎么解决呢?请看下面的方案。
Maven 安装 JAR 包的命令是:
maven install 本地jar
命令格式
mvn install:install-file -DgroupId=<group_name> -DartifactId=<artifact_name> -Dversion=<version_no> -Dfile=<path_of_the_local_jar> -Dpackaging=jar -DgeneratePom=true
示例
mvn install:install-file -DgroupId=com.redfin -DartifactId=sitemapgenerator -Dversion=1.0.1 -Dfile=E:/work/codedq/lib/sitemapgen4j-1.0.1.jar -Dpackaging=jar -DgeneratePom=true
添加依赖
<dependency> <groupId>com.redfin</groupId> <artifactId>sitemapgenerator</artifactId> <version>1.0.1</version> </dependency>
使用命令行
mvn deploy:deploy-file -DgroupId=com.hjz -DartifactId=hujunzheng -Dversion=0.0.1 -Dpackaging=jar -Dfile=jar包绝对路径 -Durl=http:xxx/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
DgroupId和DartifactId构成了该jar包在pom.xml的坐标,项目就是依靠这两个属性定位。自己起名字也行。
Dfile表示需要上传的jar包的绝对路径。
Durl私服上仓库的位置,打开nexus——>repositories菜单,可以看到该路径。
DrepositoryId服务器的表示id,在nexus的configuration可以看到。
Dversion表示版本信息,怎样得到一个jar包准确的版本呢?
解压该包,会发现一个叫MANIFEST.MF的文件,这个文件就有描述该包的版本信息。
比如Manifest-Version: 1.0可以知道该包的版本了。
通过eclipse