Maven學習之 插件plugin

Maven本質上是一個執行插件的框架。插件共分兩類:build插件和reporting插件。html

  • build插件,會在build階段被執行,應該配置在POM的<build/>元素中。
  • reporting插件,生成站點的時候會執行,應該配置在POM的<reporting/>元素中。由於reporting插件的結果是生成的站點的一部分,因此這種插件應該是國際化和本地化的。此處更多詳見 http://maven.apache.org/plugins/localization.html

但全部的插件至少都須要指明:groupId、artifactId、version。java

 

通用配置web

Maven插件都是經過指定一個<configuration>元素來配置的。而該元素中的子元素,都是Mojo中的property。正則表達式

詳見 http://maven.apache.org/guides/mini/guide-configuring-plugins.htmlapache

配置build插件api

下面僅僅是在<build>元素中配置build插件。tomcat

使用<executions>標籤安全

 

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<此處未完待續>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>服務器

 

 

 

Maven項目支持的插件session

org/apache/maven/plugins中有最新的插件列表。

核心插件

clean  負責build以後的清理工做

compiler 編譯Java源碼

deploy 將構建好的artifact部署到遠程倉庫

failsafe 在隔離的類加載器中運行JUnit集成測試

install 將構建好的artifact部署到本地倉庫

resources 將資源複製到輸出文件夾,以包含進JAR中。

site 爲當前項目生成一個站點

surefire 在一個隔離的類加載器中運行JUnit單元測試

verifier 對集成測試有用 -- 校驗特定環境的存在性

負責打包的類型/工具

ear 爲當前項目生成一個EAR

jar 從當前項目構建一個JAR

rar 從當前項目構建一個RAR

war 從當前項目構建一個WAR

shade 從當前項目構建一個Uber_JAR,包含依賴。(也叫fat jar或super jar

source 從當前項目構建一個源碼JAR

Maven大陸以外的插件:codehaus.org, code.google.com, misc

misc,其餘的縮寫。主要是指由各個項目提供的Maven插件,這裏僅列出兩個:

jetty

Apache Tomcat

 

下面用一些例子來講明使用的方法

1、WAR插件,有4種方式來使用它:

  1. 將項目package成war類型
  2. 調用 war:war goal
  3. 調用 war:exploded goal
  4. 調用 war:inplace goal

注意:當使用 war: goals時,它會假定compile階段已經完成。

<project>
    ...
    <build>
        <!-- 在parent POM中定義版本 -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                ...
            </plugins>
        </pluginManagement>
        <!-- 在POM 或 parent POM中使用插件 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>
<project>
      ...
      <groupId>com.example.projects</groupId>
      <artifactId>documentedproject</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>Documented Project</name>
      <url>http://example.com</url>
      ...
</project>
  • 調用 mvn package 或者 mvn compile war:war,會在 target/ 下面生產相應版本的war文件(文件名是:artifactId-version.war)。
  • 調用 mvn compile war:exploded 則會在 target/ 下面生成非打包的war文件夾。該文件夾的名字是finalName,而finalName一般是artifactId-version形式。固然,能夠經過制定 webappDirectory參數來覆蓋默認。
<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <webappDirectory>/sample/servlet/container/deploy/directory
                    </webappDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>
  • 調用 mvn compile war:inplace,則會在 src/main/webapp/  下面建立未打包的war文件夾

 

小結:4種調用方式,但實際效果是3種,區別在於最終建立的是文件仍是文件夾、在什麼地方建立(target/, src/main/webapp/)。

 

添加和過濾外部Web自由

Maven項目默認的資源文件夾是 src/main/resources,最終會出如今WAR文件的target/classes 和 WEB-INF/classes中。

WAR插件可使用webResources參數來包含默認資源文件夾之外的資源。

添加web資源

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <webResources>
                        <resource>
                            <!-- this is relative to the pom.xml directory -->
                            <directory>resource2</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

咱們的示例項目帶有額外的外部資源,其結構以下:

  
  
  
  
  1. .
  2. |-- pom.xml
  3. |-- resource2
  4. | |-- external-resource.jpg
  5. | `-- image2
  6. | `-- external-resource2.jpg
  7. `-- src
  8. `-- main
  9. |-- java
  10. | `-- com
  11. | `-- example
  12. | `-- projects
  13. | `-- SampleAction.java
  14. |-- resources
  15. | `-- images
  16. | `-- sampleimage.jpg
  17. `-- webapp
  18. |-- WEB-INF
  19. | `-- web.xml
  20. |-- index.jsp
  21. `-- jsp
  22. `-- websource.jsp

最終在WAR中的結構以下:

  
  
  
  
  1. documentedproject-1.0-SNAPSHOT.war
  2. |-- META-INF
  3. | |-- MANIFEST.MF
  4. | `-- maven
  5. | `-- com.example.projects
  6. | `-- documentedproject
  7. | |-- pom.properties
  8. | `-- pom.xml
  9. |-- WEB-INF
  10. | |-- classes
  11. | | |-- com
  12. | | | `-- example
  13. | | | `-- projects
  14. | | | `-- SampleAction.class
  15. | | `-- images
  16. | | `-- sampleimage.jpg
  17. | `-- web.xml
  18. |-- external-resource.jpg
  19. |-- image2
  20. | `-- external-resource2.jpg
  21. |-- index.jsp
  22. `-- jsp
  23. `-- websource.jsp

注意: external-resource2.jpg 和 image2 都被複制到WAR的root目錄下,保持了目錄結構。

配置web資源

webResources 是一組資源列表。支持資源的全部選項。

web資源:

  • 可以被 includes/excludes
  • 可以被 filtered
  • 不限於默認位置 -- root of WAR

includes/excludes

想要在WAR中包含全部jpgs,能夠這樣設置POM:

...
<configuration>
    <webResources>
        <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>resource2</directory>
            <!-- the list has a default value of ** -->
            <includes>
                <include>**/*.jpg</include>
            </includes>
        </resource>
    </webResources>
</configuration>
...

想要在WAR中排除image2目錄:

...
<configuration>
    <webResources>
        <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>resource2</directory>
            <!-- there's no default value for this -->
            <excludes>
                <exclude>**/image2</exclude>
            </excludes>
        </resource>
    </webResources>
</configuration>
...

當混合使用includes和excludes時,要當心,excludes的優先級更高

 

 

 

 

 

 

 

 

從WAR中包含或排除文件

經過 <packagingIncludes> 或 <packagingExcludes> 配置參數來實現。它們均可以接收一個逗號間隔的Ant風格文件列表。

就是說,使用 ** 來表明多級目錄,使用 * 來表明文件或目錄名字的一部分。

例如,排除全部WEB-INF/lib 下的JAR文件:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

有時候,僅使用這種通配符是不夠的。這時你可使用正則表達式,%regex[]。

下面的例子中,咱們想排除全部commons-logging和log4j JARs,但不想排除log4j-over-slf4j JAR。

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <!-- Exclude JCL and LOG4J since all logging should go through SLF4J. 
                        Note that we're excluding log4j-<version>.jar but keeping log4j-over-slf4j-<version>.jar -->
                    <packagingExcludes>
                        WEB-INF/lib/commons-logging-*.jar,
                        %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
                    </packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

 

 

 

 

 

 

2、Tomcat Maven插件

注意,以前該插件是位於Codehaus上面,惋惜那個網站已經over了。

如今在這裏:https://tomcat.apache.org/maven-plugin-2.2/

 

2.2 版本新功能:

  • 支持Tomcat 7
  • 能夠build executable WAR/JAR了

 

該插件提供了用於在Tomcat中操做WAR項目、或者使用一個內置Tomcat來運行你的WAR項目的goals。這些goals可讓你快速開發你的應用,而沒必要單獨安裝一個Tomcat實例。

關於groupId 和Mojo name的變動

自版本 2.0-beta-1 起,tomcat mojos已經被重命名爲tomcat6 和 tomcat7,兩者goals相同。

因此,你必須這樣使用它們:

在POM中:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat6-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
    </plugins>
</pluginManagement>

或者在settings.xml中:

<pluginGroups>
    ....
    <pluginGroup>org.apache.tomcat.maven</pluginGroup>
    ....
</pluginGroups>

 

goals概覽

context goalscontainer goalsbuild an executable WAR/JAR

context goals

該插件提供了能夠在項目的context上執行不一樣任務的goals -- 只要項目已經被部署到Tomcat中。以下:

  1. 從新部署一個WAR項目

      從新部署一個WAR項目取決於該項目部署的方式:

      • 當項目是使用tomcat:deploy 部署的WAR項目時,可使用 mvn package tomcat6/7:redeploy
      • 當項目是使用tomcat:exploded部署的WAR項目時,可使用 mvn war:exploded tomcat6/7:redeploy
      • 當項目是使用tomcat:inplace部署的WAR項目時,可使用 mvn war:inplace tomcat6/7:redeploy
      • 想要從新部署使用tomcat:deploy部署的一個context.xml文件時,可使用 mvn tomcat6/7:redeploy

      注意:取決於context.xml中指定的docBase,可能也會須要像上面那樣調用 war:exploded 或者 war:inplace。

  2. 取消部署一個WAR項目
      mvn tomcat6/7:undeploy
  3. 啓動一個WAR項目
      mvn tomcat6:start
  4. 中止一個WAR項目
      mvn tomcat6:stop
  5. 列出會話統計
      mvn tomcat6:sessions

container goals

該插件還提供了獲取Tomcat容器各類信息的goals:

  1. 列出被部署的全部項目
      mvn tomcat6:list
  2. 列出服務器信息
      mvn tomcat6:info
  3. 列出JNDI資源
      mvn tomcat6:resources   --  列出全部Tomcat內可用的JNDI資源
      mvn -Dmaven.tomcat.type=my.class.name tomcat6:resources  --  列出特定類型的JNDI資源
  4. 列出安全角色
      mvn tomcat6:roles

 

build an executable WAR/JAR

從 2.0 版本開始,可用使用一個內置的Tomcat7來build一個可執行的WAR/JAR。

僅tomcat7 插件支持!

注意:你的項目的<packaging/>的值必須是pom或war。

須要添加至war模塊的artifact

<project>
    ...
    <packaging>war or pom</packaging>
    ...
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>tomcat-run</id>
                        <goals>
                            <goal>exec-war-only</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <path>foo</path>
                            <!-- optional, needed only if you want to use a preconfigured server.xml 
                                file -->
                            <serverXml>src/main/tomcatconf/server.xml</serverXml>
                            <!-- optional values which can be configurable -->
                            <attachArtifactClassifier>default value is exec-war but you can
                                customize</attachArtifactClassifier>
                            <attachArtifactClassifierType>default value is jar</attachArtifactClassifierType>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
        ...
    </build>
    ...
</project>

須要添加至你的pom模塊的artifact:

<project>
    ...
    <packaging>war</packaging>
    ...
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>tomcat-run</id>
                        <goals>
                            <goal>exec-war-only</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <!-- optional only if you want to use a preconfigured server.xml file -->
                            <!-- <serverXml>src/main/tomcatconf/server.xml</serverXml> -->
                            <warRunDependencies>
                                <warRunDependency>
                                    <dependency>
                                        <groupId>a groupId</groupId>
                                        <artifactId>and artifactId</artifactId>
                                        <version>version</version>
                                        <type>war</type>
                                    </dependency>
                                    <contextPath>/</contextPath>
                                </warRunDependency>
                            </warRunDependencies>
                            <!-- naming is disabled by default so use true to enable it -->
                            <enableNaming>true</enableNaming>
                            <!-- extra dependencies to add jdbc driver, mail jars, etc. -->
                            <extraDependencies>
                                <extraDependency>
                                    <groupId>org.apache.derby</groupId>
                                    <artifactId>derby</artifactId>
                                    <version>10.1.3.1</version>
                                </extraDependency>
                                <extraDependency>
                                    <groupId>javax.mail</groupId>
                                    <artifactId>mail</artifactId>
                                    <version>1.4</version>
                                </extraDependency>
                            </extraDependencies>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
        ...
    </build>
    ...
</project>

生成的executable jar/war

經過上面的配置,你能夠執行該生成的jar (因其內置了Tomcat容器):

     java -jar yourjar

輔助輸出的參數:

usage: java -jar [path to your exec war jar]
 -ajpPort <ajpPort>                     ajp port to use
 -clientAuth                            enable client authentication for
                                        https
 -D <arg>                               key=value
 -extractDirectory <extractDirectory>   path to extract war content,
                                        default value: .extract
 -h,--help                              help
 -httpPort <httpPort>                   http port to use
 -httpProtocol <httpProtocol>           http protocol to use: HTTP/1.1 or
                                        org.apache.coyote.http11.Http11Nio
                                        Protocol
 -httpsPort <httpsPort>                 https port to use
 -keyAlias <keyAlias>                   alias from keystore for ssl
 -loggerName <loggerName>               logger to use: slf4j to use slf4j
                                        bridge on top of jul
 -obfuscate <password>                  obfuscate the password and exit
 -resetExtract                          clean previous extract directory
 -serverXmlPath <serverXmlPath>         server.xml to use, optional
 -X,--debug                             debug

 

 

運行Mojo:讓你的Maven war 項目更快速地運行

當開發一個war項目時,一般須要build war,而後將其部署到一個安裝好的Tomcat實例中。這很耗費時間和資源,同時必須有一個本地的Tomcat實例。

而運行mojo則可讓你免於這些,只須要在你的Maven build的內置Tomcat實例中運行war便可。

 

注意,若是你有多個Maven項目,且在使用Maven 3,你不須要在運行goal以前install全部資源,在根模塊直接使用tomcat6/7:run 便可,該插件會自動探測不一樣模塊的build輸出目錄,並會使用webapp類加載器中的這些目錄來替換依賴。

 

運行內置Tomcat

在POM中配置插件的版本,並使用 mvn tomcat6/7:run 。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <!-- or if you want to use tomcat 6.x <artifactId>tomcat6-maven-plugin</artifactId> -->
    <version>2.2</version>
    <configuration>
        <!-- http port -->
        <port>9090</port>
        <!-- application path always starts with / -->
        <path>/</path>
        <!-- optional path to a context file -->
        <contextFile>${tomcatContextXml}</contextFile>
        <!-- optional system propoerties you want to add -->
        <systemProperties>
            <appserver.base>${project.build.directory}/appserver-base</appserver.base>
            <appserver.home>${project.build.directory}/appserver-home</appserver.home>
            <derby.system.home>${project.build.directory}/appserver-base/logs</derby.system.home>
            <java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
        </systemProperties>
        <!-- if you want to use test dependencies rather than only runtime -->
        <useTestClasspath>false</useTestClasspath>
        <!-- optional if you want to add some extra directories into the classloader -->
        <additionalClasspathDirs>
            <additionalClasspathDir></additionalClasspathDir>
        </additionalClasspathDirs>
    </configuration>
    <!-- For any extra dependencies needed when running embedded Tomcat (not 
        WAR dependencies) add them below -->
    <dependencies>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>\${derbyVersion}</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
</plugin>

 

Maven項目結構

pom.xml             (top level pom with packaging pom)
my-api/pom.xml      (API project with packaging jar)
my-api-impl/pom.xml (API implementation project with packaging jar)
my-webapp/pom.xml   (webapp project with packaging war)

對於上面這樣的結構,須要從頂級目錄運行:mvn tomcat6/7:run -pl :my-webapp -am

 

配合selenium mojo使用

你可使用該mojo在Tomcat實例中啓動你的項目,並針對該實例運行你的selenium測試。

下列配置會在pre-integration-test中啓動一個內置的Tomcat,而後在 post-integration-test時關閉它。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <!-- or if you want to use tomcat 6.x <artifactId>tomcat6-maven-plugin</artifactId> -->
    <version>2.2</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                ....
                <fork>true</fork>
                ....
            </configuration>
        </execution>
        <execution>
            <id>tomcat-shutdown</id>
            <goals>
                <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

官方文檔連接:

http://maven.apache.org/plugins/index.html

相關文章
相關標籤/搜索