成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

常用maven插件總結

開發(fā) 開發(fā)工具
maven是目前java項目中比較流行的構建工具,特別是它提供的插件,如果使用得當,整個項目研發(fā)流程都將會受益,從而提高研發(fā)、測試和部署的效率。這里記錄幾個常用插件的用法,既方便自己日后回顧,也便于有興趣的同仁交流指正。

[[186941]]

maven是目前java項目中比較流行的構建工具,特別是它提供的插件,如果使用得當,整個項目研發(fā)流程都將會受益,從而提高研發(fā)、測試和部署的效率。這里記錄幾個常用插件的用法,既方便自己日后回顧,也便于有興趣的同仁交流指正。各位實踐過程中如果發(fā)現(xiàn)有趣的插件或者更好的用法,也歡迎留言分享。

Maven工程標準架構

插件一 maven-resources-plugin

Maven可以區(qū)別對待Java代碼文件和資源文件,默認的主資源文件目錄是src/main/resources,我們可以通過這個插件實現(xiàn)資源文件過濾。資源文件過濾的意思是指我們可以在資源文件里用使用占位符${propertyName},然后開啟對資源文件的過濾,pom.xml里再統(tǒng)一設置所有{propertyName}對應的值,就可以在構建過程中將值替換掉資源文件中對應的${propertyName},實現(xiàn)了代碼配置分離、做到了參數(shù)的統(tǒng)一維護。

示例用法

  1. <resources> 
  2.     <resource> 
  3.         <directory>src/main/resources</directory> 
  4.         <includes> 
  5.             <include>properties/*.properties</include> 
  6.         </includes> 
  7.         <filtering>true</filtering> 
  8.     </resource> 
  9.     <resource> 
  10.         <directory>src/main/resources</directory> 
  11.         <includes> 
  12.             <include>*.xml</include> 
  13.             <include>mapper/**/*.xml</include> 
  14.             <include>mysqlMapper/**/*.xml</include> 
  15.             <include>*.properties</include> 
  16.         </includes> 
  17.     </resource> 
  18. </resources> 
  19. …… 
  20. <properties> 
  21.     <runtime.env>local</runtime.env> 
  22. </properties> 

我們的主應用集成后,會根據(jù)實際要求部署到不同的環(huán)境中,比如聯(lián)調(diào)環(huán)境、測試環(huán)境、壓力環(huán)境、預發(fā)布環(huán)境、生產(chǎn)環(huán)境等,而這些環(huán)境上的資源配置信息顯然是不一樣的,針對每套環(huán)境,每個具體占位符${propertyName}都會有不同的值,而這種場景可以使用Maven的profile來支持,每個profile都可以獨立維護一套參數(shù)值,在mvn package的時候靈活指定;此外,maven也支持在package的時候指定多個profile,這個特性在執(zhí)行自動部署的時候特別有用。使用這個插件,我們的項目可以做到多環(huán)境支持,參考命令

  1. mvn package -Pnocheck,env-test 

示例用法

  1. <profiles> 
  2.     <profile> 
  3.         <id>nocheck</id> 
  4.         <properties> 
  5.             <skipTests>true</skipTests> 
  6.             <checkstyle.skip>true</checkstyle.skip> 
  7.             <license.skip>true</license.skip> 
  8.             <notice.skip>true</notice.skip> 
  9.             <versions.skip>true</versions.skip> 
  10.         </properties> 
  11.     </profile> 
  12.     <profile> 
  13.         <!-- 本地環(huán)境,默認是windows --> 
  14.         <id>local</id> 
  15.         <activation> 
  16.             <activeByDefault>true</activeByDefault> 
  17.         </activation> 
  18.         <properties> 
  19.             <runtime.env>local</runtime.env> 
  20.         </properties> 
  21.     </profile> 
  22.     <profile> 
  23.         <id>env-test</id> 
  24.         <properties> 
  25.             <runtime.env>env-test</runtime.env> 
  26.         </properties> 
  27.     </profile> 
  28. </profiles> 

插件二 maven-jar-plugin

當我們將項目模塊化后,有一些通用的資源文件基本上大多數(shù)模塊都會用到,比如log4j.properties,jdbc.properties等,模塊中有了這些資源文件,我們才能單獨對該模塊進行開發(fā)、調(diào)試。默認情況下maven-jar-plugin會將這些資源文件全部package成一個jar包進行發(fā)布,如果這樣的jar包集成到一個主應用中部署,運行,很可能導致主應用的配置不生效,我稱之為配置混亂,為了解決這個問題,可以開啟maven-jar-plugin的排除功能,在執(zhí)行mvn package之前排除指定的資源文件。

示例用法

  1. <plugin> 
  2.     <groupId>org.apache.maven.plugins</groupId> 
  3.     <artifactId>maven-jar-plugin</artifactId> 
  4.     <configuration> 
  5.         <excludes> 
  6.             <exclude>applicationContext.xml</exclude> 
  7.             <exclude>properties/**</exclude> 
  8.             <exclude>log4j.properties</exclude> 
  9.         </excludes> 
  10.     </configuration> 
  11. </plugin> 

插件三 maven-war-plugin

項目如果是web主應用,我們可以使用maven-war-plugin來對webapps下各類文件進行過濾。用法參考maven-resources-plugin

示例用法

  1. <plugin> 
  2.     <groupId>org.apache.maven.plugins</groupId> 
  3.     <artifactId>maven-war-plugin</artifactId> 
  4.     <configuration> 
  5.         <warName>demo-Rest</warName> 
  6.         <webResources> 
  7.             <resource> 
  8.                 <directory>src/main/webapp/WEB-INF</directory> 
  9.                 <filtering>true</filtering> 
  10.                 <targetPath>WEB-INF</targetPath> 
  11.                 <includes> 
  12.                     <include>web.xml</include> 
  13.                 </includes> 
  14.             </resource> 
  15.         </webResources> 
  16.     </configuration> 
  17. </plugin> 

插件四 properties-maven-plugin

隨著項目的不斷迭代,我們的資源配置項將會變得更多,這個會直接影響到pom.xml的體積膨脹;此外,如果項目目標部署環(huán)境比較多,pom.xml將會膨脹得更快,更加難以維護。為了解決這個問題,我們需要將這些配置信息獨立出來,并按照不同環(huán)境進行歸類,使用properties-maven-plugin就會達到這個效果。

示例用法(將每個環(huán)境的信息放在不同的目錄下,然后在mvn package切換不同的profile實現(xiàn)去指定目錄讀取配置信息,用讀取到的value去替換資源配置文件的占位符)

  1. <plugin> 
  2.     <groupId>org.codehaus.mojo</groupId> 
  3.     <artifactId>properties-maven-plugin</artifactId> 
  4.     <version>1.0.0</version> 
  5.     <configuration> 
  6.         <files> 
  7.             <file>profiles/${runtime.env}/jdbc.properties</file> 
  8.             <file>profiles/${runtime.env}/redis.properties</file> 
  9.             <file>profiles/${runtime.env}/batch.properties</file> 
  10.             <file>profiles/${runtime.env}/config.properties</file> 
  11.         </files> 
  12.     </configuration> 
  13.     <executions> 
  14.         <execution> 
  15.             <phase>initialize</phase> 
  16.             <goals> 
  17.                 <goal>read-project-properties</goal> 
  18.             </goals> 
  19.         </execution> 
  20.     </executions> 
  21. </plugin> 

插件五 maven-assembly-plugin

Java項目中有一種類型的主應用,是需要獨立部署在后臺啟動的,比如socket服務程序,比如定時調(diào)度程序,比如dubbo服務程序,這些程序理論上只需要執(zhí)行一個簡單的java命令即可;稍微復雜一些的,我們可以規(guī)范一下自己的主應用結構,定義配置文件夾和依賴庫文件夾,再準備啟動的批處理腳本sh或bat文件即可。使用maven-assembly-plugin就可以達到這種效果。

示例用法

  1. <plugin> 
  2.     <groupId>org.apache.maven.plugins</groupId> 
  3.     <artifactId>maven-assembly-plugin</artifactId> 
  4.     <configuration> 
  5.         <appendAssemblyId>false</appendAssemblyId> 
  6.         <descriptors> 
  7.             <descriptor>target/classes/package.xml</descriptor> 
  8.         </descriptors> 
  9.     </configuration> 
  10.     <executions> 
  11.         <execution> 
  12.             <id>make-assembly</id> 
  13.             <phase>package</phase> 
  14.             <goals> 
  15.                 <goal>single</goal> 
  16.             </goals> 
  17.         </execution> 
  18.     </executions> 
  19. </plugin> 

附package.xml

  1. <assembly 
  2. xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"
  5.     <id>package</id> 
  6.     <formats> 
  7.         <format>tar.gz</format> 
  8.     </formats> 
  9.     <includeBaseDirectory>true</includeBaseDirectory> 
  10.     <fileSets> 
  11.         <fileSet> 
  12.             <directory>src/main/bin</directory> 
  13.             <includes> 
  14.                 <include>*.sh</include> 
  15.                 <include>*.bat</include> 
  16.             </includes> 
  17.             <filtered>true</filtered> 
  18.             <outputDirectory></outputDirectory> 
  19.             <fileMode>0755</fileMode> 
  20.         </fileSet> 
  21.         <fileSet> 
  22.             <directory>${basedir}/src/main/config</directory> 
  23.             <includes> 
  24.                 <include>*.properties</include> 
  25.                 <include>log4j.xml</include> 
  26.             </includes> 
  27.             <outputDirectory>config</outputDirectory> 
  28.             <filtered>true</filtered> 
  29.             <fileMode>0644</fileMode> 
  30.         </fileSet> 
  31.         <fileSet> 
  32.             <directory>${basedir}/src/main/config</directory> 
  33.             <includes> 
  34.                 <include>log4j.dtd</include> 
  35.             </includes> 
  36.             <outputDirectory>config</outputDirectory> 
  37.             <fileMode>0644</fileMode> 
  38.         </fileSet> 
  39.     </fileSets> 
  40.     <dependencySets> 
  41.         <dependencySet> 
  42.             <outputDirectory>lib</outputDirectory> 
  43.             <scope>runtime</scope> 
  44.             <fileMode>0644</fileMode> 
  45.         </dependencySet> 
  46.     </dependencySets> 
  47. </assembly> 

附示例生成的Java應用結構圖

插件六 maven-shade-plugin

有時候,我們需要將所有配置文件和依賴庫文件全部放在一個jar包中,運維的同事只需要執(zhí)行java -jar batch.jar即可完成啟動。雖然使用maven-assembly-plugin也可以做到這一點,但是在讀取配置文件的時候有可能會遇到一些問題,這個時候,我們可能需要使用到maven-shade-plugin這個插件,經(jīng)筆者實踐按照如下示例用法配置確實可用;當然本示例配置了mainClass,直接執(zhí)行java -jar batch.jar確實沒問題,但如果執(zhí)行java com.fastjrun.demospring4.BatchInit -classpath batch.jar也是可以的。

示例用法

  1. <plugin> 
  2.     <groupId>org.apache.maven.plugins</groupId> 
  3.     <artifactId>maven-shade-plugin</artifactId> 
  4.     <version>3.0.0</version> 
  5.     <executions> 
  6.         <execution> 
  7.             <phase>package</phase> 
  8.             <goals> 
  9.                 <goal>shade</goal> 
  10.             </goals> 
  11.             <configuration> 
  12.                 <finalName>batch</finalName> 
  13.                 <shadedArtifactAttached>true</shadedArtifactAttached> 
  14.                 <shadedClassifierName>jar-with-dependencies</shadedClassifierName> 
  15.                 <transformers> 
  16.                     <transformer 
  17.                         implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"
  18.                         <mainClass>com.fastjrun.demospring4.BatchInit</mainClass> 
  19.                     </transformer> 
  20.                     <transformer 
  21.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"
  22.                         <resource>META-INF/spring.handlers</resource> 
  23.                     </transformer> 
  24.                     <transformer 
  25.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"
  26.                         <resource>META-INF/spring.schemas</resource> 
  27.                     </transformer> 
  28.                     <transformer 
  29.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"
  30.                         <resource>META-INF/spring.tooling</resource> 
  31.                     </transformer> 
  32.                 </transformers> 
  33.                 <filters> 
  34.                     <filter> 
  35.                         <artifact>*:*</artifact> 
  36.                         <excludes> 
  37.                             <exclude>META-INF/*.SF</exclude> 
  38.                             <exclude>META-INF/*.DSA</exclude> 
  39.                             <exclude>META-INF/*.RSA</exclude> 
  40.                         </excludes> 
  41.                     </filter> 
  42.                 </filters> 
  43.             </configuration> 
  44.         </execution> 
  45.     </executions> 
  46. </plugin> 

插件七 versions-maven-plugin

當項目模塊化后,我們會遇到一個問題,就是項目版本升級的時候,需要同時變更父模塊和所有子模塊中的版本號 ,而這是一個比較瑣碎且容易出錯的事情,還好maven考慮得很周到,提供了這樣一個插件,我們使用命令行就可以達到效果了。我們的項目視圖如下


 

參考命令如下

  1. mvn versions:set -DnewVersion=1.2-SNAPSHOT 

總結

本文匯總了筆者常用的幾個插件及其用法,經(jīng)實踐,基于eclipse的kepler、luna版本都能很好支持maven-resources-plugin、maven-jar-plugin、maven-war-plugin和properties-maven-plugin使用,同時也支持profile的activeByDefault設置,研發(fā)同事在不需要任何調(diào)整的情況下就能直接開發(fā)、調(diào)試代碼,且在開發(fā)結束后,可以直接使用mvn命令打包,打出各個環(huán)境的部署程序。從開發(fā)、調(diào)試、測試、驗證到上線的整個過程,所有模塊的pom.xml直到下一個迭代變更版本前都不用修改,直接使用。

【本文為51CTO專欄作者“崔瑩峰”的原創(chuàng)稿件,轉(zhuǎn)載請聯(lián)系原作者】

戳這里,看該作者更多好文

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2011-01-19 13:04:42

Thunderbird插件

2020-06-04 10:49:53

Pandas字符串技巧

2020-09-24 10:00:50

SpringBoo

2019-09-17 16:30:18

java排序算法

2014-11-24 09:49:33

Eclipse

2010-09-06 13:59:23

CSS縮寫

2017-03-02 14:35:33

Androidgradle常用配置

2009-12-08 18:11:42

PHP系統(tǒng)常量

2021-01-13 10:28:16

Maven插件Mojo

2023-09-16 18:40:43

Java插件

2023-09-18 08:27:44

插件Java

2011-06-10 15:00:02

Qt VC

2021-07-28 06:51:09

linux

2025-05-23 10:38:43

2009-12-02 20:29:30

PHP常用函數(shù)

2010-02-25 14:39:03

WCF術語

2009-09-09 13:31:15

C# TextBox

2016-10-20 20:21:09

Python爬蟲技巧

2011-05-07 14:39:00

投影

2024-01-15 08:16:10

Maven插件簡化
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲狠狠 | 国产亚洲成av人片在线观看桃 | 久久精品中文字幕 | 中文成人无字幕乱码精品 | 粉嫩av久久一区二区三区 | 综合激情av| 91精品在线观看入口 | av久久| 久久久精 | 国产精品久久国产愉拍 | 国产精品一区二区不卡 | 亚洲bt 欧美bt 日本bt | 国产ts一区 | 国产欧美日韩综合精品一 | 欧美日韩一区二区在线观看 | 人妖一区| 亚洲精品无 | 成人精品国产 | av在线伊人 | 日本高清不卡视频 | 亚洲第一av| 久久精品中文 | 国产91综合 | 国产成人免费视频网站高清观看视频 | 欧美日韩一区二区在线 | 97人澡人人添人人爽欧美 | 精品一区二区三区在线观看国产 | 国产欧美一区二区三区久久人妖 | 一区二区视频在线 | 日韩欧美久久精品 | 欧美精品乱码久久久久久按摩 | 国产黄色精品在线观看 | 九九久久久| 国产1区| 国产欧美精品一区二区色综合朱莉 | 成人久久18免费 | 成人区一区二区三区 | 精品国产伦一区二区三区观看方式 | 亚洲精品区 | 国产精品色 | 国产精品一区二区不卡 |