多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # IDEA打包 打包自定义工具类文件 ![](https://box.kancloud.cn/941c100dbbd1c9d035c83b49a25a3377_508x409.png) ![](https://box.kancloud.cn/a2658365b9298b8ca481aeaa73cdf105_686x299.png) ![](https://box.kancloud.cn/664a44d035d77cb34d108ba022bab302_515x396.png) ![](https://box.kancloud.cn/d4f14c90ae4b4cd4594a5edc4e29ac0d_797x425.png) ![](https://box.kancloud.cn/dd8c3d461bfe70941a890932a8a930a2_433x493.png) ![](https://box.kancloud.cn/dc6591d676a357cb5f94fdeb555d8e8e_304x231.png) 然后`java -jar xx.jar` # maven war包 注意:需要在pom.xml中注明打包方式为war ![](https://box.kancloud.cn/951fc21a4107679fc69bed865eed0a8d_766x376.png) 点击界面最右侧的选项:Maven Projects -> 双击package ![](https://box.kancloud.cn/fd7ec77c31be029414a6450596d892b0_224x333.png) war包存在目录结构 ![](https://box.kancloud.cn/30dfb2a3543b3764778cf34cdcb00cc1_342x382.png) 测试 `java -jar xx.jar` 测试成功!(该war包内含tomcat) # maven-assembly-plugin 在pom.xml中加入一个插件来打包,如下: ~~~ <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.yj.spark.TestSpark</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ 这种方式是通过maven命令来打包的: ![](https://box.kancloud.cn/4861060748b9c8a42cb4b7ab695992f9_168x354.png) 或者 `mvn assembly:assembly` 见名知意,其中 spark-1.0.jar 只有代码,无依赖,而 spark-1.0-jar-with-dependencies.jar则包含所有jar包 ![](https://box.kancloud.cn/b4c90825a2f045fc7f3b2831795a8687_292x235.png) # 详解 简单的说,maven-assembly-plugin 就是用来帮助打包用的,比如说打出一个什么类型的包,包里包括哪些内容等等。 目前至少支持以下打包类型: * zip * tar * tar.gz * tar.bz2 * jar * dir * war 默认情况下,打jar包时,只有在类路径上的文件资源会被打包到jar中,并且文件名是${artifactId}-${version}.jar,下面看看怎么用maven-assembly-plugin插件来定制化打包 首先需要添加插件声明: ~~~ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ 默认情况下,maven-assembly-plugin内置了几个可以用的assembly descriptor: * bin : 类似于默认打包,会将bin目录下的文件打到包中 * jar-with-dependencies : 会将所有依赖都解压打包到生成物中 * src :只将源码目录下的文件打包 * project : 将整个project资源打包 使用 descriptors,指定打包文件 src/assembly/src.xml,在该配置文件内指定打包操作。 ~~~ <project> [...] <build> [...] <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <descriptors> <descriptor>src/assembly/src.xml</descriptor> </descriptors> </configuration> [...] </project> ~~~ 要查看它们的详细定义,可以到maven-assembly-plugin-2.4.jar里去看,例如对应 bin 的assembly descriptor 如下: ~~~ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>bin</id> <formats> <format>tar.gz</format> <format>tar.bz2</format> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}</directory> <outputDirectory>/</outputDirectory> <includes> <include>README*</include> <include>LICENSE*</include> <include>NOTICE*</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}/site</directory> <outputDirectory>docs</outputDirectory> </fileSet> </fileSets> </assembly> ~~~ ## 描述符文件元素 ~~~ <id>release</id> ~~~ id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话,目标文件则是 `${artifactId}-${id}.tar.gz` ## formats maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式 ~~~ <formats> <format>tar.gz</format> <format>dir</format> </formats> ~~~ ## dependencySets 用来定制工程依赖 jar 包的打包方式,核心元素如下表所示 ![](https://box.kancloud.cn/3fbf442aee6624c20097de6ca0ba1b09_643x150.png) ~~~ <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> </dependencySet> </dependencySets> ~~~ ## fileSets 管理一组文件的存放位置,核心元素如下表所示。 ![](https://box.kancloud.cn/a08aa5e0037818a863eed27948c6047f_792x190.png) ~~~ <fileSets> <fileSet> <includes> <include>bin/**</include> </includes> <fileMode>0755</fileMode> </fileSet> <fileSet> <includes> <include>/conf/**</include> <include>logs</include> </includes> </fileSet> </fileSets> ~~~ ## files 可以指定目的文件名到指定目录,其他和 fileSets 相同,核心元素如下表所示 ![](https://box.kancloud.cn/414f5c391aadee1a801f7059a2d19571_580x185.png) ~~~ <files> <file> <source>README.txt</source> <outputDirectory>/</outputDirectory> </file> </files> ~~~ 例子: pom.xml ~~~ <build> <finalName>scribe-log4j2-test</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors> <descriptor>src/main/assembly/release.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ~~~ release.xml ~~~ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <formats> <format>tar.gz</format> <format>dir</format> </formats> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <includes> <include>bin/**</include> </includes> <fileMode>0755</fileMode> </fileSet> <fileSet> <includes> <include>/conf/**</include> <include>logs</include> </includes> </fileSet> </fileSets> <files> <file> <source>README.txt</source> <outputDirectory>/</outputDirectory> </file> </files> </assembly> ~~~ ## 自定义Assembly Descriptor ~~~ <?xml version='1.0' encoding='UTF-8'?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>demo</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly> ~~~ 这个定义很简单: * format:指定打包类型 * includeBaseDirectory:指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下) * fileSets:指定要包含的文件集,可以定义多个fileSet * directory:指定要包含的目录 * outputDirectory:指定当前要包含的目录的目的地 要使用这个assembly descriptor,需要如下配置: ~~~ <configuration> <finalName>demo</finalName> <descriptors> <descriptor>assemblies/demo.xml</descriptor> </descriptors> <outputDirectory>output</outputDirectory> </configuration> ~~~ 最后会生成一个demo-demo.jar 文件在目录 output 下,其中前一个demo来自finalName,后一个demo来自assembly descriptor中的id,其中的内容和默认的打包出来的jar类似。 如果只想有finalName,则增加配置: ~~~ <appendAssemblyId>false</appendAssemblyId> ~~~ **添加文件** 上面演示了添加所有编译后的资源,同样的可以增加其他资源,例如想添加当前工程目录下的某个文件 b.txt ,在assembly descriptor的assembly结点下增加 ~~~ <files> <file> <source>b.txt</source> <outputDirectory>/</outputDirectory> </file> </files> ~~~ 这里用到了 files 元素类型,可以想象 fileSets 下的结点都是针对文件夹的;files 下的结点都是针对文件的。 也可以改变打包后的文件名,例如上面的 b.txt ,希望打包后的名字为 b.txt.bak, 只需要在file 里添加以下配置 : ~~~ <destName>b.txt.bak</destName> ~~~ **排除文件** 在fileSet里可以使用includes 和 excludes来更精确的控制哪些文件要添加,哪些文件要排除。 例如要排除某个目录下所有的txt文件: ~~~ <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> <excludes> <exclude>**/*.txt</exclude> </excludes> </fileSet> ~~~ 或者某个目录下只想 .class 文件: ~~~ <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> <includes> <include>**/*.class</include> </includes> </fileSet> ~~~ **添加依赖** 如果想把一些依赖库打到包里,可以用 dependencySets 元素,例如最简单的,把当前工程的所有依赖都添加到包里: ~~~ <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> </dependencySet> </dependencySets> ~~~ 如果要排除工程自身生成的jar,则可以添加: ~~~ <useProjectArtifact>false</useProjectArtifact> ~~~ unpack参数可以控制依赖包是否在打包进来时是否解开,例如解开所有包,添加以下配置 ~~~ <unpack>true</unpack> ~~~ 和 fileSet 一样,可以使用 excludes 和 includes 来更详细的控制哪些依赖需要打包进来;另外 useProjectAttachments,useTransitiveDependencies,useTransitiveFiltering等参数可以对间接依赖、传递依赖进行控制 ## 其他选项 * moduleSets:当有子模块时候用 * repositories:想包含库的时候用 * containerDescriptorHandlers:可以进行一些合并,定义ArtifactHandler之类的时候可以用 * componentDescriptors:如上所述,可以包含一些componentDescriptor定义,这些定义可以被多个assembly共享 ## Assembly Plugin更多配置 **指定Main-Class** archive的一个重要用处就是配置生成的MANIFEST.MF文件。默认会生成一个MANIFEST.MF文件,不过这个文件默认值没什么意义。如果想指定生成jar的Main-Class,可以如下配置: ~~~ <archive> <manifest> <mainClass>demo.DemoMain</mainClass> </manifest> </archive> ~~~ **添加MANIFEST项** 除了可以指定Main-Class外,还可以添加任意项。比如在OSGI bundle的MANIFEST.MF定义里就有很多用来定义bundle的属性的项,如Import-Package,Export-Package等等。要添加项,可以使用如下配置: ~~~ <archive> <manifestEntries> <Import-Package>javax.xml.ws.*</Import-Package> </manifestEntries> </archive> ~~~ **指定MANIFEST.MF文件** 还可以直接指定MANIFEST.MF文件。如下: ~~~ <archive> <manifestFile>META-INF/MANIFEST.MF</manifestFile> </archive> ~~~ # 例子一 在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序。最近在工作中也遇到了这种场景,使用quartz开发一个任务调度程序。程序中依赖很多jar包,项目的启动时只需要初始化spring容器即可 使用方法 使用一个简单的基于spring框架的demo来做程序示例,来介绍maven assembly插件的使用方法。 项目中的代码目录如下: ![](https://box.kancloud.cn/ece6fd7b459bcb5eaca214b766780a29_181x318.png) 在以上代码目录中,assembly目录下为打包的描述文件,下面会详细介绍该文件,bin目录下为启动脚本以及readme等文件,main下为maven标准中建立的文件,java代码以及配置文件位于该目录下。 打包完成后压缩包目录如下: ![](https://box.kancloud.cn/25fb8851120f023c34b3ac0ebd63c4b7_502x117.png) 打包完成后,我们可以看到bin目录来存放启动脚本等文件,config为配置文件,lib下为运行时依赖的jar包。 使用maven assembly插件需要在pom文件中配置,添加这个插件 ~~~ <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>make-zip</id> <!-- 绑定到package生命周期阶段上 --> <phase>package</phase> <goals> <!-- 绑定到package生命周期阶段上 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路径--> <descriptor>src/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> ~~~ 其中execution节点,我们配置了执行maven assembly插件的一些配置,descriptor节点配置指向assembly.xml的路径。 在assembly.xml配置了,我们打包的目录以及相应的设置 ~~~ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>distribution</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> </fileSet> <fileSet> <directory>${project.basedir}\src\bin</directory> <outputDirectory>\bin</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <!-- 将scope为runtime的依赖包打包到lib目录下。 --> <scope>runtime</scope> </dependencySet> </dependencySets></assembly> ~~~ assembly.xml的配置项非常多,可以参考http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 以上只是用了很少的一部分。 format设置包输出的格式,以上格式设置的为zip格式,目前还支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式 fileSet定义代码目录中与输出目录的映射,在该节点下还有 <includes/>,<excludes/>两个非常有用的节点。 比如: ~~~ <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> <includes> <include>some/path</include> </includes> <excludes> <exclude>some/path1</exclude> </excludes> </fileSet> ~~~ 以上代码表示归档时包括some/path,不包括some/path1 dependencySets节点下为依赖设置 在上述配置中,表示所有运行时依赖的jar包归档到lib目录下。在上述截图中lib目录下的文件就是所有依赖的jar包 ![](https://box.kancloud.cn/0a839e7c4af5711894334d4d772bf9da_496x184.png) # 例子二 ## 需求 打一个zip包,包含如下: ![](https://box.kancloud.cn/075995f03976a2aa71afb0717a29ed25_532x141.png) bin为程序脚本,启动和停止 lib为依赖包 根目录下为配置文件和项目jar包 ## 插件了解 ![](https://box.kancloud.cn/66e293d4c9312fc24948a2d36bac1a40_769x227.png) ## 配置 ~~~ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <mainClass>com.jd.bt.ZuulApplication</mainClass> <!-- to create a class path to your dependecies you have to fill true in this field --> <!-- 增加classpath --> <addClasspath>true</addClasspath> <!-- classpath路径前缀 --> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> <excludes> <!--注意从编译结果目录开始算目录结构--> <exclude>/*.yml</exclude> <exclude>/*.properties</exclude> <exclude>/*.xml</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- not append assembly id in release file name --> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/depolyment.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>dist</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ **对于maven-jar-plugin配置**   其中manifest的部分是核心,在可执行的jar文件中,打包后会在jar文件内的META-INF文件夹下,生成一个MANIFEST.MF文件,里面记录了可执行文件的一些相关配置,比如像上面一段代码中所配置的内容,这里面就配置了可执行jar文件未来读取classpath的相对目录位置在什么地方,以及引入的jar文件都有哪些,上面的配置就是classpath目录是./   mainClass配置表示,哪个class作为程序的入口来执行   addClasspath配置表示,是否将依赖的classpath一起打包   classpathPrefix配置表示,依赖的classpath的前缀,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都会加上前缀,lib/,比如fastjson-1.2.7.jar,在mainfest文件里就会是lib/fastjson-1.2.7.jar   excludes配置表示,排除哪些文件夹不被打包进去   注意:其实maven-jar-plugin主要就是配置了MANIFEST.MF这个文件而已,就是让可执行文件知道自己怎么执行,加载哪些文件执行的描述,剩下的工作交由maven-assembly-plugin来处理 最终压缩的文件格式,为zip,fileSets中配置了需要将那些文件打包到最终压缩包中,     如配置文件包括了启动脚本bin文件夹,里面放着shell的启动脚本,相关的配置文件src/main/resources,里面放着整个程序提取的properties等相关的配置文件      最终可运行的jar文件,使用了${project.build.directory}变量,也就是通过maven-jar-plugin生成的那个jar文件   dependencySets里面配置了依赖库最终输出到lib文件夹下,与上面的maven-jar-plugin配置生成的manifest文件路径相对应,这样可运行jar就会按照manifest的路径来找相应的文件进行加载 **src/main/assembly/depolyment.xml配置** ~~~ <?xml version="1.0" encoding="UTF-8"?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <id>dist</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/bin</directory> <outputDirectory>bin/</outputDirectory> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>/</outputDirectory> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${groupId}:${artifactId}</exclude> </excludes> </dependencySet> </dependencySets> </assembly> ~~~ **start.sh和stop.sh** `start.sh` ~~~ #!/bin/bash ARTIFACT_ID=jd-bt-microservice-gateway-zuul VERSION=0.0.1-SNAPSHOT main_jar=${ARTIFACT_ID}-${VERSION}.jar PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then echo "${ARTIFACT_ID} already started!" echo "PID: $PIDS" exit 0; fi BIN_PATH="${JAVA_HOME}/bin" LOG_PATH="/Users/lihongxu/log/${ARTIFACT_ID}/" mkdir -p $LOG_PATH echo "ready start ${main_jar}"; nohup $BIN_PATH/java -server -Xms4096m -Xmx4096m -jar ../${main_jar} >$LOG_PATH/nohup.log 2>&1 & sleep 5 PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then echo " ${ARTIFACT_ID} Started Successed,pid:${PIDS}" exit 0; else echo " ${ARTIFACT_ID} Started Failed" exit 1; fi ~~~ `stop.sh` ~~~ #!/bin/bash ARTIFACT_ID=jd-bt-microservice-gateway-zuul VERSION=0.0.1-SNAPSHOT main_jar=${ARTIFACT_ID}-${VERSION}.jar PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then kill -9 "$PIDS" echo " ${ARTIFACT_ID} is stop !" echo " PID: $PIDS" exit 0; fi ~~~ ## 启动 java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号";",linux使用":" * 格式: `java -cp .;myClass.jar packname.mainclassname` 表达式支持通配符,例如: `java -cp .;c:\classes01\myClass.jar;c:\classes02\*.jar packname.mainclassname` * 运行jar `java -jar myClass.jar` 执行该命令时,会用到目录META-INF\MANIFEST.MF文件,在该文件中,有一个叫Main-Class的参数,它说明了java -jar命令执行的类。 * 运行jar和cp 用maven导出的包中,如果没有在pom文件中将依赖包打进去,是没有依赖包。 1. 打包时指定了主类,可以直接用java -jar xxx.jar。 2. 打包是没有指定主类,可以用java -cp xxx.jar 主类名称(绝对路径)。 3. 要引用其他的jar包,可以用java -classpath $CLASSPATH:xxxx.jar 主类名称(绝对路径)。其中 -classpath 指定需要引入的类。 实例 下面基于pom和META-INF\MANIFEST.MF两个文件的配置,进行了三种情况的测试: pom.xml的build配置: ~~~ <build> <!--<finalName>test-1.0-SNAPSHOT</finalName>--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>test.core.Core</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <!--下面是为了使用 mvn package命令,如果不加则使用mvn assembly--> <executions> <execution> <id>make-assemble</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ~~~ META-INF\MANIFEST.MF的内容: Manifest-Version: 1.0 Main-Class: test.core.Core 1. pom中build指定mainClass 但是 META-INF\MANIFEST.MF文件中没有指定Main-Class: test.core.Core ~~~ java -jar test-jar-with-dependencies.jar //执行成功 java -cp test-jar-with-dependencies.jar test.core.Core //执行失败,提示jar中没有主清单属性 ~~~ 2. pom中build没有指定mainClass 但是 META-INF\MANIFEST.MF文件中指定了Main-Class: test.core.Core ~~~ java -jar test-jar-with-dependencies.jar //执行失败,提示jar中没有主清单属性 java -cp test-jar-with-dependencies.jar test.core.Core //执行成功 ~~~ 3. pom中build指定mainClass && META-INF\MANIFEST.MF文件中增加了Main-Class: test.core.Core ~~~ java -cp test-jar-with-dependencies.jar test.core.Core //执行成功 java -jar test-jar-with-dependencies.jar //执行成功 ~~~