创建 War 与捆绑 Tomcat

Create A War With Bundled Tomcat

我的项目结构如下所示:

+Parent  
|   
+---dist
|   
+---module1 (war)
|   
+---module2 (jar)

我希望 dist 模块将 module1 中的 war 与 Tomcat 的实例打包在一起,并将其全部压缩,以便我可以通过在服务器上解压缩来部署它。

我知道创建可运行 wars 和嵌入 tomcat 的插件,它们从 jar 开始,但我希望整个 Tomcat 与我的 war 压缩在一起在 webapps 目录中。

我可以用 assembly 插件手动完成,但 cargo 插件似乎应该做我想做的事。

我的 dist pom 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>my-parent</artifactId>
    <groupId>com.my.package</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>dist</artifactId>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>${project.parent.groupId}</groupId>
      <artifactId>my-war</artifactId>
      <version>${project.parent.version}</version>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.4.15</version>
        <configuration>
          <container>
            <!--containerId must be equal to one of the containers supported by Cargo -->
            <!--https://codehaus-cargo.github.io/cargo/Container.html-->
            <containerId>tomcat8x</containerId>
            <artifactInstaller>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat</artifactId>
            <version>8.0.24</version>
            </artifactInstaller>
          </container>
          <configuration>
            <type>standalone</type>
            <home>${basedir}/target/cargo/installs/tomcat-8.0.24</home>
          </configuration>
          <deployables>
            <deployable>
              <groupId>${project.parent.groupId}</groupId>
              <artifactId>my-war</artifactId>
              <type>war</type>
            </deployable>
          </deployables>
        </configuration>
        <executions>
          <execution>
            <id>package</id>
            <phase>package</phase>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

首先,当我使用工件安装程序时必须指定主目录似乎很奇怪。

其次,输出结构如下所示:

+---dist
|   \---target
|       +---cargo
|       |   \---installs
|       |       \---tomcat-8.0.24
|       |           \---apache-tomcat-8.0.24
|       |               +---bin
|       |               +---conf
|       |        ..........
|       |        ..........
|       |              
|       \---package
|           +---apache-tomcat-8.0.24
|           |   +---bin
|           |   +---conf
|           |   ..........
|           |   ..........
|           +---bin
|           +---lib
|           \---temp

请注意 tomcat 目录是包的子目录,但包目录中已经存在 tomcat 目录的存根。

第三,也是最重要的一点,我的 war 不在 dist/target 目录中!

我不确定那个额外的目录是从哪里来的,但现在已经解决了。 我想我可能是来自 dist 模块而不是根模块的 运行 包,这就是 war 不存在的原因。

下面的示例对我有用,然后我添加了 maven 程序集插件来压缩它。

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.Whosebug</groupId>
  <artifactId>question</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <tomcat.version>8.0.24</tomcat.version>
  </properties>

  <build>
    <plugins>
      <!--Create a war-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <!--This is an empty demo project-->
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <!--Create the Tomcat bundle with our war in it-->
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.4.15</version>
        <configuration>
          <container>
            <!--containerId must be equal to one of the containers supported by Cargo -->
            <!--https://codehaus-cargo.github.io/cargo/Container.html-->
            <containerId>tomcat8x</containerId>
            <artifactInstaller>
              <groupId>org.apache.tomcat</groupId>
              <artifactId>tomcat</artifactId>
              <version>${tomcat.version}</version>
            </artifactInstaller>
          </container>
          <configuration>
            <type>standalone</type>
            <home>${project.build.directory}/cargo/installs/tomcat-${tomcat.version}/apache-tomcat-${tomcat.version}
            </home>
          </configuration>
          <deployables>
            <deployable>
              <groupId>${project.groupId}</groupId>
              <artifactId>${project.artifactId}</artifactId>
              <type>war</type>
            </deployable>
          </deployables>
        </configuration>
        <executions>
          <execution>
            <id>cargo-deploy</id>
            <phase>package</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>