執行 Mojo:快速執行您的 Maven 戰爭專案!

在開發戰爭專案時,您通常會建置戰爭並將其部署到已安裝的 Tomcat 執行個體。這會耗費時間和資源,而且也需要一個本機 Tomcat 執行個體。

執行 mojo 讓您無需執行這些工作,只要在 Maven 建置中,在嵌入式 Tomcat 執行個體內執行您的戰爭即可。

注意如果您有一個多模組 Maven 專案並使用 Maven3,您不需要在使用執行目標之前安裝所有模組,只要從根模組使用 tomcat6/7:run,外掛程式就會自動偵測來自各種模組的建置輸出目錄,並在 Web 應用程式類別載入器中將相依性替換為這些目錄。

執行嵌入式 Tomcat

使用外掛程式版本設定您的 pom(有關其他 mojo 參數,請參閱每個 mojo 的文件)。

並使用: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>