Transferred from: http://www.cnblogs.com/xdp-gacl/p/4240930.html
As a highly automated build tool, MAVEN itself provides the ability to build projects, and here's how to build a project using MAVEN.
First, buildJave Project1.1. CreateJave Project
1. Use the MVN archetype:generate command as follows:
mvn archetype:generate -darchetypeartifactid=maven-archetype-quickstart -dinteractivemode=false
2. Use the MVN archetype:create command as follows:
mvn archetype:create -darchetypeartifactid=maven-archetype-quickstart -dinteractivemode=false
You can create a project using the "mvn archetype:generate" command and "mvn archetype:create", and there is no difference between the two, and the only difference is the discovery using "MVN Archetype:generate"command creates a project for a very long time to be able to create the project, and the"mvn archetype:create"command allows you to create the project quickly.
The process of creating a Java project using the "mvn archetype:create" command is as follows:
Build success indicates that the project was built successfully when a Java project was built under the previous user directory (that is, C:\Documents and settings\administrator) called MyApp.
The directory structure of the built-in Java project is as follows:
As you can see, the project Maven helped us to create is a standard MAVEN project, but for now Maven just helped us generate the Src/main/java (the source code for the project) and the Src/test/java (which stores the test source), However, in the actual project development we will generally have configuration files, such as log4j.properties, so we also need to manually create src/main/resources (for storing the configuration files used in the project development, such as storing log4j.properties, etc.) and src/ Test/resources (the configuration file used to store the test), as shown in:
Then we can import the created MyApp project into eclipse for development, as shown in:
1.2, Javaproject of the Pom.xml file description
With maven-built javaproject, there is a pom.xml file at the root of the project, into the MyApp directory where you can see a pom.xml file, which is the core of Maven. As shown in the following:
1, POM means Project object model.
2, Pom.xml contains the project construction information, including the project information, project dependencies and so on.
3, Pom.xml files can be inherited, large projects, sub-modules Pom.xml generally inherit from the parent module Pom.xml
The contents of the Pom.xml file are as follows:
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/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 5 <groupId>com.mycompany.app</groupId> 6 <artifactid >myapp</artifactId> 7 <version>1.0-SNAPSHOT</version> 8 <packaging>jar</packaging > 9 <name>myapp</name>11 <url>http://maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</project.build.sourceencoding>15 </properties>16 <DEP endencies>18 <dependency>19 <groupid>junit</groupid>20 <artifactid>junit</ar tifactid>21 <version>3.8.1</version>22 <scope>test</scope>23 </dependency> ; </dependencies>25 </project>
Description of the node element of the Pom.xml file:
<project> the top node of the pom file
<modelVersion> object model version, for Maven2 and Maven3, can only be 4.0.0
<groupId> Project creates an identifier for an organization, typically an inverted domain name
<artifactId> defines the unique identity of the project under the identifier of the owning organization, which can have multiple projects under an organization
<version> current project version, SNAPSHOT, represents a snapshot version, in development
<packaging> packaged in a jar, war, ear, etc.
Name of the <name> project
Address of the <url> project
<properties> property configuration, such as:<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<dependencies> building a project-dependent jar
Where the GroupID, Artifactid, and version uniquely determine a project's coordinates
1.3. Use MAVEN to compile-test-package-install project 1.3.1, compile
Compile the source program, go to the command line, switch to the MyApp directory, execute the command:mvn clean Compile, as shown in:
Compile successfully, in the MyApp directory, a more target directory, target\classes is stored in the compiled class file, as shown in:
1.3.2, testing
Go to the command line, switch to the MyApp directory, execute the command:mvn clean Test, as shown in:
The test succeeds, there will be a test-classes directory under the Myapp\target directory, where the class file of the test code is stored, as shown in:
1.3.3, Packaging
Go to the command line, switch to the MyApp directory, execute the command:mvnClean packages executes the compile and test commands before executing the package command, as shown in:
After the build succeeds, the Myapp-1.0-snapshot.jar package is generated in the target directory, as shown in:
1.3.4, installation
Go to the command line, switch to the My-app directory, execute the command:mvn clean install , before executing the installation command, perform the compile, test, and package commands as shown in:
If the build succeeds, the project's jar package is installed to the local repository, as shown in:
1.3.5, running the jar package
Go to the command line, switch to the MyApp directory, execute the command: JAVA-CP Target\myapp-1.0-snapshot.jar com.mycompany.app.App, as shown in:
Ii. Building Javaweb Project 2.1, creating Javaweb Project
1. Use the mvn archetype:generate command as follows:
mvn archetype:generate -darchetypeartifactid=maven-archetype-webapp -dinteractivemode=false
The process of creating a Javaweb project using themvn archetype:generatecommand is as follows:
Using the "mvn archetype:generate" command to create a Javaweb project takes a very long time, takes more than 40 seconds, sometimes even longer, and does not know why.
2. Use the mvn archetype:create command as follows:
mvn archetype:create -darchetypeartifactid=maven-archetype-webapp -dinteractivemode=false
The process of creating a Javaweb project using themvn archetype:createcommand is as follows:
Using themvn archetype:createcommand to create a Javaweb project is very fast and takes a few seconds.
The directory structure for creating a good Javaweb project is as follows:
There are currently only src/main/resources directories in the created Javaweb project, so you will need to add Src/main/java, Src/test/java, src/test/resources manually as well.
As shown in the following:
Then we can import the created javaweb into eclipse for development, as shown in:
2.2. Use Maven to package publishing Web projects
The Javaweb project that Maven created for us was an empty project with only one index.jsp page, and we ran the Web project package release using MAVEN.
Switch to the MyWebApp directory at the command line, execute:mvnPackage, after the build is successful, the MyWebApp directory directory is more than one target directory, This directory will be packaged into a mywebapp directory. War, copy the war package to the Tomcat's release directory to run. As shown in the following:
Packaged successfully, a Mywebapp.war file is generated in the Mywebapp\target directory, as shown in:
Put the Mywebapp.war on the Tomcat server, as shown in:
The results are as follows:
In addition to running the Web project with the Tomcat server, we can also integrate the jetty release run in the Web project, first configuring the jetty plug-in in the Pom.xml file, as follows:
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.app</groupId> 5 <artifactId> Mywebapp</artifactid> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>mywebapp Maven webapp</name> 9 <url>http://maven.apache.org</url>10 <dependencies >11 <dependency>12 <groupid>junit</groupid>13 <artifactid>junit</artifactid& gt;14 <version>3.8.1</version>15 <scope>test</scope>16 </dependency>17 < ;/dependencies>18 <build>19 <finalname>mywebapp</finalname>20 <pluginManagement>21 <!--configuration Jetty-->22 <plugins>23 <plugin>24 <groupId>org.mortbay.jetty</groupId> <art ifactid>maven-jetty-plugin</artifactid>26 </plugin>27 </plugins>28 </plu ginmanagement>29 </build>30 </project>
Open a command-line window, switch to the MyWebApp directory, and then execute:mvn jetty:run start the jetty server as shown in:
The application can then be accessed on port 8080. As shown in the following:
Iii. description of the command for MAVEN to create the project
MVN archetype:create or mvn archetype:generate fixed notation
-dgroupid Organization identification (package name)
-dartifactid Project Name
-darchetypeartifactid specify Archetypeid,maven-archetype-quickstart, create a Java Project;maven-archetype-webapp, create A web Project
-dinteractivemode whether to use interactive mode
Archetype is a mvn built-in plugin that creates a Java project skeleton, DgroupId is the name of the package, DartifactId is the project name, Darchetypeartifactid is an available MVN project skeleton, The currently available skeletons are:
- Maven-archetype-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
- Maven-archetype-webapp
Each skeleton will have a corresponding directory structure and some common files, the most commonly used is the maven-archetype-quickstart and maven-archetype-webapp skeleton. The Maven-archetype-quickstart skeleton is used to create a Java project, while the Maven-archetype-webapp skeleton is used to create a Javaweb project.
It has to be said that Maven is really a good project builder. Mastering maven is very helpful for project development.
Maven Learning 3-building projects with Maven