在使用maven进行jar包管理时,通过我们都是通过maven去下载一些jar包,但有些jar在maven上没有,所以就就可能在本地直接手动加入一些需要用到的外部jar包。但如果我们用maven package打包就会发现,本地的那些jar是不能被maven识别的,所以就需要解决Maven关于本地jar包的打包处理的问题。
1. 使用system scope
org.sky my-jar 1.0 system e:\my-jar.jar
编译后出现一个警告:
[WARNING] 'dependencies.dependency.systemPath' for org.sky:my-jar:jar should use a variable instead of a hard-coded path e:\my-jar.jar @ line 35, column 16
[WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
原来是 maven 苦口婆心地告诉我们不要使用诸如 C:\Windows 这样的绝对路径,而应使用 ${java.home} 这样的相对路径(变量),否则降低了编译可重现性,并威胁我们以后可能不再支持这种方式。
后来改成<systemPath>${project.basedir}/lib/my-jar.jar</systemPath>, 结果说找不到z这个jar,无解了。
system scope引入的包,在使用jar-with-dependencies打包时将不会被包含,可以使用resources将本地包打进jar-with-dependencies
org.apache.maven.plugins maven-shade-plugin make-assembly package shade jar-with-dependencies xxx-jar-with-dependencies lib/ lib/ **/my-jar.jar
生成的xxx-jar-with-dependencies.jar中,将会包含lib目录以及my-jar.jar,并且能够被在执行的时候被找到。我反正没试过。还有看到网上说添加下面一段。
maven-compiler-plugin 2.3.2
2.使用指令 mvn install:install-file 将jar文件安装到本地仓库
语法规范:
mvn install:install-file -Dfile=-DgroupId= -DartifactId= -Dversion= -Dpackaging= -DgeneratePom=true Where: the path to the file to load the group that the file should be registered under the artifact name for the file the version of the file the packaging of the file e.g. jar
mvn install:install-file -Dfile=e:\ojdbc6.jar -DgroupId=ojdbc -DartifactId=ojdbc -Dversion=6 -Dpackaging=jar -DgeneratePom=true
3. 添加 in project repository,在新机器上执行时就不用运行mvn install:install-file命令了
in-project In Project Repo file://${project.basedir}/lib
org.richard my-jar 1.0
你的jar包及路径必须严格遵循格式:
/groupId/artifactId/version/artifactId-verion.jar
本例中: lib/org/richard/my-jar/1.0/my-jar-1.0.jar
参考地址:http://stackoverflow.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-r