如何讓使用maven產生的jar包直接運行

本文首先借鑑網上的內容,同時補充不足。

借鑑內容:

執行mvn clean package生成的JAR包默認是不可執行的,因爲帶有main方法的信息不會被添加到manifest中。使用java -jar運行該包時,報錯如下:

  no main manifest attribute, in original-helloworld-1.0-SNAPSHOT.jar

爲了生成可執行的JAR包,需要藉助maven-shade-plugin插件,生成可執行JAR包只是該插件的功能之一,其他功能見官網,配置示例網址爲:

http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

關鍵的一行在於mainClass,它重新設定了程序運行的入口地址,其內容就是我們編寫的類名稱。

採用maven官網的內容配置之後,執行時發生:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
    at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
    at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
    at java.util.jar.JarVerifier.update(JarVerifier.java:230)
    at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
    at java.util.jar.JarFile.getInputStream(JarFile.java:450)
    at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:977)
    at sun.misc.Resource.cachedInputStream(Resource.java:77)
    at sun.misc.Resource.getByteBuffer(Resource.java:160)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

這個問題的解決需要在pom.xml文件中加入內容:

For those who got this error when trying to create an uber-jar with maven-shade-plugin, the solution is to exclude manifest signature files by adding the following lines to the plugin configuration:

<configuration>
    <filters>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
    <!-- Additional configuration. -->
</configuration>

添加完成後就是這樣一個結構:

<build>
	  <plugins>
		  <plugin>
			  <groupId>org.apache.maven.plugins</groupId>
			  <artifactId>maven-shade-plugin</artifactId>
			  <version>3.1.1</version>
			  <executions>
				  <execution>
					  <phase>package</phase>
					  <goals>
						  <goal>shade</goal>
					  </goals>
					  <configuration>
						  <transformers>
							  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								  <mainClass>com.nusmart.rules.server.RulesEngineServer</mainClass>
							  </transformer>
						  </transformers>
						  <filters>
							  <filter>
								  <artifact>*:*</artifact>
								  <excludes>
									  <exclude>META-INF/*.SF</exclude>
									  <exclude>META-INF/*.DSA</exclude>
									  <exclude>META-INF/*.RSA</exclude>
								  </excludes>
							  </filter>
						  </filters>
					  </configuration>
				  </execution>
			  </executions>
		  </plugin>
	  </plugins>
  </build>

再次編譯,執行,一切正常了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章