Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat - Maven plugin: run webapps but not current project

The tomcat7-maven-plugin allows running the current project as a Web application and additional <webapps> can be specified that will be simultaneously loaded into tomcat.

My project is not a Web application, but it accesses services that are provided by webapps. So how is it possible to deploy a number of webapps without running the project itself as a webapp? The following Maven snippet results in FileNotFoundExceptions because a context.xml cannot be found.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.0</version>
  <executions>
    <execution>
      <id>run-tomcat</id>
      <phase>${tomcat.run.phase}</phase>
      <goals><goal>run-war-only</goal></goals>
      <configuration>
        <webapps>
          <webapp>
            <contextPath>/my/app1</contextPath>
            <groupId>...</groupId> 
            <artifactId>...</artifactId>
            <version>...</version>
            <type>war</type>    
            <asWebapp>true</asWebapp>
          </webapp>
          ... possibly more webapps ...
        </webapps> 
      </configuration>
    </execution>
    <execution>
      <id>tomcat-shutdown</id>
      <phase>${tomcat.shutdown.phase}</phase>
      <goals><goal>shutdown</goal></goals>
    </execution>
  </executions>
</plugin>

Workaround:

Even though your application itself is not a webapp, you need to configure a path and a contextFile for it:

<configuration>
    <path>/my/non/existing/webapp</path>
    <contextFile>src/test/resources/context.xml</contextFile>
    <webapps>
    ...

The specified context.xml file must exist. The following worked for me, even though the web.xml file does not exist:

<?xml version="1.0" encoding="utf-8"?>
<Context path="/my/non/existing/webapp">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
like image 453
dokaspar Avatar asked Mar 18 '13 15:03

dokaspar


People also ask

How to deploy a web app using tomcat7 Maven?

First all related dependencies will be downloaded, the webapp will be compiled and packaged and then tomcat7 maven plugin will deploy the webapp and start the embedded server. You will have something similar to following. [INFO] Scanning for projects...

How to embed Tomcat in a Java web application?

This is exactly what you can achieve thanks to Maven. Maven has a wonderful plugin tomcat7-maven-plugin that we can use to embed tomcat within our Java web application. The idea is to create standalone web project that can start tomcat server and run on its own. All we need to do is the add following maven plugin in your pom.xml plugins list.

Is there a tomcat8-maven-plugin?

There are other plugins tomcat8-maven-plugin and tomcat9-maven-plugin available in the maven repository but those are not from the official apache group. Edit project’s pom.xml file and plugin entry inside build tag.

How do I rerun a Maven webapplication?

To rerun the WebApplication you will need to close the currently running application. You can see a similar guide: Run Maven Web Application in Jetty Maven Plugin, at: These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.


1 Answers

This is probably abusing the tomcat maven plugin but here is a solution I found. BTW your fake context file solution didn't work for me because I needed to run a different webapp and my app is also a webapp.

There is a jira out there that would provide a better solution to our problem. See https://issues.apache.org/jira/browse/MTOMCAT-228. Now on to my solution...

First, you need to copy the war to a directory. I suggest the target directory so it can be easily cleaned. Depending on whether you want to support the run or the run-war goals depends on whether you just copy the war or copy the war and unpack it.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>${maven-dependency-plugin.version}</version>
  <executions>
    <execution>
      <id>copy-war</id>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>org.foo</groupId>
            <artifactId>bar</artifactId>
            <version>${bar.version}</version>
            <type>war</type>
            <overWrite>true</overWrite>
            <outputDirectory>${project.build.directory}/bar/</outputDirectory>
          </artifactItem>
        </artifactItems>
        <stripVersion>true</stripVersion>
      </configuration>
    </execution>
    <execution>
      <id>copy-war-unpack</id>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>org.foo</groupId>
            <artifactId>bar</artifactId>
            <version>${bar.version}</version>
            <type>war</type>
            <overWrite>true</overWrite>
            <outputDirectory>${project.build.directory}/bar/bar</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

Next, you need to configure the tomcat plugins to look at the directory or war that you just copied to in the above step. The following shows a configuration for tomcat 6 & 7 which is identical other than the artifact id.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat6-maven-plugin</artifactId>
  <version>${tomcat6-maven-plugin.version}</version>
  <configuration>
    <port>8090</port>
    <path>${default.rice.context.path}</path>
    <warDirectory>${project.build.directory}/bar/bar.war</warDirectory>
    <warSourceDirectory>${project.build.directory}/bar/bar</warSourceDirectory>
  </configuration>
</plugin>
<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>${tomcat7-maven-plugin.version}</version>
  <configuration>
    <port>8090</port>
    <path>${default.rice.context.path}</path>
    <warDirectory>${project.build.directory}/bar/bar.war</warDirectory>
    <warSourceDirectory>${project.build.directory}/bar/bar</warSourceDirectory>
  </configuration>
</plugin>

To be clear you don't need to configure warDirectory or need copy-war execution if you only want to support tomcat:run. Conversely, you don't need to configure warSourceDirectory or need copy-war-unpack execution if you only want to support tomcat:run-war.

One final note, this dependency copy workaround also works well with the Jetty Maven plugin so if you wanted to support both tomcat & jetty this might be a good way to do it.

like image 180
Travis Schneeberger Avatar answered Nov 12 '22 01:11

Travis Schneeberger