使用maven的插件进行maven项目的打包

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 打包 包含资源文件

原文地址:https://www.cnblogs.com/chenjfblog/p/10176205.html