Here is "short" entry to tell you how to use maven to quickly and nicely set up a simple and standard web application ready for development debugging and unit testing
prerequisites:
You must have installed:
Create your project from a maven archetype
The maven archetype plugin allows the user to create a Maven 2 project from an existing template called an archetype.
Here is the one I used to create my simple web app.
mvn archetype:create -DgroupId=com.adobe.consulting.fr -DartifactId=ac_fr_samples -Dpackagename=com.adobe.consulting.fr -DarchetypeArtifactId=maven-archetype-webapp
Create your own sources and unit test folder structure
Those are default maven folder structure, if you follow that, you won't have anything more to configure in your build.
The main java source folder structure and packages
mkdir src/main/java/com/adobe/consulting/fr
The test java source folders structure and packages
mkdir src/test/java/com/adobe/consulting/fr
mkdir src/test/resources
lots of archetypes:
By the way, there are a lot of archetypes out there : here is a list http://docs.codehaus.org/display/MAVENUSER/Archetypes+List
Those archetypes can create a lot of stuff in advance for you, like a full hibernate, spring, struts project skeleton.
Wouldn't it be great to have others for:
- Adobe flex blazeDS/lcds project
Create your WTP eclipse project
The Eclipse plugin supports creating configurations for Eclipse WTP (Web Tools Project).
Projects with a WAR packaging can be setup as WTP dynamic web projects
and runtime dependencies are configured to be used when running them using Eclipse internal servers.
To generate WTP project for Eclipse Europa, use this command:
mvn -Dwtpversion=1.5 eclipse:eclipse
To configure your newly create eclipse project to use with the M2Elipse plugin.
mvn eclipse:m2eclipse -DdownloadSources=true -DdownloadJavadocs=true
Other neat options:
Now, just import this project in Eclipse
Install Jetty and deploy your web app with maven:
Edit your maven project object model
Edit your pom.xml to add the maven-jetty-plugin
<build>
<finalName>ac_fr_samples</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
Install Jetty and deploy your web app in one maven command
mvn -Djetty.port=9999 jetty:run
I chose 9999 as the web http port number, because I'm already running jboss on port 8080
Test your web app
Go to http://localhost:9999/ac_fr_samples/
Combine Jetty, maven and eclipse :
Let's Debug your web app with eclipse, maven and jetty
Stop the above jetty instance
Set up you jetty web app server as a runnable external tools:
Go to the Run/External Tools/External Tools ..." menu item on the "Run" menu bar.
Select "Program" and click the "New" button.
On the "Main" tab, fill in the "Location:" as the full path to your "mvn" executable.
For the "Working Directory:" select the workspace that matches your webapp. For "Arguments:" add jetty:run.
Move to the "Environment" tab and click the "New" button to add a new variable named MAVEN_OPTS with the value:
-Xdebug -Xnoagent -Djetty.port=9999 -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=9998,server=y,suspend=n
A few remarks about this:
- If you supply suspend=y instead of suspend=n, it will suspend the web server until the debugger is launched
- I chose 9999 as the web http port number, because I'm already running jboss on port 8080
- I chose port 9998 as the debug port number cause it was already used as well, I faced a ""ERROR: transport error 202: bind failed: Address already in use"
You may start jetty this way.
Deploy and Debug your project through eclipse launching maven jetty
Pull up the "Run/Debug/Debug ..." menu item and select
"Remote Java Application" and click the "New" button.
Fill in the dialog by selecting your webapp project for the "Project:"
field, and ensure you are using the same port number as you specified in the address= property above.
You may now Run/Debug your project.
Monitor the quality of your project
Lots of plugin out there.
Here is the first few that don't cost a penny to set up and give quite a few indicators.
Add this to your pom.xml:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.1</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>
http://svn.apache.org/repos/asf/struts/maven/trunk/build/struts_checks.xml
</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.sf.dtddoc</groupId>
<artifactId>dtddoc-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/web-app*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</reporting>
To generate the associated reports and the project site, just use:
and you'll get this for free (as in free software, and free beer
)
Alternatives : use Jboss or tomcat :
What you did with the very fast and light jetty web server, you may also do it with the full J2EE beast Jboss
Edit your pom.xml to add the maven-jboss-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<hostName>localhost</hostName>
<serverName>all</serverName>
<port>8080</port>
</configuration>
</plugin>
It will allow you to deploy through maven (and jmx) your web application build by maven:
The same goes with Tomcat: you also have a maven tomcat plugin.
References:
maven:
Web application, eclipse wtp, jetty and maven :