Maven

 
 
Maven is a Java project management and project comprehension tool. It's primary goal is to make the developers life easier by providing a well defined project structure, well defined development processes to follow, and a coherent body of documentation that keeps your developers and clients apprised of what's happening with your project.

Maven is distributed at no charge for commercial or non-commercial use and is licensed under the Apache License, Version 2.0.

More information about Maven can be found at: http://maven.apache.org

The latest Maven version can be downloaded from: http://maven.apache.org/download.html







Maven 3.0.4: Setup web application with Jetty server



Information
Before you can create a Maven project you need to have internet access because Maven automatically downloads dependencies (= JAR files) from remote central repositories.

Operating system used
Windows Vista Home Premium SP 2

Software prerequisites
Maven 3.0.4

Procedure
  1. First install Maven 3.0.4

  2. Open a Windows Terminal (Command prompt or cmd), right click the title bar and select "Properties".

    Windows terminal properties

  3. Select tab Layout and change "Screen Buffer Size" height to 9999.
    Now you can scroll up to see the previous loggings in the Windows Terminal.

    Windows terminal screen buffer size

  4. Create a Maven project folder.
    For example: c:\mymockapi

  5. Goto this folder.
    Type: cd mymockapi

  6. Generate a new project from an archetype.
    Type: mvn archetype:generate

    You will see a long list of archetypes, numbered 1, 2, 3 etc.. which you can select.

  7. Search for a J2EE 5 web application archetype.
    In this example 408 is the archetype we want. Enter in the last line the number 408 and press Enter.

    :
    408: remote -> org.codehaus.mojo.archetypes:webapp-jee5 (-)
    :
    646: remote -> org.wikbook:wikbook.archetype (-)
    647: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-glassfish (-)
    648: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-spring (-)
    649: remote -> org.xwiki.commons:xwiki-commons-component-archetype
    (Make it easy to create a maven project for creating XWiki Components.)
    650: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro
    (Make it easy to create a maven project for creating XWiki Rendering Macros.)
    651: remote -> org.zkoss:zk-archetype-component (The ZK Component archetype)
    652: remote -> org.zkoss:zk-archetype-webapp (The ZK wepapp archetype)
    653: remote -> ru.circumflex:circumflex-archetype (-)
    654: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
    655: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
    656: remote -> tk.skuro:clojure-maven-archetype
    (A simple Maven archetype for Clojure)
    657: remote -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)
    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 225:
    408

    Note:
    See the complete maven archetype list.

  8. Select jee5 version:
    Choose org.codehaus.mojo.archetypes:webapp-jee5 version:
    1: 1.0
    2: 1.0.1
    3: 1.1
    4: 1.2
    5: 1.3

    Choose a number: 5:
    Enter

    Note:
    We have selected the webapp-jee5 version: 1.3. This has nothing to do with the java compiler version.

  9. Define the group Id:
    Define value for property 'groupId': :com.mobilefish.apps

  10. Define the artifact Id:
    Define value for property 'artifactId': :mockapi

  11. Define the version:
    Define value for property 'version': 1.0-SNAPSHOT:Enter

  12. Define the package:
    Define value for property 'package': com.mobilefish.apps: :
    com.mobilefish.apps.mockapi

  13. Confirm properties configuration:
    groupId: com.mobilefish.apps
    artifactId: mockapi
    version: 1.0-SNAPSHOT
    package: com.mobilefish.apps.mockapi
    Y: :Enter


  14. You should see:

    [INFO] ------------------------------------------------------------------------
    [INFO] Using following parameters for creating project from Archetype:
    webapp-jee5:1.3
    [INFO] ------------------------------------------------------------------------
    [INFO] Parameter: groupId, Value: com.mobilefish.apps
    [INFO] Parameter: artifactId, Value: mockapi
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: package, Value: com.mobilefish.apps.mockapi
    [INFO] Parameter: packageInPathFormat, Value: com/mobilefish/apps/mockapi
    [INFO] Parameter: package, Value: com.mobilefish.apps.mockapi
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: groupId, Value: com.mobilefish.apps
    [INFO] Parameter: artifactId, Value: mockapi
    [INFO] project created from Archetype in dir: c:\mymockapi\mockapi
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:00:21.732s
    [INFO] Finished at: Tue Oct 30 20:39:29 CET 2012
    [INFO] Final Memory: 7M/16M
    [INFO] ------------------------------------------------------------------------




  15. See the initial created maven pom.xml file.

  16. Go to directory where the pom.xml is located. Type: cd c:\mymockapi\mockapi

  17. Create the web applcation war file.
    Type: mvn package

  18. The dependencies from the pom.xml are downloaded.
    You will see:

    Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/
    2.4.1/maven-archiver-2.4.1.jar (20 KB at 38.1 KB/sec)
    Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-fi
    ltering/1.0-beta-2/maven-filtering-1.0-beta-2.jar (33 KB at 119.1 KB/sec)
    Downloaded: http://repo.maven.apache.org/maven2/com/thoughtworks/xstream/xstream
    /1.3.1/xstream-1.3.1.jar (422 KB at 564.7 KB/sec)
    [INFO] Packaging webapp
    [INFO] Assembling webapp [mockapi] in [c:\mymockapi\mockapi\target\mockapi-1.0-S
    NAPSHOT]
    [INFO] Processing war project
    [INFO] Copying webapp resources [c:\mymockapi\mockapi\src\main\webapp]
    [INFO] Webapp assembled in [34 msecs]
    [INFO] Building war: c:\mymockapi\mockapi\target\mockapi-1.0-SNAPSHOT.war
    [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ig
    nored
    (webxml attribute is missing from war task, or ignoreWebxml attribute is specifi
    ed as 'true')
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 17.362s
    [INFO] Finished at: Tue Oct 30 20:45:58 CET 2012
    [INFO] Final Memory: 4M/8M
    [INFO] ------------------------------------------------------------------------


  19. To deploy a web application (war) you need a servlet container, for example a Jetty Server.
    Jetty is a lightweight servlet container.
    Add a Jetty server plugin in the pom.xml file:

    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.26</version>
       <configuration>
          <scanIntervalSeconds>5</scanIntervalSeconds>
       </configuration>
    </plugin>


    Note 1:
    To find the correct Jetty server groupId, artifactId and version:
    • Goto: http://mvnrepository.com
    • In the search box enter: org.mortbay.jetty
    • Select link: Server Core, The core jetty server artifact.
    • Select the last release version and click the version number
    • You will see:

      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>maven-jetty-plugin</artifactId>
         <version>6.1.26</version>
      </plugin>

    Note 2:
    The scanIntervalSeconds configuration has been added.
    This will hot deploy the war file when the Java files are changed.
    Changes in jsp files are seen immediately without the need to restart the Jetty server.

    Note 3:
    Plugins extends the maven functionality.

    Note 4:
    In the maven-compiler-plugin, the J2EE versions are defined.

    <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>


    In Maven 2 the default compiler plugin is: J2EE 1.4 (source and target)
    In Maven 3 the default compiler plugin is: J2EE 1.5 (source and target)

  20. Start Jetty server.
    Goto the directory where the pom.xml file is located.
    Type: mvn jetty:run

    Note:
    The maven command meaning: mvn <plugin name>:<what the plugin should do>

  21. Jetty dependencies are downloaded.
    You will see:

    :
    Downloaded: http://repo.maven.apache.org/maven2/org/eclipse/jdt/core/3.1.1/core-
    3.1.1.jar (3484 KB at 483.3 KB/sec)
    [INFO] Configuring Jetty for project: mockapi
    [INFO] Webapp source directory = C:\mymockapi\mockapi\src\main\webapp
    [INFO] Reload Mechanic: automatic
    [INFO] Classes directory C:\mymockapi\mockapi\target\classes does not exist
    2012-10-30 21:14:35.282:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
    [INFO] Context path = /mockapi
    [INFO] Tmp directory = determined at runtime
    [INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
    [INFO] Web overrides = none
    [INFO] web.xml file = C:\mymockapi\mockapi\src\main\webapp\WEB-INF\web.xml
    [INFO] Webapp directory = C:\mymockapi\mockapi\src\main\webapp
    [INFO] Starting jetty 6.1.26 ...
    2012-10-30 21:14:35.555:INFO::jetty-6.1.26
    2012-10-30 21:14:35.853:INFO::No Transaction manager found - if your webapp requ
    ires one, please configure one.
    2012-10-30 21:14:36.179:INFO::Started [email protected]:8080
    [INFO] Started Jetty Server


  22. The Jetty Server is started. By default port 8080 is used. To access the web page, open a browser.
    Type: http://localhost:8080/mockapi

    You should see:

    Hello world

  23. In this example the web application context is mockapi. It is the artifact Id you specified earlier.

    :
    <groupId>com.mobilefish.apps</groupId>
    <artifactId>mockapi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    :


    There are two ways to change the web application context.

    Method 1:
    Changing the artifactId (see above) in the pom.xml file.

    Method 2:
    Add the following line is the pom.xml file.

    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.26</version>
       <configuration>
          <scanIntervalSeconds>5</scanIntervalSeconds>
          <contextPath>/mb</contextPath>
       </configuration>
    </plugin>


  24. To change the default port number to something else, for example to 9080, add the following lines:

    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       :
       <configuration>
       <connectors>
          <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
             <port>9080</port>
             <maxIdleTime>60000</maxIdleTime>
          </connector>
       </connectors>
       :
       </configuration>
    </plugin>


  25. The Jetty Server default port number is 8080. To change this port number to something else, for example to 9080, add the following lines:

    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       :
       <configuration>
       <connectors>
          <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
             <port>9080</port>
             <maxIdleTime>60000</maxIdleTime>
          </connector>
       </connectors>
       :
       </configuration>
    </plugin>


  26. There are two ways to stop the Jetty Server.

    Method 1:
    Press: CTRL+C in the same terminal window where the Jetty Server is started.

    Method 2:
    Configure the Jetty plugin with a special port number and key.

    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.26</version>
       <configuration>
          :
          <stopPort>9966</stopPort>
          <stopKey>foo</stopKey>
       </configuration>
    </plugin>


    Now, to stop the Jetty Server, open a new terminal window and goto the directory where the pom.xml file is located.
    Type: mvn jetty:stop

  27. Place the following 3 files in their corresponding locations:

    • File:web.xml
      Location: C:\mymockapi\mockapi\src\main\webapp\WEB-INF

    • File:demo.html
      Location: C:\mymockapi\mockapi\src\main\webapp

    • File:HelloWorldServlet.java
      Location: C:\mymockapi\mockapi\src\main\java\com\mobilefish\apps\mockapi

  28. If you now start Jetty: mvn jetty:run
    the following error message is displayed:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.
    0.2:compile (default-compile) on project mockapi: Compilation failure
    [ERROR] c:\mymockapi\mockapi\src\main\java\com\mobilefish\apps\mockapi\HelloWorl
    dServlet.java:[26,40] cannot find symbol
    [ERROR] symbol : method getServletContext()
    [ERROR] location: interface javax.servlet.http.HttpServletRequest
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
    ch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please rea
    d the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc eption


    The problem is caused by the HelloWorldServlet.java file.
    javax.servlet.ServletContext is not supported by Servlet 2.5. It is supported by Servlet 3.0.

    To solve this problem, change the pom.xml file.

    Replace:

    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
       <scope>provided</scope>
    </dependency>


    With:

    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.0.1</version>
       <scope>provided</scope>
    </dependency>


  29. Start Jetty server.
    Goto the directory where the pom.xml file is loacted.
    Type: mvn jetty:run

  30. Open a browser and enter the following URLs:
    • http://localhost:9080/mb/demo.html
    • http://localhost:9080/mb/HelloServlet?firstname=robert&lastname=lie

  31. The final maven pom.xml file as created by this tutorial.
    Download the complete project (zip file) including all sources and xml files.