mvn tomcat:run - 我该如何编辑server.xml文件?

8
我希望能够在命令行中运行"mvn tomcat:run"命令,但是我该如何编辑server.xml文件以设置连接器中的maxHttpHeaderSize="65536"呢?或者我可以在pom.xml文件中配置这些连接器吗?
谢谢,
Nik
4个回答

8

org.codehaus.mojo:tomcat-maven-plugin可以让你在配置部分中设置server.xml文件的路径:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>tomcat-maven-plugin</artifactId>
  <configuration>
    <serverXml>path_to_server_xml_file</serverXml>
  </configuration>
</plugin>

6
很遗憾,经过一些调查,我认为没有办法编辑server.xml的连接器。 mvn tomcat:run使用嵌入式Tomcat。
除非有人找到了解决方案,否则最好的选择似乎是转移到maven cargo插件并将自定义的server.xml与Tomcat安装一起打包成ZIP文件。
<cargo containerId="tomcat7x" [...]>
  <zipUrlInstaller
      installUrl="file://tomcat-custom.zip",
      installDir="target/installs"/>
  [...]
</cargo>

或类似的东西...

看起来你是对的,目前除了自己动手通过 cargo 插件等方式实现外,没有其他方法可以做到这一点。 - niklassaers

3
我一直在尝试使用tomcat:run目标的serverXml参数(http://tomcat.apache.org/maven-plugin-2/tomcat6-maven-plugin/run-mojo.html#serverXml)进行实验。
以下server.xml似乎没有错误,但是没有Context元素时,它不会加载Web应用程序。我认为,如果我将src/main/webapp/META-INF/context.xml中的Context元素复制到Host元素内部,它可能会正常工作。
<?xml version='1.0' encoding='utf-8'?>
<Server port="-1" shutdown="SHUTDOWN">
    <Service name="Catalina">
        <Connector port="8080" protocol="HTTP/1.1" />
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost" appBase="webapps">
            </Host>
        </Engine>
    </Service>
</Server>

要在此服务器上运行,我需要将serverXml作为Maven命令行上的属性传递:

mvn -Dmaven.tomcat.serverXml=src/main/resources/server.xml tomcat:run

如果你使用支持Tomcat 6和7的插件版本,目标可能需要是tomcat6:run


1

请查看http://docs.codehaus.org/display/CARGO/Custom+File+Configurations

我认为您可以这样做,并将自定义的server.xml放置在您的项目中:

<configuration>
    <type>standalone</type>
    <configfiles> 
        <configfile> 
            <file>${basedir}/src/main/resources/server.xml</file> 
            <todir>conf</todir> 
        </configfile> 
    </configfiles> 
</configuration>

使用默认的cargo server.xml作为模板,进行属性替换:

<Server port="@cargo.rmi.port@" shutdown="SHUTDOWN" debug="@catalina.logging.level@">

  <Service name="Catalina" debug="@catalina.logging.level@">

    <Connector port="@cargo.servlet.port@"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
        connectionTimeout="20000" disableUploadTimeout="true"
        scheme="@cargo.protocol@" secure="@catalina.secure@"
        debug="@catalina.logging.level@"
        emptySessionPath="@catalina.connector.emptySessionPath@"
        URIEncoding="@catalina.servlet.uriencoding@" />

    <!-- Define an AJP 1.3 Connector on port @cargo.tomcat.ajp.port@ -->
    <Connector port="@cargo.tomcat.ajp.port@" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="@cargo.hostname@" 
        debug="@catalina.logging.level@">

      <Realm className="org.apache.catalina.realm.MemoryRealm" />

      <!-- Note: There seems to be a bug in Tomcat 5.x if the debug attribute 
           is present. Ideally we would have written:
               debug="@catalina.logging.level@"
           However, doing this result in a NullPointerException in 
           ExpandWar.java at line 145. -->
      <Host name="@cargo.hostname@" appBase="webapps" unpackWARs="true"
          autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">

        <!-- Contexts to explicitely point to where the wars are located -->
        @tomcat.webapps@

        <Valve className="org.apache.catalina.valves.AccessLogValve"
            directory="logs" prefix="@cargo.hostname@_access_log." suffix=".txt"
            pattern="common" resolveHosts="false"/>

      </Host>
    </Engine>
  </Service>
</Server>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接