9

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

4

3 回答 3

17

尤里卡!在研究了这个问题很多天后,我终于找到了一个非常有效的解决方案。关键是获取您的 Tomcat XML 上下文片段文件,并使用<configfiles>cargo 元素将其放入conf/Catalina/localhost名称为 的目录中context.xml.default。唯一的缺点是这将使您的上下文定义可用于所有 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。希望这可以帮助某人。

于 2010-12-11T17:24:53.177 回答
4

根据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>
于 2020-03-22T10:06:25.680 回答
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 中查找版本的评论在这一点上可能是不正确的;我认为这个技巧可能在某个时候被依赖插件的改变打破了。

于 2010-11-29T23:54:03.860 回答