Maven打可运行jar包
来源:互联网 发布:游戏编程软件 编辑:程序博客网 时间:2024/06/08 02:28
Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in xxx.jar"(没有设置Main-Class)、ClassNotFoundException(找不到依赖包)等错误。
要想jar包能直接通过java -jar xxx.jar运行,需要满足:
1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,这样才能确定程序的入口在哪里;
2、要能加载到依赖包。
使用Maven有以下几种方法可以生成能直接运行的jar包,可以根据需要选择一种合适的方法。
方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包
在pom.xml中配置:
<!-- 生成可执行JAR包命令 maven-jar-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <!-- 依赖jar包前缀 --> <classpathPrefix>lib/</classpathPrefix> <!-- Main入口 --> <mainClass>*.spring.ui.Program</mainClass> </manifest> <manifestEntries> <!-- classpath 路径 --> <Class-Path>./</Class-Path> </manifestEntries> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>lib</classifier> <excludes> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </configuration> </execution> </executions> </plugin> <!-- 拷贝依赖的jar包到lib目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>UTF-8</encoding> <!-- 把配置文件拷到和jar包同一个路径下 --> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/main/resources/</directory> <!-- 对于有一层包无法copy到 --> <!--<includes>--> <!--<include>*.xml</include>--> <!--<include>*.properties</include>--> <!--</includes>--> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分内容,<mainClass>*.spring.ui.Programe</mainClass>指定MANIFEST.MF中的Main-Class,<addClasspath>true</addClasspath>会在MANIFEST.MF加上Class-Path项并配置依赖包,<classpathPrefix>lib/</classpathPrefix>指定依赖包所在目录,<Class-Path>./</Class-Path>指明classpath的路径。
例如下面是一个通过maven-jar-plugin插件生成的MANIFEST.MF文件片段:
- Class-Path: ./ lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
- Main-Class: *.spring.ui.Programe
只是生成MANIFEST.MF文件还不够,maven-dependency-plugin插件用于将依赖包拷贝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置,即lib目录下。
配置完成后,通过mvn package指令打包,会在target目录下生成jar包,并将依赖包拷贝到target/lib目录下,目录结构如下:
指定了Main-Class,有了依赖包,那么就可以直接通过java -jar xxx.jar运行jar包。
这种方式生成jar包有个缺点,就是生成的jar包太多不便于管理,下面两种方式只生成一个jar文件,包含项目本身的代码、资源以及所有的依赖包。
方法二:使用maven-shade-plugin插件打包
在pom.xml中配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 由于一些包重复引用,过滤打包后的 META-INF 目录多出了一些 *.SF 等文件 --> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <!-- 打包jar文件时,配置manifest文件,加入程序入口mainClass --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <mainClass>cn.richinfo.spring.ui.Program</mainClass> <Build-Number>123</Build-Number> </manifestEntries> </transformer> <!-- AppendingTransformer 指定当文件冲突时需要合并的文件 --> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/cxf/bus-extensions.txt</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.tooling</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
配置完成后,执行mvn package即可打包。在target目录下会生成两个jar包,注意不是original-xxx.jar文件,而是另外一个。和maven-assembly-plugin一样,生成的jar文件包含了所有依赖,所以可以直接运行。
如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,所以使用AppendingTransformer来对文件内容追加合并。
Maven打可以直接运行的jar包有2中方式:
1、maven-jar-plugin和maven-resources-plugin插件配合可以分隔依赖包到独立文件,但是不是一个整体的jar包。
2、maven-shade-plugin插件可以直接将依赖包整体集成一个jar包。
so.任君选择-_-
so.任君选择-_-
转载链接:
http://blog.csdn.net/xiao__gui/article/details/47341385
阅读全文
0 0
- maven打可运行的jar包
- Maven打可运行jar包
- idea环境下用maven打可运行jar包
- idea maven工程打可运行jar包方式
- maven打可运行jar,包含依赖jar,且打成zip包
- Eclipse 打可运行的jar包
- maven创建可运行jar包
- MAVEN打包可运行jar包
- Maven 如何打包可运行jar包
- Maven 打jar包
- Maven 打jar包
- maven 打jar包
- Maven打Jar包
- maven 打jar包
- maven打Jar包
- maven打jar包
- maven打jar包
- maven打jar包
- 谁获得了最高奖学金
- 20170715 heforher
- 一物一码生码软件5.0发布
- HDU 1856 More is better
- 微信小程序不同页面之间的传值
- Maven打可运行jar包
- CAM350 导出DXF文件
- Spring-Spring Web项目中配置使用Log4j 2
- linux(debian) 安装tomcat 8.5
- django中mysql中表增加字段,migrate失败
- #POJ1328#Radar Installation(贪心)
- 创建第一个Servlet类配置web.xml文件
- eclipse中的maven出现红色的感叹号
- 关联本地仓库与远程gitlab仓库