Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Tomcat and deploy project with own server.xml

Tags:

java

maven

tomcat

I just want to run a web project that has been configured in a maven project with the pom.xml. It uses the maven tomcat7-maven-plugin to deploy the web app artifact and all is working properly at this point.

Now I want to add its own server.xml and tomcat-users.xml in the tomcat configuration. I read that I need to add the following lines.

 <serverXml>src/main/resources/tomcat/server.xml</serverXml>
 <tomcatUsers>src/main/resources/tomcat/tomcat-users.xml</tomcatUsers>

And that is fine. It is working now and tomcat is deployed using the configuration file above,but the problem is > Web application artifact is not deployed there (is not deployed automatically when I run tomcat7:run). It seems the artifact is not detected by the plugin and just starts the tomcat server without adding the artifact to the webapps just use the new config files.

I use this config.

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
          <execution>
            <id>tomcat-run</id>
            <goals>
              <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
           <configuration>
        <url>http://localhost:8080/web-services</url>
        <path>/web-services</path>
        <serverXml>src/main/resources/tomcat/server.xml</serverXml>
                <tomcatUsers>src/main/resources/tomcat/tomcat-users.xml</tomcatUsers>
                   <warRunDependencies>
                    <warRunDependency>
                     <dependency>
                        <groupId>com.koitoer.webservices</groupId>
                        <artifactId>web-services</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <type>war</type>
                      </dependency>
                    <contextPath>/web-services</contextPath>
                </warRunDependency>
              </warRunDependencies>
    </configuration>
    </execution>
        </executions>
</plugin>

But tomcat starts deploy the webapp artifact but does not use the new config files.

What is the correct configuration there, I checked this post but nothing, this is possible or I should add the war file manually using tomcat manager?

like image 310
Koitoer Avatar asked Apr 16 '14 06:04

Koitoer


People also ask

What is the server server xml configuration file in Tomcat?

The server. xml file is Tomcat's main configuration file, and is responsible for specifying Tomcat's initial configuration on startup as well as defining the way and order in which Tomcat boots and builds. The elements of the server.

How can I deploy WAR file in Tomcat?

Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.

Where is Tomcat server xml?

Inside the configurations of tomcat server, the Server. xml file is the main configuration file located at path TOMCAT_HOME/conf/server. xml. Inside TOMCAT_HOME/conf/name-of-server.


2 Answers

The tomcat7-maven-plugin documentation says for <serverXml>:

server.xml to use Note if you use this you must configure in this file your webapp paths.

I think this means that you have to insert a <Context> element with the path of your war inside the <Host> element, like so:

<Host appBase="webapps" autoDeploy="true" deployXML="false" name="localhost" unpackWARs="true">
    <Context docBase="../../webapp" path="/webapp" reloadable="true" />
</Host>

Where "webapp" is my generated war name. Apparently, appBase is relative to target/tomcat (and "webapps" seems to be the default value). docBase is relative to appBase so for simplicity I used the relative path to the build directory.

This is working for me, and without that <Context> element I get a white page.

If you are using filtering you can probably replace docBase by a property. However take care to exclude the server.xml (and tomcat-users.xml) from your war file!

like image 86
Didier L Avatar answered Oct 23 '22 11:10

Didier L


I have added the following into server.xml and my application is working fine with tomcat7:run-war command:

<Host appBase="webapps" autoDeploy="true" deployXML="false"
    name="localhost" unpackWARs="true">
    <Context docBase="../../webApplicationName"
        path="/webApplicationName" reloadable="true" />
    <Valve className="org.apache.catalina.valves.AccessLogValve"
        directory="logs"
        prefix="localhost_access_log." suffix=".txt"
        pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

Here is how I configure plug-in in pom.xml

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>

    <configuration> 
        <path>/</path>
        <serverXml>src/main/tomcatconf/server.xml</serverXml>
        <warDirectory>target/appName</warDirectory>
        <webapps> 
            <webapp> 
                <groupId>com.abc</groupId> 
                <artifactId>appName</artifactId> 
                <version>7.0.1</version> 
                <type>war</type>    
            </webapp> 
        </webapps> 
    </configuration>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.5</version>
    </dependency>
</plugin>

I have dropped server.xml + other config files into src/main/tomcatconf directory (default value of plugin) and also have context.xml into META-INF dir and into src/main/tomcatconf. The plugin seems to be picking up context.xml from one of the directories.

Hope this works for you.

like image 26
user4075768 Avatar answered Oct 23 '22 11:10

user4075768