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

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.

Related

tomcat-maven-plugin in GWT project. What is the difference between: org.codehaus.mojo and org.apache.tomcat.maven plugin

I am trying to set up tomcat server for gwt application. I would like to configure server to update my server side code changes immediately.
While looking for some helpful examples over the web, I found that there are to plugins for maven.
One from:
org.codehaus.mojo (which also provide plugin for gwt in maven echosystem).
And second from: org.apache.tomcat.maven.
What is the difference between them? Which one should I choose for:
GWT 2.7 Maven Project. I will develope app in Eclipse IDE. So I would like to have good integration with it to. F.e.: the Eclipse tab/view "Servers".
From https://tomcat.apache.org/maven-plugin-2.2/
This is the new home for the Tomcat Maven Plugin (previously hosted at Codehaus).
The CodeHaus Mojo (now MojoHaus) Tomcat Maven Plugin is obsolete.
I have Maven archetypes that use the Tomcat Maven Plugin to fire up a server for development, with automatic redeployment of the webapp when classes change, at https://github.com/tbroyer/gwt-maven-archetypes
Note that they use a different Maven Plugin for GWT than the one from MojoHaus (ex-CodeHaus Mojo), one that works much better with multi-module builds.
I've never used Eclipse WTP though (tried it and had too much trouble, probably because I didn't really know how to use it properly though) so I can't really comment, but I see no reason why it wouldn't work.

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.

What is the correct maven2 phase for deployment into application server?

I'm trying to configure pom.xml so that it automatically deploys EAR archive to GlassFish application server. I want to attach this operation to the proper maven execution phase. But can't understand which one is dedicated just for this operation? Deploy? Install? Please, help. This is what I'm doing:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<tasks>
<copy file="${project.build.directory}/${project.build.finalName}.ear"
tofile="${glassfish.home}/domains/domain1/autodeploy"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
When I'm doing mvn deploy, maven is trying to deploy my artifacts to repository. This is not what I'm going to accomplish. I feel that the execution phase is wrong..
When I'm doing mvn deploy, maven is trying to deploy my artifacts to repository. This is not what I'm going to accomplish. I feel that the execution phase is wrong...
In the Maven lingua, deploy has nothing to do with the deployment to an application server and is not an appropriate phase to bind a plugin doing this kind of work. Here is what we can read about the deploy phase in the Introduction to the Build Lifecycle:
deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
But, before I go further with phases, I need to mention that there are several plugins allowing to interact with GF (start/stop/deploy/undeploy/etc) that might do a better job than the AntRun plugin (AntRun might work for trivial use cases but, for example, you might want to wait for the deployment to be done and the application to be in a ready state during the build; for such use cases, you need more advanced control). These candidates are:
the Maven Glassfish Plugin: this is a GlassFish specific plugin that can be used with a local or remote GlassFish install.
the Maven Embedded GlassFish Plugin: this is a GlassFish specific plugin that runs an embedded GlassFish. Nice for portable builds.
the Maven Cargo Plugin: this a container agnostic plugin that now supports GlassFish 3.
Using one or the other really depends on your use case. If you don't plan to deploy on many containers, the GlassFish specific plugins are the most powerful. The beauty of Cargo is that it offers an unified API. But its configuration is less intuitive, especially if you're not used to it.
Now, if you just want to deploy an application during development and don't want the build to interact in any way with the container, binding any of these plugins to a particular phase is not that useful, although some people do deploy their app during package.
However, you might want to run integration/functional tests against a container as part of you build. This is actually a perfectly valid and very common use case and the relevant phases to implement this are:
pre-integration-test: perform actions required before integration
tests are executed. This may involve
things such as setting up the required
environment.
integration-test: process and deploy the package if necessary into an
environment where integration tests
can be run.
post-integration-test: perform actions required after integration
tests have been executed. This may
including cleaning up the environment.
The pre-integration-test phase is typically used to start a container and deploy an application on it. The post-integration-test phase is used to undeploy an application and stop the container.
So I think that deploying to a server can be a typical build activity, there are very valid use cases, and this is well supported by Maven. I don't deploy to my development server (nor to production server) as part of a build though.
See also
Maven Embedded Glassfish Plugin
Which Maven Glassfish plugin to use?
In addition to scdef's answer, here's a brief example of what using the cargo plugin looks like: http://blank.jasonwhaley.com/2010/03/automated-deployment-with-cargo-drive.html. I personally didn't bind it to a phase, since I don't want deployment to happen on every invocation of maven and didn't intend on writing another pom/profile to handle invoking the plugin.
Additionally, I'd recommend not using maven at all to deploy my applications to stable environments where there are almost always going to be other applications/databases/systems in the environment that need to be altered in addition placing the application in the container. Production almost always falls under this scope. Coordinating a .war/.ear/whatever deployment to a container/server under this context really should be decoupled from actually building your application. Leave deployments like this to external scripts or perhaps a comprehensive tool like Puppet.
It might not be a direct answer to your question, but have a look at the cargo plugin: http://cargo.codehaus.org/
It addresses this exact need (among other things)
I've implemented that behavior using a copy task with the ant plug-in on maven.
The correct phase for doing this is the package phase.
See http://maven.apache.org/plugins/maven-antrun-plugin/index.html for more details.
Regards.

Categories