Maven编译系列(一)——Plugin

做java开发的肯定对maven再熟悉不过了,可是我之前对maven的了解却也仅限于拷贝pom模版,然后添加自己的dependecy而已,顶多加上build和repository这两个参数来指定java版本和maven源,最后编译这块儿还是靠IDEA的编译功能来打jar包,对于怎么用maven编译这块儿实在不了解,碰到有些需要maven编译的项目就无从下手,这才痛下决心找个时间彻底研究一下maven。先从开源项目中占比最大的plugin这部分学起吧。

要想知道plugin的用法,还是得首先了解maven的生命周期,maven的生命周期如上图所示,看起来是maven帮我们实现了打包等逻辑,但事实上,maven自身的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成。所以Maven本质上是一个插件框架。每个生命周期包含了多个步骤(phase),而 goal 则是绑定到 phase 上的,每一个 phase 都对应 1 个或多个 goal。

这里注意一点就是在一个生命周期中,运行某个阶段的时候,它之前的所有阶段都会被运行。

下面我们以开源项目maxwell的pom(https://github.com/zendesk/maxwell/blob/master/pom.xml)文件中的plugin入手,学习一下常用的plugin的用法把

   <plugins>
      <!-- horrible sonatype plugins -->
      <plugin>
        <groupId>org.sonatype.plugins</groupId>
        <artifactId>nexus-staging-maven-plugin</artifactId>
        <!--是一个nexus用来自动控制流程的客户端插件。如果你想分享自己的java开源项目到maven中央仓库,就有可能需要这个插件。-->
        <version>1.6.6</version>
        <extensions>true</extensions>
        <configuration>
          <serverId>ossrh</serverId>
          <nexusUrl>https://oss.sonatype.org/</nexusUrl>
          <autoReleaseAfterClose>true</autoReleaseAfterClose>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <!--maven-gpg-plugin是对maven构件进行签名的插件,使用的是GnuPG-->
        <version>1.5</version>
        <executions>
          <execution>
            <id>sign-artifacts</id>
            <phase>verify</phase>
            <goals>
              <goal>sign</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <!--该插件的作用是将源码也单独打成一个jar包,默认情况下,这个jar包会在target目录生成。-->
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <!-- 将main中的代码打包到jar 包中,因为它不会fork build,所以适合放在build 这个阶段,默认是绑在 package阶段-->
        <version>2.9.1</version>
        <configuration>
            <source>8</source>
        </configuration>
        <executions>
          <execution>
            <id>attach-javadocs</id>
            <goals>
              <!--用标准javadoc Tool将非聚合项目的main代码打包到一个jar包里-->
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <!--编译代码的插件,默认使用的是javax.tools.JavaCompiler,想强制该插件使用javac,可以通过配置forceJavacCompilerUse这个配置-->
        <version>3.6.2</version>
        <configuration>
          <!--配置jdk版本-->
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <!--Antlr 是一个基于 Java 开发的功能强大的语言识别工具,这个插件的作用是告诉标准ANTLR解析器生成器输入语法文件在哪里,默认是src/main/antlr4,输出文件应该在哪里生成,默认是target/generated-sources/antlr4-->
        <version>4.5</version>
        <executions>
          <execution>
            <id>antlr</id>
            <goals>
              <goal>antlr4</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <!--很实用的工具,用来指导maven怎么生成打包后的release包结构,我们看到的开源项目里的release包里有bin,有lib,有conf目录,都是这个插件的功能-->
        <version>3.1.0</version>
        <configuration>
          <!--将assembly id 从最终的assembly名中排除>
          <appendAssemblyId>false</appendAssemblyId>
          <!--设置遇到文件名过长(超过100字符)的时候,打包工具的行为,支持warn(默认),fail,truncate,gnu,posix,posix_warn,omit-->
          <tarLongFileMode>posix</tarLongFileMode>
          <descriptors>
            <!--指定release包生成方式的配置xml位置-->
            <!--formats,打包格式,支持dir,zip,tar,tar.gz等等-->
            <!--fileSets 要包含的文件夹,里面可以配置include-->
            <!--files 要打包的单个文件>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <!--为项目生成一个jar包,包含资源文件-->
        <version>3.0.2</version>
        <configuration>
          <!--archive配置,参考http://maven.apache.org/shared/maven-archiver/index.html#class_manifest-->
          <archive>
            <manifest>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <!--依赖管理方式,可以将依赖打包从local或者remote拷贝到指定位置-->
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <!--将依赖拷贝到指定位置-->
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <!--输出目录-->
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <!--要排除的Artifact-->
              <excludeArtifactIds>kafka-clients</excludeArtifactIds>
              <includeScope>runtime</includeScope>
            </configuration>
          </execution>
          <execution>
            <id>kafka-clients</id>
            <phase>compile</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib/kafka-clients</outputDirectory>
              <includeArtifactIds>kafka-clients</includeArtifactIds>
            </configuration>
          </execution>
        </executions>
      </plugin>

授人以鱼不如授人以渔,学习maven插件的最好的办法还是去官网看,http://maven.apache.org/plugins/index.html,这个地方有所有官方的插件列表,需要查询什么的时候,点进插件详情页,再点击Goals,就可以看到每一个Goal的功能介绍,配置,还又实例。最后还是要多多实践,不能一直只依赖idea或者eclipse提供的打包工具,毕竟,程序员的世界,命令行才是最高效的工具,大家加油。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章