IDEA导出可执行包
1.* 方式一
1、点击Project Structure按钮->选择Artifacts->点击“+”->选择JAR->选择“From modules with dependecies...”
2、选择App,设置/META-INF/MANIFEST.MF
注意:创建的/META-INF/MANIFEST.MF要放在项目的根目录下面,否则可能导出来的jar包中不包含/META-INF/MANIFEST.MF文件。
3、点击Build->点击“Build Artifacts...”
4、将构建好的jar包拷贝到桌面运行
1.* 方式二
1、选择菜单栏下运行配置的下拉框->Edit Configuration,进入Run/Debug Configurations界面
2、点击“+”号,选择Maven,对Maven进行配置如下:
3、配置完之后点击运行按钮:
4、运行结果
1.*.& 没有主清单属性
1.*.&.% 方式一
点击查看代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.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.hzhiping.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
当上述插件添加之后,重新执行maven编译打包,或者直接执行maven命令:
点击查看代码
mvn -X clean package -Dmaven.test.skip=true
1.*.&.% 方式二
下面这种方式,会将所有的依赖包都打进同一个包中,因此包的体积会变大:
点击查看代码
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--此处指定main方法入口的class-->
<mainClass>com.hzhiping.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
如果出现如下报错,升级插件版本2.6即可:
点击查看代码
constituent[39]: file:/D:/Program%20Files/apache-maven-3.5.4/lib/slf4j-api-1.7.25.jar
constituent[40]: file:/D:/Program%20Files/apache-maven-3.5.4/lib/wagon-file-3.1.0.jar
constituent[41]: file:/D:/Program%20Files/apache-maven-3.5.4/lib/wagon-http-3.1.0-shaded.jar
constituent[42]: file:/D:/Program%20Files/apache-maven-3.5.4/lib/wagon-provider-api-3.1.0.jar
---------------------------------------------------
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
at java.io.PrintStream.write(PrintStream.java:526)
at java.io.PrintStream.print(PrintStream.java:583)
at org.fusesource.jansi.FilterPrintStream.write(FilterPrintStream.java:99)
at org.fusesource.jansi.FilterPrintStream.write(FilterPrintStream.java:107)
at org.fusesource.jansi.FilterPrintStream.print(FilterPrintStream.java:156)
at org.fusesource.jansi.FilterPrintStream.println(FilterPrintStream.java:231)
at org.slf4j.impl.SimpleLogger.write(SimpleLogger.java:318)
at org.slf4j.impl.SimpleLogger.log(SimpleLogger.java:295)
at org.slf4j.impl.SimpleLogger.info(SimpleLogger.java:480)
at org.apache.maven.cli.logging.Slf4jLogger.info(Slf4jLogger.java:59)
at org.codehaus.plexus.archiver.AbstractArchiver$1.hasNext(AbstractArchiver.java:464)
at org.codehaus.plexus.archiver.AbstractArchiver$1.hasNext(AbstractArchiver.java:464)
...............
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
Process finished with exit code 1
当上述插件添加之后,重新执行maven编译打包,或者直接执行maven命令:
点击查看代码
mvn -X clean package -Dmaven.test.skip=true
1.*.&.% 方式三
下面这种方式,会将依赖包的classpath写到MANIFEST.MF文件中,但是不会打包到同一个jar包中,所以如果运行jar包的时候,可能出现NoClassDefError的异常,这时候,我们就需要在此jar包所在的服务器目录下创建一个lib文件夹,然后将相应的jar包拷贝到lib目录下,但是如何定位到相应的jar包呢?在IDEA中可以使用快捷键Ctrl+Shift+T来找到对应的类,这时候就会显示jar包的groupId和artifact,然后我们就能在本地仓库中找到相应的jar包拷贝到文件夹中了:
点击查看代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.hzhiping.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
如果要避免NoClassDefError的问题,有另外一个比较方便的方法,就是直接将Maven依赖的所有jar包都拷贝到lib文件夹下面,然后一次性全部上传到某一个目录下,接着将编译后的项目jar包上传到和lib同级的目录下,即可避免NoClassDefError问题。
将项目依赖的所有jar包都拷贝到lib文件夹下面的命令为:
点击查看代码
$ mvn -X clean jar:jar dependency:copy-dependencies -DoutputDirectory=lib
当上述插件添加之后,重新执行maven编译打包,或者直接执行maven命令:
点击查看代码
mvn -X clean package -Dmaven.test.skip=true