其实这个功能平时用的实在不多,之前配置过,之后长期不用又忘记了。百度了一圈,按照他们配置的都失败了,平白为这个简单的东西浪费诸多时间。现总结如下,特意记下提醒自己和需要的朋友。
首先如需要的是在打出的JAR包中指定main方法所在的类,也就是启动类:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.henry.netty.webSocket.server.WebSocketServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
至于引入第三方jar使用的是maven的插件maven-shade-plugin,配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass></mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
从打出的jar内容来看,其实是将第三方的jar解压到执行jar包当中。
我记得还有一种方式是不需要解压第三方jar包,
首先需要将依赖的jar列表添加到MANIFEST里面:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.henry.netty.webSocket.server.WebSocketServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
但是却没有找到可以将依赖jar自动一键复制到执行jar的方法,只能手动添加,如果有知道的朋友也可以提醒一下。
欢迎使用haptool.com(哈普工具),让程序员的工作更有效率。