Maven 之 常用plugin

Posted 走慢一点点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven 之 常用plugin相关的知识,希望对你有一定的参考价值。

Maven项目的标准目录结构

目录结构说明
src/main/javaapplication library sources - java源代码文件,会自动编译到classes文件夹下
src/main/resourcesapplication library resources - 资源库,会自动编译到classes文件夹下
src/main/filtersresources filter files - 资源过滤文件
src/main/assemblyassembly descriptor - 组件的描述配置,如何打包
src/main/configconfiguration files - 配置文件
src/main/webappweb application sources - web应用的目录,WEB-INF,js,css等
src/main/bin脚本库
src/test/java单元测试java源代码文件
src/test/resources测试需要的资源库
src/test/filters测试资源过滤库
src/site一些文档
pom.xml工程描述文件
LICENSE.txtlicense
README.txtread me
target/存放项目构建后的文件和目录,jar包,war包,编译的class文件等;Maven构建时生成的

Maven属性

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.encoding>UTF-8</java.encoding>
    <java.version>1.7</java.version>
</properties>

文件编码配置

<!-- 设置源文件编码方式 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>$java.version</source>
        <target>$java.version</target>
        <encoding>$project.build.sourceEncoding</encoding>
    </configuration>
</plugin>

资源文件的配置

资源文件是Java代码中要使用的文件。代码在执行的时候会到指定位置去查找这些文件。Maven默认打包src/main/resources下的资源文件到classes下,但是有时候我们需要进行自定义的配置。比如:
1、某些配置文件可能放在src/main/java目录中(如mybatis或hibernate的表映射文件)
2、希望把其他目录中的资源也复制到classes目录中

实现方法(两种方法):

1、配置resouces节点

<build>
    .......
      <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*</exclude>
             </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    ......
</build>

2、配置资源处理插件

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.0.2</version>
  <!-- 解决资源文件的编码问题 -->
  <configuration>
      <encoding>$project.build.sourceEncoding</encoding>
  </configuration>
  <executions>
      <execution>
          <id>copy-resources</id>
          <phase>validate</phase>
          <goals>
              <goal>copy-resources</goal>
          </goals>
          <configuration>
              <outputDirectory>$basedir/target/classes</outputDirectory>
              <resources>
                  <resource>
                      <directory>src/main/java</directory>
                        <includes>
                            <include>**/*.xml</include>
                        </includes>
                  </resource>
              </resources>
          </configuration>
      </execution>
  </executions>
</plugin>

另一个插件也能完成相同的功能

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>add-resource</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>add-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/main/java</directory>
                        <includes>
                            <include>**/*.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

打包配置

maven-jar-plugin 无依赖其他任何jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <!-- 过滤掉不希望包含在jar中的文件 -->
        <excludes>
            <exclude>**/*-dev.properties</exclude>
            <exclude>**/*-dev.xml</exclude>
        </excludes>
        <archive>
            <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <!-- 是否要把第三方jar放到manifest的classpath中 -->
                <addClasspath>false</addClasspath>
                <!-- 生成的manifest中classpath的前缀,比如把第三方jar放到lib目录下 -->
                <classpathPrefix>lib/</classpathPrefix>
                <!-- 应用的main class -->
                <mainClass>cn.toltech.dubbo.insp.main.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

运行:mvn clean package,在target中找到打包出来的,命令后运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去。

如需将依赖的包也打进去可使用一下三种方式之一

maven-shade-plugin 依赖jar(解压后)和本地源文件柔和到一起

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</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.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>cn.toltech.dubbo.insp.main.Main</mainClass>
                    </transformer>
                </transformers>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>executable</shadedClassifierName>
            </configuration>
        </execution>
    </executions>
</plugin>

打包jar展开结果如下图:

maven-assembly-plugin 归档、压缩

如下配置,打包结果和maven-shade-plugin的结果一样。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>com.think.TestMain</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assembly</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但有一个问题,使用以上配置打包出来的jar,在运行java -jar xxx.jar的时候可能会报错,比如

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinit
ionParsingException: Configuration problem: Unable to locate Spring NamespaceHan
dler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: URL [jar:file:/D:/Java/Java%20Project/GenerateImprovedTarget
ByBatch/target/GenerateImprovedTargetByBatch.jar!/META-INF/spring/spring-context
.xml]

而 maven-shade-plugin 中因为配置了

<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.schemas</resource>  
</transformer> 

java -jar xxx.jar运行的时候就没问题。

jar归档,最终压缩成zip

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <mainClass>cn.toltech.dubbo.insp.main.Main</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>src/main/assembly/assembly.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>

需要额外的配置,一般放在src/main/assembly目录下,名字如assembly.xml:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>$project.version-release</id>
    <baseDirectory>GenerateImprovedTargetByBatch-$project.version</baseDirectory>
    <includeBaseDirectory>true</includeBaseDirectory>

    <!-- 最终打包成一个用于发布的zip文件 -->
    <formats>
        <format>zip</format>
    </formats>

    <!-- 第三方jar打包进zip文件的lib目录 -->
    <dependencySets>
        <dependencySet>
            <!-- 第三方jar不要解压 -->
            <unpack>false</unpack>
            <scope>runtime</scope>
            <!-- 不使用项目的artifact -->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>$project.basedir</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.txt</include>
            </includes>
        </fileSet>
        <!-- 把项目的配置文件,打包进zip文件的config目录 -->
        <!-- $project.build.directory是maven变量,内置的,表示target目录 -->
        <fileSet>
            <directory>$project.build.directory/classes</directory>
            <outputDirectory>/conf</outputDirectory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <!--MyBatis的Mapper文件不需要打包到conf目录中-->
            <excludes>
                <exclude>**/*Mapper.xml</exclude>
            </excludes>
        </fileSet>
        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>$project.build.directory</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

打包结果如下图:

maven-dependency-plugin + maven-jar-plugin 归档、压缩

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <!--&lt;!&ndash; 不希望某些文件打入jar &ndash;&gt;-->
        <!--<excludes>-->
        <!--<exclude>**/*.properties</exclude>-->
        <!--</excludes>-->
        <archive>
            <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <!--&lt;!&ndash; 是否要把第三方jar放到manifest的classpath中 &ndash;&gt;-->
                <!--<addClasspath>true</addClasspath>-->
                <!--&lt;!&ndash; 生成的manifest中classpath的前缀,比如把第三方jar放到lib目录下 &ndash;&gt;-->
                <!--<classpathPrefix>lib/</classpathPrefix>-->
                <!-- 应用的main class -->
                <mainClass>cn.toltech.dubbo.insp.main.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</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>
                <!-- $project.build.directory是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
                <outputDirectory>$project.build.directory/lib</outputDirectory>
                <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
                <excludeTransitive>true</excludeTransitive>
                <!-- 复制的jar文件去掉版本信息 -->
                <stripVersion>false</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

打包结果如下图:

以上是关于Maven 之 常用plugin的主要内容,如果未能解决你的问题,请参考以下文章

Maven插件系列之spring-boot-maven-plugin

Maven插件系列之spring-boot-maven-plugin

SpringBoot入门之spring-boot-maven-plugin

介绍 maven 三个常用的插件使用注意事项

介绍 maven 三个常用的插件使用注意事项

介绍 maven 三个常用的插件使用注意事项