【问题标题】:Is it possible to supply Tomcat6's context.xml file via the Maven Cargo plugin?是否可以通过 Maven Cargo 插件提供 Tomcat6 的 context.xml 文件?
【发布时间】:2011-05-17 09:31:16
【问题描述】:

如果可能,我希望将 Tomcat 的 context.xml 文件保留在我的 WAR 文件的 META-INF 目录之外。这可以用 Maven 的 cargo 插件来完成吗?我似乎找不到正确的配置。

【问题讨论】:

    标签: tomcat maven tomcat6 cargo maven-cargo


    【解决方案1】:

    尤里卡!在研究了这个问题很多天后,我终于找到了一个非常有效的解决方案。关键是获取您的 Tomcat XML 上下文片段文件并使用 cargo 的 <configfiles> 元素将其放入名称为 context.xml.defaultconf/Catalina/localhost 目录中。唯一的缺点是这将使您的上下文定义可用于所有 Web 应用程序,但这并不重要,只有 Cargo 使用这个 Tomcat 实例,因此没有其他 Web 应用程序。

    这是配置:

    <configuration> <!-- Deployer configuration -->
        <type>standalone</type>
        <properties>
           <cargo.servlet.port>${tomcat6.port}</cargo.servlet.port>
        </properties>
        <deployables>
          <deployable>
            <groupId>com.myapp<groupId>
            <artifactId>myapp-war</artifactId>
            <type>war</type>
            <properties>
                   <context>${tomcat6.context}</context>
            </properties>
           </deployable>
         </deployables>
        <configfiles>
           <configfile>
             <file>${basedir}/../config/tomcat-context.xml</file>
             <todir>conf/Catalina/localhost/</todir>
             <tofile>context.xml.default</tofile>
           </configfile>
        </configfiles>
    </configuration>
    

    最终结果是不再有仅用于测试的虚假 WAR 模块,也不再合并 WAR。希望这对某人有所帮助。

    【讨论】:

    • 它工作除了货物 1.1.1 接缝不添加新文件。我只能替换文件。
    • @Ralph 有一个 &lt;overwrite&gt; 属性默认设置为 true,将其更改为 false 应该可以解决问题。
    【解决方案2】:

    根据https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_contexthttps://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming Tomcat 9 允许制作单独的应用程序context.xml(选中)。

            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.7.10</version>
                <configuration>
                    <container>
                        <containerId>tomcat9x</containerId>
                        <systemProperties>
                            <file.encoding>UTF-8</file.encoding>
                            <spring.profiles.active>tomcat,datajpa</spring.profiles.active>
                        </systemProperties>
                        <dependencies>
                            <dependency>
                                <groupId>org.postgresql</groupId>
                                <artifactId>postgresql</artifactId>
                            </dependency>
                        </dependencies>
                    </container>
                    <configuration>
                        <configfiles>
                            <configfile>
                                <file>src/main/resources/tomcat/context.xml</file>
                                <todir>conf/Catalina/localhost/</todir>
                                <tofile>${project.build.finalName}.xml</tofile>
                            </configfile>
                        </configfiles>
                    </configuration>
                    <deployables>
                        <deployable>
                            <groupId>ru.javawebinar</groupId>
                            <artifactId>topjava</artifactId>
                            <type>war</type>
                            <properties>
                                <context>${project.build.finalName}</context>
                            </properties>
                        </deployable>
                    </deployables>
                </configuration>
            </plugin>
        </plugins>
    

    【讨论】:

      【解决方案3】:

      我还没有找到一种方法来做到这一点,但我想出了一个可以在我的项目中使用的解决方法。我目前有一个基本上包含 3 个子模块的项目:

          dependencies
          webapp
          smoketest
      

      当我构建“webapp”项目时,我执行以下插件声明:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <executions>
              <execution>
              <id>create-war-smoketest</id>
              <phase>verify</phase>
              <goals>
                  <goal>war</goal>
              </goals>
              <configuration>
                  <webappDirectory>${project.build.directory}/exploded</webappDirectory>
                  <primaryArtifact>false</primaryArtifact>
                  <classifier>smoketest</classifier>
                  <webResources>
                  <resource>
                      <filtering>true</filtering>
                      <directory>src/test/resources/smoketest</directory>
                      <targetPath>META-INF</targetPath>
                      <includes>
                          <include>context.xml</include>
                      </includes>
                  </resource>
                  </webResources>
              </configuration>
              </execution>
          </executions>
      </plugin>
      

      然后,当我在 SmokeTest 项目中运行我的 Cargo/WebTest 套件时,我将 Smoketest WAR 文件指定为依赖项,并在我的 Cargo 配置中设置我的可部署对象:

      <deployables>
          <deployable>
              <groupId>${pom.groupId}</groupId>
              <artifactId>webapp</artifactId>
              <type>war</type>
              <properties>
                  <context>smoketest</context>
              </properties>
          </deployable>
      </deployables>
      

      依赖看起来像:

      <dependencies>
          <dependency>
              <groupId>${pom.groupId}</groupId>
              <artifactId>webapp</artifactId>
              <version>${pom.version}</version>
              <classifier>smoketest</classifier>
              <type>war</type>
              <scope>system</scope>
              <!-- trick the dependency plugin to never look for it in the repo -->
              <systemPath>${basedir}/../webapp/target/webapp-${pom.version}-smoketest.war</systemPath>
          </dependency>
      </dependencies>
      

      它非常脏,但至少现在可以使用。一个快速说明:我关于强制它永远不要在 repo 中查找版本的评论在这一点上可能是不正确的;我认为这个技巧可能在某个时候被依赖插件的改变打破了。

      【讨论】:

      • 我也试过这个——它也适用于我。只是希望放弃一次性的 WAR 工件——因为我们不希望/不能发送带有过滤的 context.xml 文件的 WAR。
      猜你喜欢
      • 2011-09-20
      • 2012-03-05
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2012-05-02
      • 2012-04-17
      • 2011-04-24
      相关资源
      最近更新 更多