常用Maven 插件總結

jopen 10年前發布 | 17K 次閱讀 Maven 項目構建

一. 打包類

1. Shade Plugin 

用來打可執行的Jar包

示例:

<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/

2. Jar plugin

示例:

<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/

3. portable-config-maven-plugin 

分不同環境進行打包,如測試環境、開發環境

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

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

那么在打包的過程中可以指定替換變量內容,如在測試環境中

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

<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中配置插件

<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

來自:http://my.oschina.net/u/1387007/blog/519581

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!