1 maven项目打包的插件有3种

maven-jar-plugin

maven-assembly-plugin

maven-shade-plugin

2 maven-jar-plugin

现在要新增一个Premain-Class属性,配置如下:

    <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.mymaven.test.TestMain</mainClass>
</manifest>
                        <manifestEntries>
<Premain-Class>
com.xzq.test.PreAgent
</Premain-Class>
</manifestEntries>
                    </archive>
</configuration>
</plugin>
</plugins>
</build>

01.指定manfestFile位置:具体配置如下:

<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>

02. 使用maven-jar-plugin 修改 MANIFEST.MF文件,具体代码如下:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

所有 Maven 插件通过一个 <configuration> 元素公布了其配置,在本例中,maven-jar-plugin 修改它的 archive 属性,特别是存档文件的 manifest 属性,它控制 MANIFEST.MF 文件的内容。包括 3 个元素:

  • addClassPath:将该元素设置为 true 告知 maven-jar-plugin 添加一个 Class-Path 元素到 MANIFEST.MF 文件,以及在 Class-Path 元素中包括所有依赖项。
  • classpathPrefix:如果您计划在同一目录下包含有您的所有依赖项,作为您将构建的 JAR,那么您可以忽略它;否则使用 classpathPrefix 来指定所有依赖 JAR 文件的前缀。在清单 1 中,classpathPrefix 指出,相对存档文件,所有的依赖项应该位于 “lib” 文件夹。
  • mainClass:当用户使用 lib 命令执行 JAR 文件时,使用该元素定义将要执行的类名。上述可以通过是用maven-dependency-plugin将依赖包添加进去
maven-dependency-plugin:
当您使用这 3 个元素配置好了 MANIFEST.MF 文件之后,下一步是将所有的依赖项复制到 lib 文件夹。为此,使用 maven-dependency-plugin。代码如下: 

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals><goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions></plugin>

maven-dependency-plugin 有一个 copy-dependencies,目标是将您的依赖项复制到您所选择的目录。本例中,我将依赖项复制到 build 目录下的 lib 目录(project-home/target/lib)。

将您的依赖项和修改的 MANIFEST.MF 放在适当的位置后,您就可以用一个简单的命令启动应用程序:

java -jar jarfilename.jar

参考:maven打包在MANIFEST.MF文件中增加属性

spring-boot-maven-plugin插件的作用,参考:http://www.cnblogs.com/acm-bingzi/p/mavenSpringBootPlugin.html

如何构建多个子目录,参考:http://www.cnblogs.com/acm-bingzi/p/6625202.html
 

3 maven-assembly-plugin

        <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>com.panda521.SpringPracServerApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>

使用 maven assembly:assembly进行打包操作

4 maven-shade-plugin

使用maven-shade-plugin 的注意项

maven-shade-plugin插件有个配置属性:createDependencyReducedPom,默认值为true.

如果你用这个插件来deploy,或者发布到中央仓库

这个属性会缩减你的pom文件,会把你依赖的<dependency>干掉,正确的做法是把这个值改成false

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.panda521.SpringPracServerApplication</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

参考: maven-shade-plugin 入门指南

参考maven官方文档: 
[http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html#](http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html#)

maven-shade-plugin介绍及使用

5 maven插件使用的注意项

pluginManagement标签

pluginManagement标签的配置是方便子项目引用的,

但是在idea2017没法显示maven引入插件,所以只能注释了,

至于它跟plugins标签有什么区别,百度看看就一目了然了

maven 打包 包含资源文件

最新文章

  1. SSISDB4:Execution
  2. HDU 1405
  3. python实践——批量统计mongodb数据库的集合大小
  4. 折腾笔记之wordpress安装出现错误---【wordpress点击文章找不到网页的解决办法】
  5. Unity手游之路&lt;十二&gt;手游资源热更新策略探讨
  6. 数据库Error:The ScriptCollection in ScriptName not find
  7. DXT纹理压缩
  8. Linux查看CPU信息
  9. JQ避免出现多次执行一个事件的解决方案
  10. 判断手机还是PC浏览器
  11. Linux启动级别
  12. hdu4745
  13. 手势(Gesture)的增加和识别
  14. RabbitMQ 3.6.1 升级至 3.7.9 版本(Windows 升级至Centos)
  15. Android-PullToRefresh 下拉刷新增加setOnItemLongClickListener
  16. Windows 网卡超过序列
  17. Go Revel - Websockets
  18. PHP与Java进行通信的实现方法
  19. 尝试.Net Core—使用.Net Core + Entity FrameWork Core构建WebAPI(一)
  20. redis tutorail

热门文章

  1. Scala学习之路 (二)使用IDEA开发Scala
  2. ingress 代理方式
  3. vagrant特性——基于docker开发环境(docker和vagrant的结合)-2-命令
  4. Shell学习心得(二):传递参数、运算符
  5. Java之时间转换
  6. Android给拼接好的Bitmap加上个性化边框
  7. oracle 在存储过程或函数中得到异常sql
  8. 模板自定义函数 template function
  9. tomcat-在cmd窗口启动Tomcat
  10. [HAOI2008]排名系统 &amp; [Zjoi2006]GameZ游戏排名系统 BZOJ1862&amp;BZOJ1056