【问题标题】:How can I deploy multiple wars using the tomcat plugin in maven?如何使用 maven 中的 tomcat 插件部署多个战争?
【发布时间】:2010-11-14 10:45:46
【问题描述】:

我有两个战争,我使用 tomcat 插件在两个 maven 项目中部署。 我想一步完成,并且能够在一个 Maven 项目中部署不止一场战争。 我怎样才能做到这一点?有什么建议吗?

【问题讨论】:

    标签: tomcat maven-2


    【解决方案1】:

    我无法对此进行测试,但我可以想到两种方法。任何一种都可能适合你。

    选项 1:

    在其中一个项目中,您可以定义 tomcat 插件的配置。在下面的 sn-p 中定义了两个执行,都绑定到预集成测试阶段(这可能不是执行此操作的最佳阶段,但它似乎是一个很好的起点,因为战争将被打包)。每次执行都会部署在其配置的 warFile 属性中定义的战争。

    <project>
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.0-beta-1</version>
            <executions>
              <execution>
                <id>deploy1</id>
                <phase>pre-integration-test</phase>
                <configuration>
                  <warFile>path/to/my/warFile1.war</warFile>
                </configuration>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
              <execution>
                <id>deploy2</id>
                <phase>pre-integration-test</phase>
                <configuration>
                  <warFile>path/to/my/warFile2.war</warFile>
                </configuration>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          ...
        </plugins>
        ...
      </build>
      ...
    </project>
    

    选项 2: 这可能是更好的方法。在每场战争中定义一个执行(您可以省略 warFile 元素,因为可以使用默认值)。然后,您可以使用引用每个战争项目的模块声明定义第三个项目。构建父级时,将构建两个战争并部署战争。

    第三个项目的模块声明:

    <modules>
      <module>relative/path/to/war1</module>
      <module>relative/path/to/war2</module>
    <modules>
    

    以及每个战争项目的配置:

    <project>
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.0-beta-1</version>
            <executions>
              <execution>
                <id>deploy</id>
                <phase>pre-integration-test</phase>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          ...
        </plugins>
        ...
      </build>
      ...
    </project>
    

    Maven 有一些属性可以用来避免绝对路径。

    ${maven.repo.local} will resolve to the local repository
    ${project.basedir} is the project directory (the root of the project)
    ${project.build.directory} is the build directory (default is "target")
    ${project.build.outputDirectory} is the directory where compilation output goes (default is "target/classes")
    

    【讨论】:

    • 嘿,非常感谢您的建议。我有一个后续问题。在 pom 中而不是给出war文件的绝对路径,有没有一种方法可以从本地存储库或远程存储库路径中获取war文件。
    • 我在答案中添加了一些属性,您可以使用 ${property}/path/to/reference 指定相对路径
    【解决方案2】:

    您也可以创建“超级战争”并进行部署

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2014-08-20
      • 2012-11-22
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      相关资源
      最近更新 更多