maven tomcat plugin configuration - java

Requirement is to deploy application at tomcat root using maven plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://www.myhost.com:8080/manager</url>
<server>tomcat6</server>
<path>/</path>
<contextFile>src/main/tomcatconf/context.xml</contextFile>
<mode>context</mode>
</configuration>
Above is deploying application at root but the problem is application using images, javascript and other pdf files (of large size) from a folder "static" which is stored outside application.
c:\static\
Please suggest for configuration required in pom.xml to access images like below.
http://www.myhost.com:8080/static/image.js
http://www.myhost.com:8080/static/about.pdf

You can get embedded tomcat7 plugin like this:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>

Related

Overriding Tomca7 Maven Plugin configuration

I am working in a project that uses a tomcat7-maven-plugin for testing, defined in the pom like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9092</port>
...
</configuration>
</plugin>
</plugins>
</build>
Is there a way to start this on a different port without editing the pom? When running mvn tomcat7:run -Dmaven.tomcat.port=9099 it still starts the tomcat on port 9092. I can't change the pom permanently due to others in the project desiring this default configuration, and I don't want to have to edit it locally every time I start the tomcat.
Thanks for help!

maven war application setting up contextroot

i am building a war application file using the below maven config, however when i start the application in tomcat the Context Root is set to "/CommerceApi-0.0.1-SNAPSHOT/"
I want this to be set to "/api",
any ideas?, below is the pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CommerceApi</groupId>
<artifactId>CommerceApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>CommerceApiCommon</groupId>
<artifactId>CommerceApiCommon</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
There are three ways to do it:
1. If you are not using Eclipse/MyEclipse to deploy the application onto application server -
You need to make use of maven-war plugin, you can specify warName in configuration section.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warName>customwarname</warName>
</configuration>
</plugin>
2. If you are using Eclipse/MyEclipse to deploy the application onto application server -
If you are using eclipse and deploying war using eclipse then you can use following maven configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<wtpversion>2.0</wtpversion>
<wtpContextName>customwarname</wtpContextName>
</configuration>
</plugin>
Then, run following commands to update eclipse settings.
mvn eclipse:eclipse -Dwtpversion=2.0
Restart Eclipse and then navigate to project properties, Properties->Web to view the reflected changes in root-context value or navigate to Deployment Assembly of the project to view the changes
Note that above can be achieved using m2eclipse by adding a new plugin.
3. Application server specific:
You should prefer to follow server agnostic approach, but if are required to do it then you can configure root context url in server specific configuration file. You can find detailed approach here
Your application is not in charge to define its own context path. That's task of the container, the Tomcat in your case. Tomcat offers several options of how to set the context path. You may define the context path it in a context file or specify the context path in the manager application. If you use Jenkins or other CI tools you'd be able to specify the context path there, as well.
Best you read up on the options you have regarding your particular Tomcat version.
There are several options. Some are described in Define Servlet Context in WAR-File
Using tomcat you can also define the context.xml path: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#containerConfigXML and maybe configure it in there: https://tomcat.apache.org/tomcat-7.0-doc/config/context.html
the fastest way os probably to change the final name (see other stackoverflow question).

tomcat-maven-plugin with multiple modules building one war only

I try to build a web application based on multiple maven modules. One of the modules is called "web" and is solely responsible to package a war which should be deployed to a tomcat using the tomcat7-maven-plugin. I have following modules defined in my parent.pom:
common
persistence
persistence-embedded
service
rest
web
All of them are combined into one web-application-war, the web module has set packaging to war. The problem is, that my war file is deployed for each submodule (and the main-parent-module) over and over again when I run mvn tomcat7:redeploy, which leads to 7 deployments. Apparently, this is not how it should be. The tomcat7-maven-plugin configuration currently looks like this:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<ignorePackaging>true</ignorePackaging>
<url>http://localhost:8080/manager/text</url>
<server>tomcatServer</server>
<path>/webapp</path>
<warFile> /home/username/dev/maven-multimodule-example/web/target/maven-multimodule-example-1.0-SNAPSHOT.war</warFile>
<username>admin</username>
<password>password</password>
</configuration>
</plugin>
As you can see, I need to specify the warFile (which is not a solution but rather a hack, because I can't use ${project.basedir} which would lead to the submodule-dir) to make it work.
However, if I run the web application with mvn tomcat7:run, it looks quite good, because the other non-war-building modules are skipped by the plugin.
How can I configure the plugin the right way to deploy the war file only once?
Every configuration within the <build> section of a parent POM will be inherited and thus executed in all child modules. So if you want to deploy only once, add it to only one POM (e.g. the web POM).
Thanks to dunni's help I noticed my missunderstanding of how multimodule projects are built. Now I've placed the plugin configuration in the web module and added an execution, bound to the install phase so that I can rebuild the whole project and get it deployed to my tomcat. Obviously maven takes care of the right execution order of the modules.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>tomcatServer</server>
<path>/webapp</path>
<warFile>${project.basedir}/target/${project.parent.artifactId}-${project.parent.version}.war</warFile>
<username>admin</username>
<password>password</password>
</configuration>
<executions>
<execution>
<id>redeployafterinstall</id>
<phase>install</phase>
<goals>
<goal>redeploy</goal>
</goals>
</execution>
</executions>
</plugin>

Maven War Plugin Websphere

I am trying to get a Rational Application Developer project to run on a websphere server. I am trying to get the maven-war-plugin to work. However, when trying to start the server, websphere can not find the UI Files. I have my plugin here:
<build>
<outputDirectory>${project.basedir}\WebContent\WEB-INF\classes</outputDirectory>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webXml>/WebContent/WEB-INF/web.xml</webXml>
<webappDirectory>WebContent</webappDirectory>
<source>/codeCoverageUI2/src/</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
I feel the issue is with my webappDirectory but I do not know what else to put it to, besides the location of all my UI files.
Anyone have some insight?
I am assuming you are using WebSphere Classic V8 or earlier?
What we recommend because when developing a project in RAD/WDT, WAS Classic requires building the project in a "Single Root" structure. Add the following to your pom:
<build>
<outputDirectory>${project.basedir}\WebContent\WEB-INF\classes</outputDirectory>
<finalName>${project.name}</finalName>
...
This will build your source within the Web content folder. (Make sure you do a Project -> "Maven" -> "Update Project...") then rebuild...

Exclude application.properties when generating war using spring boot and spring-boot-maven-plugin

I am developing a web application using Spring Boot, and want to generate war instead of jar.
It works very fine using the conversion from jar to war described here : http://spring.io/guides/gs/convert-jar-to-war/
But I want to exclude the application.properties from the war, because I use #PropertySource(value = "file:${OPENSHIFT_DATA_DIR}/application.properties") to get the file path on production environment.
This method works when generating my war, but in eclipse I can't run my application because application.properties not copied at all to target/classes :
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
</resources>
</build>
This method doesn't work at all, I think that spring-boot-maven-plugin doesn't support packagingExcludes :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/classes/application.properties</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Have you another suggestion?
Thanks
Try using the solution below. This will work:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you are using the above solution , while running the project in Eclipse IDE you may get error that the properties file is not found. To get rid of this you need to add the resources folder in Run as configuration.(Run configurations... -> Classpath -> User Entries -> Advanced... -> Add Folders)
When running in Eclipse, at your Run Configuration, you need to specify the path of the propeties to Spring Boot:
--spring.config.location=${workspace_loc:/YOURPROYECTNAME}/src/main/resources/
The solution I added is to unzip my packaged war, delete the file application.properties and create a new war named ROOT.war using maven-antrun-plugin and run some ant tasks.
this is what i added to my plugins in pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<configuration>
<target>
<unzip src="target/${artifactId}-${version}.${packaging}" dest="target/ROOT/" />
<delete file="target/ROOT/WEB-INF/classes/application.properties"/>
<zip destfile="target/ROOT.war" basedir="target/ROOT" encoding="UTF-8"/>
<delete dir="target/ROOT"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I named my target war as ROOT.war because I am using tomcat on openshift PaaS, so I just copy my ROOT.war and push to my openshift repo. That's it
What I understand from your question is, you want to use application.properties for your development. But you dont want to use it for production.
I would suggest using Spring profiles to achieve this.
In your properties file
Create a profile for development. Put all your development properties under it.
Do not create a profile for production in your properties file.
When you are developing, set active profile to development, so that the properties are loaded from your application.properties file.
When you run it in production, set active profile to Production. Though application.properties will be loaded into your WAR, since there is no profile for production, none of the properties will be loaded.
I have done something similar using YML. I am sure there must be a way to do the same thing using .properties file too.
spring:
profiles.active: development
--
spring:
profiles: development
something:
location: USA
unit1: Test1
unit2: Test2
You could change the profile in run time using
-Dspring.profiles.active=production
Try to using this solution:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.version}</version>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
<addResources>false</addResources> will keep properties when you run mvn spring-boot:run

Categories