maven-插件

maven插件是在生命周期中某些阶段执行的任务。一个插件完成一项功能。

1. maven-compiler-plugin

2. maven-jar-plugin

3. maven-shade-plugin

4. maven-assembly-plugin

5. spring-boot-maven-plugin

maven-compiler-plugin

编译Java源码,一般只需设置编译的jdk版本

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

maven-jar-plugin

maven 默认打包插件,用来创建 project jar。

打成jar时,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>/data/lib</classpathPrefix>
                <mainClass>com.zhang.spring.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

maven-shade-plugin

用于把多个jar包,打成1个jar包,一般Java项目都会依赖其他第三方jar包,最终打包时,希望把其他jar包包含在一个jar包里。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.test.WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

maven-assembly-plugin

定制化打包方式。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 只运行一次 -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${assembly.name}</finalName>
                            <!--false 打包文件末尾不包含assembly id-->
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <!--配置描述文件路径-->
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <outputDirectory>${project.parent.basedir}/dist</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

assembly.xml:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>${project.version}</id>
    <formats>
        <format>tgz</format>
    </formats>
    <!--默认值是:true。
        设置为true将创建一个包含此基本目录的归档文件。      /libra-service-1.1.0/libra-service-1.1.0
        设置为false,则创建的存档将其内容解压缩到当前目录。 /libra-service-1.1.0
     -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <!--
    libra-service-1.1.0 (libra-service-1.1.0.tgz)
      conf:
        conf下面所有文件
      jars:
        libra-service-1.1.0.jar
        libra-dps.jar
      sbin:
        sbin下面所有文件
    -->
    <!--指定包含在程序集中的每个包含模块的哪些文件组。fileSet通过提供一个或多个<fileSet>子元素来指定。-->
    <fileSets>
        <fileSet>

            <!--设置模块目录的绝对或相对位置。-->
            <directory>${project.parent.basedir}/libra-dps/target</directory>
            <!--设置输出目录相对于程序集根目录的根目录。-->
            <outputDirectory>${assembly.name}/jars</outputDirectory>
            <includes>
                <include>libra*.jar</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.parent.basedir}/libra-service/target</directory>
            <outputDirectory>${assembly.name}/jars</outputDirectory>
            <includes>
                <include>libra*.jar</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.parent.basedir}/sbin</directory>
            <outputDirectory>${assembly.name}/sbin</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.parent.basedir}/conf</directory>
            <outputDirectory>${assembly.name}/conf</outputDirectory>
            <includes>
                <include>*</include>
                <include>*/*</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

这里指定将两个jar包, sbin文件夹, conf文件夹打包生成在libra-service-1.1.0.tgz

spring-boot-maven-plugin

spring-boot-maven-plugin插件是将springboot的应用程序打包成fat jar的插件。

我们一般的jar,里面放的是.class文件已经resources目录下的东西,但是fat jar 它可以把jar作为内容包含进去。也就是说,spring boot 借助spring-boot-maven-plugin将所有应用启动运行所需要的jar都包含进来,从逻辑上将具备了独立运行的条件。

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${springboot.verison}</version>
        <configuration>
          <mainClass>com.ultiwill.libra.Application</mainClass>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
posted @ 2020-08-11 17:48  所向披靡zz  阅读(189)  评论(0编辑  收藏  举报