其实用命令行的方式将jar包打到本地仓库,然后dependency也行,但是因为我需要将jar放到linux机器中,总不能每次换环境都要执行一次命令吧,而且jar包很多的时候呢?(你要会docker当我没说。),所以我用了下面的方法。
<dependency>
<groupId>com.fbcds</groupId>//随便写,一个唯一的标识,不能重复
<artifactId>bcpkix-jdk15on-1.57</artifactId>
//artifactId虽然也是随便写,但是后面会用到,所以需要记录下来。
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/bcpkix-jdk15on-1.57.jar</systemPath>
//systemPath第三方jar在本地的目录,${project.basedir}自动获取
//项目路径
</dependency>
这里只演示了jar中的一个,其他的省略但是注意
groupId和artifactId不能重复,artifactId和version后面会用到,所以artifactId最好用jar包的名字
本地idea中编译的时候会classnotfound
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib/bcprov-jdk15on-1.57.jar</extdirs>
<extdirs>lib/com.ibm.mq.allclient-9.0.4.0.jar</extdirs>
<extdirs>lib/javax.jms-api-2.0.1.jar</extdirs>
<extdirs>lib/javax.servlet-api-3.1.0.jar</extdirs>
<extdirs>lib/bcpkix-jdk15on-1.57.jar</extdirs>
</compilerArguments>
</configuration>
</plugin>
加入相应的extdirs即可 ,比如我这里的lib/bcprov-jdk15on-1.57.jar
问题:上一步本地编译没问题了,终端到生成的jar的目录,java -jar,还是报错classnotfound
比如我的错误:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/MQException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.ibm.mq.MQException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
原因:idea中是自带classpath的功能的,包括war包中也有这个功能,可是java -jar的时候classpath就没有了,而且只要一执行-jar的命令,命令行的-classpath就失效的了,只能去MANIFEST.MF中去找classpath,在这里我们是空的,只能通过手动写进去,或者通过maven配置写进去,但是前提是现在必须有jar包,否则即使写了classpath也是无济于事的
解决:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
${project.build.directory}是target目录,会在target目录下生成lib文件,并将jar包打入到,里面。
注意:jar包的名字是dependency中的artifactId-version格式的。
如图:
上一节中已经将jar包打入到了target目录中
这里需要MANIFEST.MF中写入classpath
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>${project.build.directory}/classes</classesDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>lib/bcpkix-jdk15on-1.57-1.0.jar lib/bcprov-jdk15on-1.57-1.0.jar lib/com.ibm.mq.allclient-9.0.4.0-1.0.jar lib/javax.jms-api-2.0.1-1.0.jar lib/javax.servlet-api-3.1.0-1.0.jar
</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
注意:classpath中间用空格隔开,这里的路径是相对路径,名字就是我们target目录下lib中相对应的jar包的名字。所以第一步中的artifactId和version还是很重要的。
当然Class-Path写成${basedir}/libs/xxx.jar也是可以的,这是如果在本地运行的话,但是如果像我这样将jar包放到linux服务其中是读不到basedir目录的。
到这里将target目录压缩成zip包,scp到linux机器中,解压缩,进入target目录,执行
java -jar xxx.jar
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloudwise</groupId>
<artifactId>ibmmq_consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.fbcds</groupId>
<artifactId>bcpkix-jdk15on-1.57</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/bcpkix-jdk15on-1.57.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aaa</groupId>
<artifactId>bcprov-jdk15on-1.57</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/bcprov-jdk15on-1.57.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sss</groupId>
<artifactId>com.ibm.mq.allclient-9.0.4.0</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/com.ibm.mq.allclient-9.0.4.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ddd</groupId>
<artifactId>javax.jms-api-2.0.1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/javax.jms-api-2.0.1.jar</systemPath>
</dependency>
<dependency>
<groupId>com.fff</groupId>
<artifactId>javax.servlet-api-3.1.0</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/javax.servlet-api-3.1.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>${project.build.directory}/classes</classesDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>lib/bcpkix-jdk15on-1.57-1.0.jar lib/bcprov-jdk15on-1.57-1.0.jar lib/com.ibm.mq.allclient-9.0.4.0-1.0.jar lib/javax.jms-api-2.0.1-1.0.jar lib/javax.servlet-api-3.1.0-1.0.jar
</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib/bcprov-jdk15on-1.57.jar</extdirs>
<extdirs>lib/com.ibm.mq.allclient-9.0.4.0.jar</extdirs>
<extdirs>lib/javax.jms-api-2.0.1.jar</extdirs>
<extdirs>lib/javax.servlet-api-3.1.0.jar</extdirs>
<extdirs>lib/bcpkix-jdk15on-1.57.jar</extdirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
完结。