maven 可运行jar包打包

时间:2022-04-30 09:38:39

最近要使用开发socket,但直接放在tomcat中和web项目耦合太深,准备直接打可运行jar,但直接打包出来的没有对应依赖和主函数入口,查了下资料,吧步骤整理了下。

1.在pom文件中的build标签里引入控件。

<pluginManagement>
<plugins>
<!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

2.引入配置主函数入口,和lib依赖打包

<plugins>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!--配置程序入口 -->
<mainClass>你对应的主函数地址(启动服务的main方法)</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<useUniqueVersions>false</useUniqueVersions>

<outputDirectory>

                                          <!--依赖包的地址,结束后放在target文件夹里-->

${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>


4.右击pom文件,选择run as 选 maven install

5.在target找到对应的jar包和依赖目录lib

6.把两者复制到你准备好的运行目录,进入对应的dos地址(window直接在对应目录按shift+右键)

输入java -jar 对应jar包名