是否可以通过Maven Cargo插件提供Tomcat6的context.xml文件?

问题描述 投票:7回答:3

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

tomcat maven tomcat6 cargo maven-cargo
3个回答
13
投票

尤里卡!经过数天的研究,我终于找到了一个非常有效的解决方案。关键是获取Tomcat XML上下文片段文件,并使用cargo的<configfiles>元素将其拖放到名称为conf/Catalina/localhostcontext.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。希望这对某人有帮助。


2
投票

我还没有找到一种方法,但是我想出了一个解决方案,可以在我的项目中使用。我目前有一个基本上包含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>

它非常脏,但至少可以正常工作……目前。一个简短的注释:我关于强迫它永远不要在仓库中寻找版本的评论可能是不正确的;我认为在某些时候对依赖插件的更改可能会破坏这一技巧。


0
投票

根据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>
© www.soinside.com 2019 - 2024. All rights reserved.