經常使用Maven 插件總結

一. 打包類

1. Shade Plugin 

用來打可執行的Jar包web

示例:數據庫

<build> 
  <finalName>storm-start-${project.version}</finalName>  
  <plugins> 
    <plugin> 
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-shade-plugin</artifactId>  
      <version>2.4.1</version>  
      <executions> 
        <execution> 
          <phase>package</phase>  
          <goals> 
            <goal>shade</goal> 
          </goals>  
          <configuration> 
            <transformers> 
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
                <manifestEntries> 
                  <Main-Class>sun.storm.start.tutorial.ExclamationTopology</Main-Class>  
                  <X-Compile-Source-JDK>${jdk.version}</X-Compile-Source-JDK>  
                  <X-Compile-Target-JDK>${jdk.version}</X-Compile-Target-JDK> 
                </manifestEntries> 
              </transformer>  
              <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer"> 
                <resource>.txt</resource> 
              </transformer> 
            </transformers>  
            <minimizeJar>true</minimizeJar>  
            <outputDirectory>${project.build.directory}</outputDirectory>  
            <createDependencyReducedPom>true</createDependencyReducedPom>  
            <createSourcesJar>false</createSourcesJar>  
            <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 
  </plugins> 
</build>

shade:shade綁定在package階段,詳細說明參考 http://maven.apache.org/plugins/maven-shade-plugin/apache

2. Jar plugin

示例:app

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>  
  <artifactId>maven-jar-plugin</artifactId>  
  <version>2.6</version>  
  <executions> 
    <execution> 
      <phase>package</phase>  
      <goals> 
        <goal>jar</goal> 
      </goals>  
      <configuration> 
        <classifier>client</classifier>  
        <includes> 
          <include>**/service/*</include> 
        </includes> 
      </configuration> 
    </execution> 
  </executions> 
</plugin>

詳細參考: http://maven.apache.org/plugins/maven-jar-plugin/webapp

3. portable-config-maven-plugin 

分不一樣環境進行打包,如測試環境、開發環境maven

將若有一個數據庫配置文件 src/main/resource/db.properties 以下:ide

database.jdbc.username=dev    database.jdbc.password=dev_pwd

那麼在打包的過程當中能夠指定替換變量內容,如在測試環境中測試

src/main/portable/test.xml 中配置以下內容:ui

<portable-config> 
  <config-file path="WEB-INF/classes/db.properties"> 
    <replace key="database.jdbc.username">test</replace>  
    <replace key="database.jdbc.password">test_pwd</replace> 
  </config-file> 
</portable-config>

最後在maven中配置插件this

<plugin> 
  <groupId>com.juvenxu.portable-config-maven-plugin</groupId>  
  <artifactId>portable-config-maven-plugin</artifactId>  
  <version>1.1.5</version>  
  <executions> 
    <execution> 
      <goals> 
        <goal>replace-package</goal> 
      </goals> 
    </execution> 
  </executions>  
  <configuration> 
    <portableConfig>src/main/portable/test.xml</portableConfig> 
  </configuration> 
</plugin>

或者使用命令行 

mvn clean package -DportableConfig="src/main/portable/test.xml"

二.核心插件

1. resources plugins

    資源插件主要拷貝項目資源文件到輸出目錄,資源文件分爲兩種:main resources 和 test resources,從2.3版本 maven增長了資源過濾的特性( maven filtering)

(1) resources:resources

           copy the resources for the main source code to the main output directory. this goal is bound by default to the process-resources life-cycle phase. It always uses the project.build.resources element to specify the resources, and by default uses the project.build.outputDirectory to specify the copy destination.

    示例:

<build> 
  <resources> 
    <resource> 
      <directory>src/main/resources</directory>  
      <filtering>true</filtering> 
    </resource>  
    <resource> 
      <directory>${project.basedir}/src/main/webapp</directory>  
      <filtering>true</filtering>  
      <includes> 
        <include>**/*.*</include> 
      </includes>  
      <targetPath>${project.build.directory}/webapp</targetPath> 
    </resource> 
  </resources> 
</build>


其中filtering爲true表示開啓資源過濾,什麼意思呢?

例若有一個資源文件 src/main/resources/hello.txt中有如下內容:

Hello ${name}

在pom中定義

<name>world</name>

若是沒有開啓filtering,拷貝後的文件內容爲 Hello ${name},開啓後側爲 Hello world

同時配置資源拷貝是的編碼

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>  
  <artifactId>maven-resources-plugin</artifactId>  
  <version>2.7</version>  
  <configuration> 
    <encoding>UTF-8</encoding> 
  </configuration> 
</plugin>

執行資源拷貝命令:

mvn clean resources:resources

(2)resources:testResources

<testResources> 
  <testResource> 
    <directory>${project.basedir}/src/test/resources</directory> 
  </testResource> 
</testResources>

2. Compliler 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>

3. Deploy Plugin

這裏介紹使用ftp方式部署

<project>... 
  <distributionManagement> 
    <repository> 
      <id>ftp-repository</id>  
      <url>ftp://repository.mycompany.com/repository</url> 
    </repository> 
  </distributionManagement>  
  <build> 
    <extensions> 
      <!-- Enabling the use of FTP -->  
      <extension> 
        <groupId>org.apache.maven.wagon</groupId>  
        <artifactId>wagon-ftp</artifactId>  
        <version>1.0-beta-6</version> 
      </extension> 
    </extensions> 
  </build> ...
</project>

在settings.xml中配置

<settings>... 
  <servers> 
    <server> 
      <id>ftp-repository</id>  
      <username>user</username>  
      <password>pass</password> 
    </server> 
  </servers> ...
</settings>

執行 mvn deploy

相關文章
相關標籤/搜索