Sample Maven Webapp Project

Sample Webapp Project

Generate Project

* Generate sample webapp by choosing archetype174.
* Note that the numbers do change day by day so I ran command mvn archetype:generate > mvn_archetypes.txt to find the exact number.

mvn archetype:generate
 
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 171: 174
Choose org.apache.maven.archetypes:maven-archetype-webapp version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
Choose a number: 5:
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
 
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
 
Define value for property 'groupId': : test.maven.one
Define value for property 'artifactId': : sample-webapp
Define value for property 'version': 1.0-SNAPSHOT:
Define value for property 'package': test.maven.one:
Confirm properties configuration:
groupId: test.maven.one
artifactId: sample-webapp
version: 1.0-SNAPSHOT
package: test.maven.one
Y:
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: test.maven.one
[INFO] Parameter: packageName, Value: test.maven.one
[INFO] Parameter: package, Value: test.maven.one
[INFO] Parameter: artifactId, Value: sample-webapp
[INFO] Parameter: basedir, Value: C:\Users\jli
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\jli\sample-webapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 minutes 23 seconds
[INFO] Finished at: Mon Feb 20 17:04:32 EST 2012
[INFO] Final Memory: 22M/215M
[INFO] ------------------------------------------------------------------------

* sample-webapp folder generated

* Generated pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.maven.one</groupId>
  <artifactId>sample-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sample-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>sample-webapp</finalName>
  </build>
</project>

* Run package:

cd sample-webapp
mvn package

target folder is generated for all packaging artifacts:

* Run install: copy war file to local repository (while deploy phase copy war file to remote repository)

[INFO] [install:install {execution: default-install}]
[INFO] Installing C:\jml\demo\sample-webapp\target\sample-webapp.war to C:\Users\jli\.m2\repository\test\maven\one\sample-webapp\1.0-SNAPSHOT\sample-webapp-1.0-SNAPSHOT.war

Add a Java Source File

* Create package directories: test/maven/one
* Create a Java source file named HelloWorld.java:

package test.maven.one;
 
import org.apache.log4j.*;
 
public class HelloWorld {
  Logger log = LogManager.getLogger(HelloWorld.class);
 
  public String sayHello() {
    String hello = "Hello world!";
	  log.info(hello);
    return hello;
  }
}

* Create log4j.properties file in src\main\resources directory:

log4j.rootLogger=debug, stdout
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

* Add log4j dependency to pom.xml file:

   ...
   <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.14</version>
   </dependency>
 
  </dependencies>

* Do build. Note that
– Log4j dependency is automatically resolved
log4j-1.2.14.jar is automatically downloaded and included in WEB-INF/lib directory
– Java class is compiled

C:\jml\demo\sample-webapp>mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-webapp Maven Webapp
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
Downloading: http://repo1.maven.org/maven2/log4j/log4j/1.2.14/log4j-1.2.14.pom
 
Downloading: http://repo1.maven.org/maven2/log4j/log4j/1.2.14/log4j-1.2.14.jar
 
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to C:\jml\demo\sample-webapp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Mon Feb 20 17:51:20 EST 2012
[INFO] Final Memory: 9M/152M
[INFO] ------------------------------------------------------------------------

* Do package:

mvn package

Specify JDK 1.5 Dependency

* In pom.xml, use build plugin to specify using jdk1.5 for compiling:

  <build>
    <finalName>sample-webapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
  </build>

Enforce Dependencies

* Enforce that commons-logging not to be used.

    <plugin> 
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0-beta-1</version>
        <executions> 
            <execution>
                <id>enforce-dependencies</id>
                <goals> 
                    <goal>enforce</goal> 
                </goals>
                <configuration>
                    <rules>
                        <bannedDependencies>
                            <excludes>
                                <exclude>commons-logging:commons-logging</exclude>
                            </excludes>
                        </bannedDependencies>
                    </rules> 
                </configuration>
            </execution> 
        </executions>
    </plugin>
</plugins>
</build>

* You will see in the output message when you do mvn compile

[INFO] [enforcer:enforce {execution: enforce-dependencies}]

Enable Unit Testing

* Add a test library dependency for TestNG

    <dependency> 
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>5.8</version>
        <classifier>jdk15</classifier>
        <scope>test</scope>
    </dependency>

* Add a test Java class file named HelloWorldTest to test HelloWorld in src/main/test/java directory:

package test.maven.one;
 
import org.apache.log4j.*;
import org.testng.annotations.*;
 
public class HelloWorldTest {
 
  Logger log = LogManager.getLogger(HelloWorldTest.class);
  private HelloWorld hw = new HelloWorld();
 
  @Test
  public void testSayHello() {
    log.info("testSayHello...");
 
    assert "Hello world!".equals(hw.sayHello());
  }
 
}

* Run mvn test

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
 INFO [main] (HelloWorldTest.java:13) - testSayHello...
 INFO [main] (HelloWorld.java:12) - Hello world!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.36 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] -----------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Feb 22 09:24:44 EST 2012
[INFO] Final Memory: 14M/215M
[INFO] -----------------------------------------------------------------

* Report also generated in surefire-reports directory:

Test Webapp with Jetty Servlet Container

* Add Jetty plugin to pom.xml

  <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
    <configuration>
      <stopPort>9966</stopPort>
      <stopKey>foo</stopKey>
    </configuration>
  </plugin>
    </plugins>
</build>

* Run Jetty with mvn jetty:run

[INFO] [jetty:run {execution: default-cli}]
[INFO] Configuring Jetty for project: sample-webapp Maven Webapp
[INFO] Webapp source directory = C:\jml\demo\sample-webapp\src\main\webapp
[INFO] web.xml file = C:\jml\demo\sample-webapp\src\main\webapp\WEB-INF\web.xml
[INFO] Classes = C:\jml\demo\sample-webapp\target\classes
2012-02-21 13:43:04.667::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /sample-webapp
[INFO] Tmp directory =  determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] Webapp directory = C:\jml\demo\sample-webapp\src\main\webapp
[INFO] Starting jetty 6.1.10 ...
2012-02-21 13:43:05.029::INFO:  jetty-6.1.10
2012-02-21 13:43:05.398::INFO:  No Transaction manager found - if your webapp re
quires one, please configure one.
2012-02-21 13:43:06.115::INFO:  Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

* View page from web browser:

* Stop Jetty with: mvn jetty:stop (or ctr-C)

C:\jml\demo\sample-webapp>mvn jetty:stop
[INFO] Scanning for projects...
[INFO] -----------------------------------------------------
[INFO] Building sample-webapp Maven Webapp
[INFO]    task-segment: [jetty:stop]
[INFO] -----------------------------------------------------
[INFO] Preparing jetty:stop
[INFO] [enforcer:enforce {execution: enforce-dependencies}]
[INFO] [jetty:stop {execution: default-cli}]
[INFO] -----------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Tue Feb 21 14:00:43 EST 2012
[INFO] Final Memory: 12M/218M
[INFO] -----------------------------------------------------

Generate Site Report

* Run mvn site
* Site report generated in target/site directory
* Double click index.html to view site:

References

* Apache Maven 2 Effective Implementation By: Brett Porter; Maria Odea Ching

This entry was posted in maven and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.