Work with Maven: Create a Project Skeleton

Source: Internet
Author: User

Maven has always been used for project construction. It is easier to use Maven than ant. At first, I chose Maven to do not want to spend more energy on the ant build script. However, after practice, I chose
Correct. Does it save time and energy? It turns out that I didn't save much time because I chose maven. It is worthwhile to spend more time and energy on building a building tool, however, Maven
My return far exceeds my expectation.

What can Maven do? Basically, ant can do Maven, and it can do better in many places. Why do we say this? I will not talk about theory, but I will prove it with facts !.
Maven can be seen in struts2, webwork2, spring2, appfuse2, and other components. Another major characteristic is that it relies on the package management system,
This concept is not a new concept. Debian/Ubuntu and ruby-Gems all adopt similar ideas to manage dependency packages/libraries.

After familiarizing yourself with the maven2 project configuration file, you can compile, test, and package a complete web project without writing a line of build scripts, clean up and other tasks, but if you want Maven to do more, follow me...

First, start with a simple web project. Assume that the project name is myweb, JDK or later, and the Linux system (the use method on XP is the same, but the xp cmd is too bad, it is easy to use without the Linux console, or I am too fond of using the XP advanced cmd command line interface ).
1) install Maven, the installation process is omitted, simply say it is the same as installing ant, the official website http://maven.apache.org

2) create a Project Skeleton, open a Linux console command line window, and enter the following command:

Java code
  1. MVN archetype: Create/
  2. -Dgroupid = com. mycompany. webapp/
  3. -Dartifactid = myweb/
  4. -Darchetypeartifactid = Maven-Archetype-webapp


A brief explanation:
Archetype is a built-in plug-in. Its Create task will create a Project Skeleton.
Type of the archetypeartifactid Project Skeleton

Available Project Skeleton:

* Maven-Archetype
* Maven-archetype-j2ee-simple
* Maven-Archetype-mojo
* Maven-Archetype-Portlet
* Maven-Archetype-profiles (currently under development)
* Maven-Archetype-Quickstart
* Maven-Archetype-simple (currently under development)
* Maven-Archetype-site
* Maven-Archetype-site-simple, and
* Maven-Archetype-webapp

The Java Package Structure of the groupid project, which can be modified
The name of the artifactid project. You can modify the name of the generated project directory.

A myweb folder is generated in the current directory. This file is our project folder.

3) Complete the directory structure of the project, because the generated Project Skeleton is a webapp that minimizes the skeleton structure, and there is still a gap with the standard Maven project layout,
For the maven standard project layout structure, see the http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

In fact, you just need to manually create several directories. The following command is all done.

Java code
  1. Mkdir-P src/{main/Java/COM/mycompany/webapp, test/Java/COM/mycompany/webapp, test/resources}


4) add an embedded Servlet/JSP Container jetty6 and use NiO to rewrite the Embedded Server. It is very convenient to develop and debug web projects. Here, you need to edit the unique configuration file pom. XML is actually not difficult

XML Code
  1. <
    Project
     
    Xmlns
    =
    Http://maven.apache.org/POM/4.0.0"
     
    Xmlns: xsi
    =
    Http://www.w3.org/2001/XMLSchema-instance"

  2. Xsi: schemalocation
    =
    Http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    >

  3. <
    Modelversion
    >
    4.0.0
    Modelversion
    >


  4. <
    Groupid
    >
    Com. mycompany. webapp
    Groupid
    >


  5. <
    Artifactid
    >
    Myweb
    Artifactid
    >


  6. <
    Packaging
    >
    War
    Packaging
    >


  7. <
    Version
    >
    1.0-Snapshot
    Version
    >


  8. <
    Name
    >
    Myweb Maven webapp
    Name
    >


  9. <
    URL
    >
    Http://maven.apache.org
    URL
    >


  10. <
    Dependencies
    >

  11. <
    Dependency
    >

  12. <
    Groupid
    >
    JUnit
    Groupid
    >


  13. <
    Artifactid
    >
    JUnit
    Artifactid
    >


  14. <
    Version
    >
    3.8.1
    Version
    >


  15. <
    Scope
    >
    Test
    Scope
    >


  16. Dependency
    >


  17. Dependencies
    >


  18. <
    Build
    >

  19. <
    Finalname
    >
    Myweb
    Finalname
    >





  20. <
    Plugins
    >


  21. <
    Plugin
    >


  22. <
    Groupid
    >
    Org. mortbay. Jetty
    Groupid
    >



  23. <
    Artifactid
    >
    Maven-jetty-plugin
    Artifactid
    >



  24. Plugin
    >



  25. Plugins
    >










  26. Build
    >


  27. Project
    >



5) run the test

Java code
  1. MVN jetty: Run

When the network is connected normally, Maven automatically downloads all jar dependent packages and starts jetty6 to access http: // localhost: 8080/myweb/in the browser/

Jetty6 has been started and displays the "Hello world !"
Now you can modify src/main/webapp/index. jsp and refresh the browser to see the effect. The simplest web skeleton is born.
The command to stop jetty6 is Ctrl + C.

Then, you can go to the Pom. add spring, Hibernate/ibatis, struts/struts2/webwork2, report, XDoclet, Hibernate code generation tool, dbunit, etc. to XML... (write it here today and write it later)

Two articles are recommended:

Http://www.blogjava.net/calvin/archive/2006/03/19/36098.html

Http://www.ibm.com/developerworks/cn/opensource/os-maven2/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.