常用的 maven 插件总结

分享 青牛 ⋅ 于 2017-01-05 09:10:13 ⋅ 3986 阅读

我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成。
这里总结一些常用插件:

maven-compile-plugin

这个插件就如同名字所显示的这样,用来编译源代码的

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>  
  <artifactId>maven-compiler-plugin</artifactId>  
  <version>2.3.2</version>  
  <configuration> 
    <source>${jdk.version}</source>  
    <target>${jdk.version}</target>  
    <encoding>UTF-8</encoding>  
    <compilerVersion>${jdk.version}</compilerVersion>  
    <verbose>true</verbose> 
  </configuration> 
</plugin>

exec-maven-plugin

它能让你运行任何本地的系统程序,就是使用 mvn exec:exec。除了exec目标之外,exec-maven-plugin还提供了一个java目标,该目标要求你提供一个mainClass参数,然后它能够利用当前项目的依赖作为classpath,在同一个JVM中运行该mainClass。你可以在POM中配置好exec-maven-plugin的相关运行参数,然后直接在命令运行 mvn exec:java 以查看运行效果。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>com.hainiubl.App</mainClass>
  </configuration>
</plugin>

maven-dependency-plugin

  • maven-dependency-plugin最大的用途是帮助分析项目依赖
  • dependency:list能够列出项目最终解析到的依赖列表
  • dependency:tree能进一步的描绘项目依赖树
  • dependency:analyze可以告诉你项目依赖潜在的问题

这个插件可以把你项目依赖的jar包,copy到 一个目录,比如libs。这样部署执行的时候只需要将编译生成的程序jar包和依赖包文件夹拷到特定目录去执行。

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

maven-war-plugin

打包war项目,排除某些web资源文件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
      <webResources>
        <resource>
          <directory>src/main/webapp</directory>
          <excludes>
            <exclude>**/*.jpg</exclude>
          </excludes>
        </resource>
      </webResources>
    </configuration>
  </plugin>

maven-source-plugin

生成项目源码包

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.1.2</version>
    <executions>
      <execution>
        <id>attach-sources</id>
        <phase>verify</phase>
        <goals>
          <goal>jar-no-fork</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

maven-javadoc-plugin

生成javadoc

  <plugin>          
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.7</version>
    <executions>
      <execution>
        <id>attach-javadocs</id>
          <goals>
            <goal>jar</goal>
          </goals>
      </execution>
    </executions>
  </plugin>    

maven-shade-plugin

生成可执行jar,可以让用户配置Main-Class的值,然后在打包的时候将值填入/META-INF/MANIFEST.MF文件。关于项目的依赖,它将依赖JAR文件全部解压后,再将得到的.class文件连同当前项目的.class文件一起合并到最终的jar包。
可以通过java -jar app.jar 直接执行jar包

 <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.ManifestResourceTransformer">
              <mainClass>com.hainiubl.App</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>

maven-assembly-plugin

主要用途是制作项目分发包,该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的。
使用一个名为assembly.xml的元数据文件来表述打包,它的single目标可以直接在命令行调用,也可以被绑定至生命周期。
更多使用细节可以参考官方说明http://maven.apache.org/plugins/maven-assembly-plugin/

打zip包

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptors>
                <descriptor>src/main/assemble/assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

assembly.xml 文件:

<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

maven-enforcer-plugin

创建一系列规则强制大家遵守,包括设定Java版本、设定Maven版本、禁止某些依赖、禁止SNAPSHOT依赖等

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <id>enforce</id>
            <phase>validate</phase>
            <goals>
                <goal>display-info</goal>
                <goal>enforce</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--规则检查不通过就构建失败;Default:false. -->
        <!--<failFast>true</failFast>-->
        <rules>
            <requireMavenVersion>
                <version>3.0.4</version>
            </requireMavenVersion>
            <requireJavaVersion>
                <version>1.8.0</version>
            </requireJavaVersion>
            <bannedDependencies>
                <!--是否检查传递性依赖(间接依赖)-->
                <searchTransitive>true</searchTransitive>
                <excludes>
                    <exclude>junit:junit</exclude>
                </excludes>
                <message>use TestNG</message>
            </bannedDependencies>
        </rules>
    </configuration>
</plugin>

配置了三条规则 :

  • requireMavenVersion 表示Maven的版本大于等于3.0.4;
  • requireJavaVersion 表示JDK的版本大于等于1.8.0;
  • bannedDependencies 表示禁止使用的依赖,其可用配置如下:这里面配置的是禁止使用junit

maven-surefire-plugin

maven-surefire-plugin有默认配置,当需要修改一些测试执行的策略时,可以重新配置这个插件。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
    </plugin>

默认策略:

if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1
版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-青牛,http://hainiubl.com/topics/42
本帖由 青牛 于 6年前 解除加精
点赞
成为第一个点赞的人吧 :bowtie:
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter