Maven插件

Maven本質上是一個插件框架,它的核心並不執行任何具體的構建任務,所有這些任務都交給插件來完成,像編譯是通過maven-compile-plugin實現的、測試是通過maven-surefire-plugin實現的,maven也內置了很多插件,所以我們在項目進行編譯、測試、打包的過程是沒有感覺到。

概念模型

進一步說,每個任務對應了一個插件目標(goal),每個插件會有一個或者多個目標,例如maven-compiler-plugin的compile目標用來編譯位於src/main/java/目錄下的主源碼,testCompile目標用來編譯位於src/test/java/目錄下的測試源碼。

認識上述Maven插件的基本概念能幫助你理解Maven的工作機制,不過要想更高效率地使用Maven,瞭解一些常用的插件還是很有必要的,這可以幫助你避免一不小心重新發明輪子。多年來Maven社區積累了大量的經驗,並隨之形成了一個成熟的插件生態圈。Maven官方有兩個插件列表,第一個列表的GroupId爲org.apache.maven.plugins,這裏的插件最爲成熟,具體地址爲:http://maven.apache.org/plugins/index.html。第二個列表的GroupId爲org.codehaus.mojo,這裏的插件沒有那麼核心,但也有不少十分有用,其地址爲:http://mojo.codehaus.org/plugins.html

下面列舉了一些常用的核心插件,每個插件的如何配置,官方網站都有詳細的介紹。

一個插件通常提供了一組目標,可使用以下語法來執行:

mvn [plugin-name]:[goal-name]

例如,一個Java項目使用了編譯器插件,通過運行以下命令編譯

mvn compiler:compile

Maven提供以下兩種類型的插件:

l 構建插件

在生成過程中執行,並應在pom.xml中的元素進行配置

l 報告插件

在網站生成期間執行的,應該在pom.xml中的元素進行配置。

這裏僅列舉幾個常用的插件,每個插件的如何進行個性化配置在官網都有詳細的介紹。

<plugins>
      <plugin>
             <!-- 編譯插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.3.2</version>
             <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
             </configuration>
      </plugin>
      <plugin>
             <!-- 發佈插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-deploy-plugin</artifactId>
             <version>2.5</version>
      </plugin>
      <plugin>
             <!-- 打包插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-jar-plugin</artifactId>
             <version>2.3.1</version>
      </plugin>
      <plugin>
             <!-- 安裝插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-install-plugin</artifactId>
             <version>2.3.1</version>
      </plugin>
      <plugin>
             <!-- 單元測試插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>2.7.2</version>
             <configuration>
                    <skip>true</skip>
             </configuration>
      </plugin>
      <plugin>
             <!-- 源碼插件 -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-source-plugin</artifactId>
             <version>2.1</version>
             <!-- 發佈時自動將源碼同時發佈的配置 -->
             <executions>
                <execution>
                       <id>attach-sources</id>
                             <goals>
                                   <goal>jar</goal>
                            </goals>
                      </execution>
                 </executions>
      </plugin>
</plugins>

除了這些核心插件之外,還有很多優秀的第三方插件,可以幫助我們快捷、方便的構架項目。當使用到某些功能或者特性的時候多加搜索,往往得到讓你驚喜的效果。

例如,項目中使用了Mybatis,就有一款神奇的maven插件,運行一個命令,就可以根據數據庫的表,自動生成Mybatis的mapper配置文件以及DAO層接口模板。

在pom.xml中添加plugin:

<plugin>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
       <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
           <id>Generate MyBatis Artifacts</id>
            <goals>
               <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
           <groupId>org.mybatis.generator</groupId>
           <artifactId>mybatis-generator-core</artifactId>
           <version>1.3.2</version>
        </dependency>
    </dependencies>
</plugin>

定義generatorConfig.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <classPathEntry  location="/Users/winner/mysql/mysql-connector-java-5.1.36.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <!-- 去掉自動生成的註解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        < jdbcConnection driverClass="com.mysql.jdbc.Driver"  connectionURL="jdbc:mysql://localhost:3344/db?characterEncoding=utf8" userId="id" password="password">
        </jdbcConnection>


        <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer true,把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成映射類-->
        <javaModelGenerator targetPackage="com.clf.model"  targetProject="/Users/winner/Documents/workspace/project/src/main/java/">
            <!-- enableSubPackages:是否讓schema作爲包的後綴 -->
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成xml文件-->
        <sqlMapGenerator targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/resources/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mapperinterface-->
        <javaClientGenerator type="XMLMAPPER"  targetPackage="com.clf.mapper"  targetProject="/Users/winner/Documents/workspace/project/src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaClientGenerator>

             <table tableName="table_name" domainObjectName="object_name"
              enableCountByExample="false" enableUpdateByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false"
              selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

然後定位到pom.xml所在的路徑下面,運行:

mvn mybatis-generator:generate

所有的文件就會自動生成,怎一個爽字了得。

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