I'm often using Jetty Maven plugin to execute my web applications right from the build. It provides an easy and operating system independent way to execute the web app. You only have to check out the source code from version contol, execute Maven build and start Jetty with Maven command. What could be easier way to run your web application in development environment?
I want to point out the following benefits in using Maven Jetty plugin
- IDE independent way to execute web application. Works on Eclipse, IDEA, NetBeans and whatever
- Support hot code replace: you can change code inside methods without restarting the whole application
- Works on Windows, Linux and Mac or any other Java compatible operating system. No need to setup the application server instance
- Starts fast and it's easy to reload the application to the server after changes
Just add the XML snippet in the end of this post to your pom.xml and execute Maven with mvn jetty:run. As a result, you will get response from your web app in http://localhost:8180/example/. You can run the same goal from your favorite IDE and, thus, you get an IDE independent web app execution!
In case you want to try hot code replace, add the classpaths containing your code inside the extraclasspath-element. Then start the Maven build running Jetty in debug mode and connect to the debugging session with your favorite IDE. The easiest way to achieve this in Eclipse is to run the Maven build in debug mode and then connect to the process with Eclipse debugger. After Eclipse is properly connected to the Maven process running Jetty, all the code changes in method bodies are instantly visible in the running process (of course the same code has to be modified by Eclipse so that the changes get to the classpath of Jetty).
If you prefer doing things in IDE, at least Eclipse has excellent Jetty plugin called Run Jetty Run (http://code.google.com/p/run-jetty-run/).
<build>
<plugins>
...
<plugin>
<groupid>org.mortbay.jetty</groupid>
<artifactid>jetty-maven-plugin</artifactid>
<version>7.5.2.v20111006</version>
<configuration>
<stopport>9966</stopport>
<stopkey>${project.artifactId}<stopkey>
<!-- scanning is not used if reload is set to manual -->
<scanintervalseconds>5</scanintervalseconds>
<!-- application reloading by pressing enter in the console -->
<reload>manual</reload>
<webappconfig>
<contextpath>/example</contextpath>
<!-- Changes in these classes will be instantly applied to running Jetty process without restart -->
<extraclasspath>target/classes;../dependant-project/target/classes;../another-dependant-project/target/classes</extraclasspath>
</webappconfig>
<!-- directories whose changes cause automated Jetty context reloading, not used if reload is manual -->
<scantargets>
<scantarget>../dependant-project/target/classes</scantarget>
</scantargets>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8180</port>
<maxidletime>60000</maxidletime>
</connector>
</connectors>
<systemproperties>
<!-- system properties that are used for running Jetty -->
<systemproperty>
<name>some.system.property</name>
<value>somevalue</value>
</systemproperty>
</systemproperties>
</configuration>
<dependencies>
<!-- dependencies added to Jetty's classpath -->
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>${log4j.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
...
</plugins>
</build>
Ei kommentteja:
Lähetä kommentti