Implement tomcat in java to deploy war - java

I am trying to deploy my war using tomcat (implement in java) before running my functional test. Initially I implemented Jetty Server in java and I successfully exposed the URI and I could execute my test. But I want to deploy my war to test it and not just expose the URI,I can obviously use jetty plugin and cargo in my pom file, but I want to use tomcat to do so. I thoroughly searched online, I found sample code on deploying war using tomcat implementation in java [1], so that I can start tomcat, deploy my war and then run my functional test. But it is not clearly explained. Can I refer to any good documentation online which will help me with deploying my war using tomcat?
[1] http://www.programcreek.com/java-api-examples/index.php?api=org.apache.catalina.startup.Tomcat

Depending on your level of testing the recommended maven approach would be one of the following
For unit tests
Put any startup of maven and possibly deployment in the pretest phase
process-test-classes
Then run the tests in the test phase
test
This approach will work if the unit tests themselves need to be deployed to tomcat to run small parts of the code base note there is no way here to shutdown tomcat you will need to find a phase to bind the shutdown to.
For integration tests that need to run the whole war file Put any startup of maven and possibly deployment in the pre-test phases
pre-integration-test
Run the test in the test phase
integration-test
Undeploy and shutdown during the post test phase
post-integration-test
Under this scenario the link you included in your question becomes relevant.

Related

What is the enterprise use of the Jetty plugin in Maven?

Since I learn from some maven tutorials we can import Jetty as a maven plugin like this.
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
</configuration>
</plugin>
And can run the plugin like this.
$ mvn jetty:run
Also we can change the port and context path and lots of stuff in this plugin.
As I understood that we can use Jetty as a server like tomcat, and we can deploy an application through it.
But the thing I don't understand is what is the actual enterprise use of Jetty in maven..
From the official documentation:
The Jetty Maven plugin is useful for rapid development and testing. You can add it to any webapp project that is structured according to the Maven defaults. The plugin can then periodically scan your project for changes and automatically redeploy the webapp if any are found. This makes the development cycle more productive by eliminating the build and deploy steps: you use your IDE to make changes to the project, and the running web container automatically picks them up, allowing you to test them straight away.
However (and maybe this addresses what you call "enterprise use"):
While the Jetty Maven Plugin can be very useful for development we do not recommend its use in a production capacity. In order for the plugin to work it needs to leverage many internal Maven apis and Maven itself it not a production deployment tool. We recommend either the traditional distribution deployment approach or using embedded Jetty.
The main usage is for testing, Jetty can also be started programmatically (see this example Java code) which means you can start server directly from your code and interact with your REST API for instance.
You can also use it for easier deployment of small applications, just package everything into the JAR which runs server from main method when executed via java -jar your-app.jar. You don't need any dependencies installed except Java then.
As a side note, I currently work in Clojure (JVM language based on Lisp) and many people deploys their application as JAR which internally runs embedded Jetty because this way it's also starts REPL which you can connect to remotely and debug your application when it's running.
I don't know exactly what you mean by enterprise use, but let's say you're developing a web application and it's a Maven project.
Each time you want to test whether the web application works correctly, you need to deploy the web archive (WAR) on a web server, e.g. Jetty or Tomcat. Usually this involves a couple of manual steps like:
Start the web server
Deploy the WAR on it
Where the Maven plugin comes in handy is that it allows you to just execute
mvn jetty:run-war
and it does all these steps automatically for you in a single command, saving you lots of time. The plugin is even able to redeploy the application once it notices changes have been made.

OSGI Integration test in karaf container

I am new to Karaf IT testing within the karaf container. I created a small practice project
Practice OSGI project. All the bundles are running fine as I am able to run them in karaf container(3.0.8) and able to make a rest hit to manually test it out. But Now I am trying to test it via integration tests. I wrote a testcase as well, but when I try to execute it, the maven says, no tests to build. Here my objective is to run a simple command like mvn clean install or mvn clean test so that it builds the complete project and in end test it using integration test(deploy karaf container, deploy bundles and execute the test cases, and bring the container down).
I hope I am able to explain my objective and question well.
Any help will be greatly appreciated.
Your test code is in the wrong directory. It is in src/test/main/java while it should be in src/test/java.
After this change the test runs but produces an exception. So there is more to fix but I guess this already helps you a bit.
Btw. I saw that your maven projects all have version 0.1 this is not a good idea in maven. You should always use a SNAPSHOT version. Release version should only be built and deployed once as they can not be overwritten in a remote maven repo.

Maven: running Unit-Tests remotely

We are currently working on a distributed Java EE-Application and have therefore a separated test and production system.
Compiling and Bundling is done via an Ant-Task. Now we want to deploy the Jar-Files of the different servers to the test-servers and run the JUnit Integration / Function-Tests there. If they succeed, then the current version should be deployed to the live-servers.
Plain Unit-Tests are executed by Hudson.
Is that possible with Maven and is there any information or best practice available?
Yes. Hudson has maven integration. Take a loot this wiki and this link.
You can set unit test case thresholds for your job to see if it does not pass a certain number of test cases. In that the deploy plugin will not get invoked and the app will not get deployed.
Take a JAR built from Ant and reuse it. I would add a Maven repository to your environment such as Artifactory, Archiva, or Nexus and deploy to that using Ivy. You almost certainly need to use a Maven repository to be happy with Maven for anything other than small scale personal projects. http://ant.apache.org/ivy/
Use Maven to grab the JAR from the Maven Repository. For this, just use a normal Maven dependency declaration.
Run Maven on the QA server, with the JUnit tests declared in that project. If that succeeds, deploy the JAR to the production server. For this, the details depend on the production server. If it's a WAR, I would use Cargo, but if it's a JAR it really depends on what's executing the JAR - you might need some sort of file copy, scp, etc. http://cargo.codehaus.org/
Hudson and TeamCity both have deployment features as well. You just set up a job to run (in this case the Maven job) and tell the CI server to deploy on success.

What is an alternative to jetty-maven-plugin?

Is there any workable alternative to jetty-maven-plugin? This particular plugin is not supported properly. I even failed to find its source code repository.
What I need is an embedded container for integration testing.
There is a very similar Tomcat Maven Plugin. Works exactly the same, except... it uses Tomcat. You might also try Maven Cargo plugin that uniforms deploy process to several different containers/servers.
There's an embedded-glassfish plugin that can be used for this.
I often use an embedded jetty server that is started in the #beforeClass part of an junit test. That embedded jetty along with openejb provides most things you need without having to deploy your application anywhere

Maven tomcat plugin

is it possible to run spring mvc application on tomcat(with this plugin http://mojo.codehaus.org/tomcat-maven-plugin/) if yes, where is the application log and deploy web app directory located ?
Let me rephrase the question. I'm asking for some way to start my web app(spring mvc application) from a command line, either with maven or with something inside application that creates web server.
What would you recommend? I found this tomcat maven plugin while googling, no special reason to use it
Currently I use jboss server for this application, but I want to run both server and application from application,any suggestions?
tomcat:run runs a web application in an embedded tomcat instance ... useful for testing during development.
See http://mojo.codehaus.org/tomcat-maven-plugin/run-mojo.html for listing of config parameters for the run goal, including warSourceDirectory which, if not specified defaults to ${basedir}/src/main/webapp
I'm not sure exactly why but we use the mvn cargo:start plugin instead, for testing locally when building with maven. We also utilize Spring and haven't had any issues regarding the mix. When starting up the Tomcat container with the command above it shows the following in the console
C:\SVN\myproject-war\target\tomcat6x\home>
This contains the webapps folder where the war is deployed and a logs folder.
Hope this helps, sorry I don't have direct experience with the tomcat-maven-plugin itself.
If I understand your question correctly, from your application you want to start tomcat to serve a spring MVC application.
If so, you do not need maven or a maven plugin to do this. You can use tomcat catalina APIs to achieve this. Though dated, this article explains how.
As the other answers stated, maven's tomcat and cargo plugins allow you to start and stop tomcat from maven - typically to run some integration tests.

Categories