[置顶] 如何用Maven把依赖也打到可自动执行的jar里面?

标签: Mavenjavajar-with-dependenciemaven-assembly-plugi
757人阅读 评论(3) 收藏 举报
分类:

最近笔者在用Maven打成一个可执行的jar包(包括main方法)的时候,遇到一个比较诡异的错误,Error: A JNI error has occurred, please check your installation and try again。下面是笔者的Manifest文件。那么如何解决呢?

Manifest-Version: 1.0
Built-By: HenryZhu
Build-Jdk: 1.7.0_55
Class-Path: lib/junit-4.12.jar lib/hamcrest-core-1.3.jar lib/selenium-
 java-3.0.1.jar lib/selenium-chrome-driver-3.0.1.jar lib/selenium-remo
 te-driver-3.0.1.jar lib/selenium-api-3.0.1.jar lib/cglib-nodep-3.2.4.
 jar lib/commons-exec-1.3.jar lib/gson-2.3.1.jar lib/guava-19.0.jar li
 b/jna-platform-4.1.0.jar lib/jna-4.1.0.jar lib/selenium-edge-driver-3
 .0.1.jar lib/selenium-firefox-driver-3.0.1.jar lib/selenium-ie-driver
 -3.0.1.jar lib/selenium-opera-driver-3.0.1.jar lib/selenium-safari-dr
 iver-3.0.1.jar lib/netty-3.5.7.Final.jar lib/selenium-support-3.0.1.j
 ar lib/htmlunit-2.23.jar lib/xalan-2.7.2.jar lib/serializer-2.7.2.jar
  lib/commons-lang3-3.4.jar lib/httpclient-4.5.2.jar lib/httpcore-4.4.
 4.jar lib/httpmime-4.5.2.jar lib/commons-codec-1.10.jar lib/htmlunit-
 core-js-2.23.jar lib/neko-htmlunit-2.23.jar lib/xercesImpl-2.11.0.jar
  lib/xml-apis-1.4.01.jar lib/cssparser-0.9.20.jar lib/sac-1.3.jar lib
 /commons-io-2.5.jar lib/commons-logging-1.2.jar lib/phantomjsdriver-1
 .3.0.jar lib/websocket-client-9.2.15.v20160210.jar lib/jetty-util-9.2
 .15.v20160210.jar lib/jetty-io-9.2.15.v20160210.jar lib/websocket-com
 mon-9.2.15.v20160210.jar lib/websocket-api-9.2.15.v20160210.jar lib/h
 tmlparser-2.1.jar lib/htmllexer-2.1.jar lib/jgroups-3.0.11.Final.jar
Created-By: Apache Maven 3.3.9
Main-Class: com.winneryum.csdn.CSDNAutomationAccess
Archiver-Version: Plexus Archiver

下面是笔者的jar的代码结构,




Manifest文件都是对的。指向的路径也是对的。但是在jar运行的时候,就是找不到jar文件里面的lib目录下的依赖的jar文件。

下面这个是笔者自己的在pom.xml文件中用的Build部分的plugin

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.winneryum.csdn.CSDNAutomationAccess</mainClass>
							<addClasspath>true</addClasspath>  
                            <classpathPrefix>lib/</classpathPrefix>  
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>


当前解决的方式,是用不要把把依赖的jar和可执行的jar放在一起,而是直接把依赖的jar文件,解压缩成class文件,然后和我们自己生成的代码放在一起,正确的pom.xml文件中用的Build部分的plugin应该如下:

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.4</version>
				<executions>
					<execution>
						<id>unpack-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>unpack-dependencies</goal>
						</goals>
						<configuration>
							<includes>**/*.class</includes>
							<excludes>**/*.properties</excludes>
							<outputDirectory>${project.build.outputDirectory}</outputDirectory>


						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.winneryum.csdn.CSDNAutomationAccess</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>



上面只是笔者自己的一种解决方案,如果大家有其他的解决方案,希望能给我留言,我把其也添加到里面来。





1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:384200次
    • 积分:6319
    • 等级:
    • 排名:第4065名
    • 原创:227篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏