Maven插件

Maven Available Plugins

背景

  • 在使用maven做spring-boot web应用的时候,默认会将resources目录下的资源都打包到jar包中。但是我的应用里resources目录下有些前端库,不想打包到jar包中,不然jar包会一百多兆,去掉这些前端库就只有十几兆了。这方面的优化对部署运维人员来说省时省力不少,做docker镜像也会轻松很多。于是查找了一下maven的插件,看官方文档就很详细,不明白的地方再搜索就好。

1. Build plugins

  • Build plugins will be executed during the build and they should be configured in the element from the POM.

1.1 Resources Plugin

  • The Resources Plugin handles the copying of project resources to the output directory.
  • 补充几点:
    • directory标签默认将所有该目录下的资源加入
    • 如果同时使用了includes标签就只会加入includes标签里的内容
    • 对于比较复杂的资源加入可以参考:
      • maven的resources介绍
      • 可以通过使用多个resource标签来实现,比如:
      • 下面的代码本来想只用一个resource来实现只包含父目录下指定子目录的内容,但是一个resource不能实现,使用两个resource即可:
<resources>
      <resource>
         <directory>src/main/resources/</directory>
          <filtering>true</filtering>
          <!--<includes>-->
              <!--<include>assets/js/bower_components/bootstrap/dist/</include>-->
              <!--<include>assets/js/bower_components/jquery/dist/</include>-->
          <!--</includes>-->
         <excludes>
             <exclude>assets/js/node_modules/**</exclude>
             <exclude>assets/js/bower_components/**</exclude>
             <exclude>assets/gentelella/vendors/**</exclude>
             <exclude>static/gentelella/**</exclude>
         </excludes>
      </resource>
      <resource>
          <directory>src/main/resources/</directory>
          <filtering>true</filtering>
          <includes>
                <include>assets/js/bower_components/bootstrap/dist/</include>
                <include>assets/js/bower_components/jquery/dist/</include>
          </includes>
      </resource>
  </resources>

2. Surefire

  • The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats:
  • surefire插件是在测试阶段起作用的。可以添加对应的测试框架依赖,来进行测试,生成测试报告。
  • 使用如下:
 <plugin>
    <!--this plugin and dependency jars are used for testing-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <additionalClasspathElements>
            <additionalClasspathElement>${base.dir}/cloud.jar</additionalClasspathElement>
            <additionalClasspathElement>${base.dir}/framwork.jar</additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>

3. 编译插件

  • 使用:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
        <compilerArguments>
            <extdirs>${base.lib.dir}</extdirs>
        </compilerArguments>
    </configuration>
</plugin>

4. jar打包插件

  • 使用:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${base.lib.dir}</outputDirectory>
    </configuration>
</plugin>
posted @ 2017-07-11 18:53  James_飏  阅读(261)  评论(0编辑  收藏  举报