Friday, March 16, 2012

Maven - WAR Archetype

Let us now see another useful archetype - one which makes it easy to create web applications (WARs)

Go to the command prompt, cd to workspace-confucius and type:
> mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp

If we already know the archetype we want, we can specify it directly on the command line using -DarchetypeGroupId and -DarchetypeArtifactId - this way we do not need to see the entire list of 100s of available archetypes.

You can also specify a -DarchetypeVersion if you want to specifically use a certain version of the archetype. By default, the latest version will be chosen by Maven.

When prompted for:
groupId, specify org.confucius
artifactId, specify HelloWorldWARArch

Everything else, use defaults.
 C:\Users\mlavannis\workspace-confucius>mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp  
 [INFO] Scanning for projects...  
 [INFO]  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Building Maven Stub Project (No POM) 1  
 [INFO] ------------------------------------------------------------------------  
 [INFO]  
 [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>  
 [INFO]  
 [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<  
 [INFO]  
 [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---  
 [INFO] Generating project in Interactive mode  
 [INFO] Archetype [org.apache.maven.archetypes:maven-archetype-webapp:1.0] found in catalog remote  
 Define value for property 'groupId': : org.confucius  
 Define value for property 'artifactId': : HelloWorldWARArch  
 Define value for property 'version': 1.0-SNAPSHOT: :  
 Define value for property 'package': org.confucius: :  
 Confirm properties configuration:  
 groupId: org.confucius  
 artifactId: HelloWorldWARArch  
 version: 1.0-SNAPSHOT  
 package: org.confucius  
  Y: :  
 [INFO] ----------------------------------------------------------------------------  
 [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0  
 [INFO] ----------------------------------------------------------------------------  
 [INFO] Parameter: groupId, Value: org.confucius  
 [INFO] Parameter: packageName, Value: org.confucius  
 [INFO] Parameter: package, Value: org.confucius  
 [INFO] Parameter: artifactId, Value: HelloWorldWARArch  
 [INFO] Parameter: basedir, Value: C:\Users\mlavannis\workspace-confucius  
 [INFO] Parameter: version, Value: 1.0-SNAPSHOT  
 [INFO] project created from Old (1.x) Archetype in dir: C:\Users\mlavannis\workspace-confucius\HelloWorldWARArch  
 [INFO] ------------------------------------------------------------------------  
 [INFO] BUILD SUCCESS  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Total time: 19.416s  
 [INFO] Finished at: Fri Mar 16 23:42:55 EDT 2012  
 [INFO] Final Memory: 7M/13M  
 [INFO] ------------------------------------------------------------------------  
 C:\Users\mlavannis\workspace-confucius>  

You can now bring this project into Eclipse (command>mvn eclipse:eclipse), build it using m2eclipse and it will generate HelloWorldWARArch.war (in the /target folder)

If you deploy this war to Apache Tomcat, and point the browser to:
http://localhost:8080/HelloWorldWARArch/

you will see "Hello World!"

You can now write your own Servlets and supporting Java classes in /src/main/java.
You can write junit tests in /src/test/java.

Your project is ready to generate the war.

Take home point: By using a Maven archetype and following its conventions, we have made our project building configuration-free.

No comments: