Tomcat plugin - Maven: launching webapps but not the current project

tomcat7-maven-plugin allows you to run the current project as a web application, and an optional <webapps> can be specified that will be loaded into tomcat at the same time.

My project is not a web application, but it accesses the services provided by webapps. So, how can you deploy multiple webapps without starting the project itself as a webapp? The following Maven snippet results in FileNotFoundExceptions because context.xml could not 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 if your application is not webapp, you need to configure path and 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, although the web.xml does not exist:

 <?xml version="1.0" encoding="utf-8"?> <Context path="/my/non/existing/webapp"> <WatchedResource>WEB-INF/web.xml</WatchedResource> </Context> 
+7
source share
2 answers

This is probably an abuse of the tomcat maven plugin, but here is the solution I found. By the way, your fake contextual file solution did not work for me because I needed to run another webapp, and my application is also webapp.

There is a jira that will provide the best solution to our problem. See https://issues.apache.org/jira/browse/MTOMCAT-228 . Now about my decision ...

First you need to copy the war to the directory. I suggest a target directory so that it can be easily cleaned. Depending on whether you want to support the fulfillment or goals during the war, it depends on whether you 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> 

Then you need to configure tomcat plugins to view the directory or war that you just copied in the previous step. Below is the configuration for tomcat 6 and 7, which is identical to the identifier of the artifact.

 <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 do not need to configure warDirectory or copy if you want to support tomcat: run. Conversely, you do not need to configure warSourceDirectory or you need to perform unpacking and unpacking if you want to support tomcat: run-war.

In conclusion, we note that this workaround for dependencies also works well with the Jetty Maven plugin, so if you want to support both tomcat and the pier, this may be a good way to do this.

+6
source

I just had a different idea on how to solve this problem in maven-like. I wanted to document this here, as this might help others. I have not tested this solution yet, but I see no reason why it will not work.

Basically, if you want to launch a โ€œdifferentโ€ webapp than the current one using the berth or tomcat plugins, what you can do is:

Create another maven module in your project. This module will be the empty military webapp overlay that you want to run. Then you can put all your berth or tomcat configurations into this new module (or just centralize the configuration of the berth and tomcat in the parent pom within the framework of pluginManagement). Since jetty and tomcat are designed to run the "current" application, no special hacked configurations are required.

+2
source

All Articles