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
- MVN archetype: Create/
- -Dgroupid = com. mycompany. webapp/
- -Dartifactid = myweb/
- -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
- 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
- <
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
>
Com. mycompany. webapp
Groupid
>
- <
Artifactid
>
Myweb
Artifactid
>
- <
Packaging
>
War
Packaging
>
- <
Version
>
1.0-Snapshot
Version
>
- <
Name
>
Myweb 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
>
Myweb
Finalname
>
-
- <
Plugins
>
- <
Plugin
>
- <
Groupid
>
Org. mortbay. Jetty
Groupid
>
- <
Artifactid
>
Maven-jetty-plugin
Artifactid
>
- Plugin
>
- Plugins
>
-
- Build
>
- Project
>
5) run the test
Java code
- 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/