常用maven外掛總結-joed
阿新 • • 發佈:2019-02-12
1。官方外掛地址
http://maven.apache.org/plugins/index.html
2。maven常用外掛彙總
2.1 編譯外掛需要配置編碼
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version >
<configuration>
<encoding>UTF8</encoding>
</configuration>
</plugin>
</plugins>
2.2需要在編譯和生成的時候使用不同的jdk版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId >maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.6</source>
<target>1.7</target>
</configuration>
</plugin>
2.3Tomcat外掛
<plugin>
<groupId >org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
2.4 打包外掛maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>**/*.jpg</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
2.5 打包時生成原始碼包外掛maven-source-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
2.6原始碼打包外掛
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- <finalName>${project.build.name}</finalName> -->
<attach>true</attach>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
2.7生成javadoc包外掛
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
2.8 maven-assembly-plugin 它支援各種打包檔案格式,包括zip、tar.gz、tar.bz2等等,通過一個打包描述檔案(該例中是src/main/assembly.xml),它能夠幫助使用者選擇具體打包哪些檔案集合、依賴、模組、和甚至本地倉庫檔案,每個項的具體打包路徑使用者也能自由控制。如下就是對應上述需求的打包描述檔案src/main/assembly.xml
<assembly>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>README.txt</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory>/bin</outputDirectory>
<includes>
<include>run.sh</include>
<include>run.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
最終生成一個zip格式的分發包,它包含如下的一個結構:
bin/
lib/
README.txt
最後,我們需要配置maven-assembly-plugin使用打包描述檔案,並繫結生命週期階段使其自動執行打包操作:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
執行mvn clean package之後,我們就能在target/目錄下得到名為hello-world-1.0-bin.zip的分發包了。
2.9 maven-surefire-plugin 打包時跳過單元測試
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
mvn package -Dmaven.test.skip=true
如果單元測試中有輸出中文,eclipse的控制檯裡中文可能會變成亂碼輸出,也可以通過這個外掛解決,參考配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</plugin>
2.10 maven-resource-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
2.11 把web專案的輸出copy到tomcat的webapp下
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>deploy-website</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${server_home}/webapps/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/${project.build.finalName}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
2.12 maven-dependency-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}為Maven內建變數,預設為target -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!-- 表示是否不包含間接依賴的包 -->
<excludeTransitive>false</excludeTransitive>
<!-- 表示複製的jar檔案去掉版本資訊 -->
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
在部署war包時,需要將專案依賴的jar包,也打到war包中,因此就會用到上述外掛
2.13 在打包時,需要清空一些指定的目錄
在一個J2EE專案中,想使用mvn clean命令清除target裡的內容的同時,也清除tomcat/webapp下的相應目錄,該怎麼辦呢?
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<filesets>
<fileset>
<directory>c:/a/b/c/</directory>
</fileset>
</filesets>
</configuration>
</plugin>
本例中,刪除的是e:/test/zbw/c/目錄.
當用戶在該maven專案中執行mvn clean後,除了刪除clean外掛預設的
project.build.directory
project.build.outputDirectory
project.build.testOutputDirectory
project.reporting.outputDirectory
e:/test/zbw/c/
2.14 利用tomcat-maven-plugin外掛將專案自動打包並部署到tomcat中
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<server>tomcat6-manager</server>
<path>/${project.build.name}</path>
<url>http://localhost:8080/manager</url>
<username>admin</username>
<password>admin</password>
</configuration>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
path: 是指專案部署到tomcat後的專案名稱
url: 是指tomcat的manager訪問地址
server: 這個是tomcat服務名稱設定,需要配置maven的settings.xml檔案,在servers節點中手動配置server,如下所示:
<server>
<id>tomcat6-manager</id>
<username>admin</username>
<password>admin</password>
</server>
2.15 利用cargo-maven2-plugin外掛將專案自動打包並部署到tomcat中
cargo外掛可以幫助你完成WAR包到伺服器的部署及伺服器的啟動和關閉等工作,方便,快速!
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<container>
<containerId>${server_name}</containerId>
<home>${server_home}</home>
</container>
<configuration>
<type>existing</type>
<home>${server_home}</home>
<properties>
<cargo.servlet.port>8088</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
注意,如果你的tomcat伺服器的埠使用的不是預設的8080(如本例中的8088),則需要使用cargo.servlet.port引數將cargo的監聽埠也配置到tomcat的那個監聽埠(如本例中的8088),否則使用mvn cargo:run啟動的伺服器會在120000毫秒(120秒)後自動關閉!
mvn cargo:start命令完成WAR包部署後,啟動伺服器,然後會將伺服器立即關掉;
mvn cargo:run命令完成WAR包部署後,啟動伺服器,直到你Ctrl+C將伺服器關掉;
mvn cargo:stop命令關閉伺服器。
參考:http://cargo.codehaus.org/Maven2+plugin
<plugin>
<!-- 指定外掛名稱及版本號 -->
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.3</version>
<!-- 外掛的Tomcat6.x配置 -->
<configuration>
<!-- 容器的配置 -->
<container>
<!-- 指定伺服器版本 -->
<containerId>tomcat6x</containerId>
<!-- 指定伺服器的安裝目錄 -->
<home>E:\Program Files\tomcat-6.0.32</home>
</container>
<!-- 具體的配置 -->
<configuration>
<!-- 部署模式:existing、standalone等 -->
<type>existing</type>
<!-- Tomcat的位置,即catalina.home -->
<home>E:\Program Files\tomcat-6.0.32</home>
<!-- 配置屬性 -->
<properties>
<!-- 管理地址 -->
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<!-- Tomcat使用者名稱 -->
<cargo.remote.username>admin</cargo.remote.username>
<!-- Tomcat密碼 -->
<cargo.remote.password>admin</cargo.remote.password>
<!-- <cargo.jvmargs> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 </cargo.jvmargs> -->
</properties>
</configuration>
</configuration>
</plugin>