Maven Basic Getting Started usage

Source: Internet
Author: User

First, download and install

1.1 Download Maven 3.1.1

First to the official website http://maven.apache.org/download.cgi Download the latest version (currently 3.1.1), after the download is complete, extract to a directory (this is C:\Java\maven-3.1.1)

2.1 Configuring Environment variables

In the system environment variable, add maven_home (or m2_home) with a value of C:\Java\maven-3.1.1, then the PATH environment variable is appended with ";%maven_home%\bin"

Detection method:

A) Re-enter the command line (DOS window) mode, enter echo%maven_home% if you can show C:\Java\maven-3.1.1 the environment variable is working

b) Enter Mvn-version, and the version number of MAVEN and JDK will be displayed under normal circumstances

(Prerequisite: The JDK environment must be installed first, otherwise it will not compile the project properly later)

3 Understanding " Warehouses"

After the first run of Mvn-version, a. M2 directory is created under the user directory (for example: C:\Users\ Current user name \.m2\), which is Maven's "local Repository", which is a very important concept in Maven.

Imagine that we create many projects at the same time in our work, and each project may refer to some common jar packages (. NET is a DLL file), a practice is that each project, a copy of these dependent jar packages (or DLL files), it is obviously not good, the same files on the hard disk to save multiple copies, too much space, and these dependent jar package (or DLL file) version is not very good management ( For example, a common jar package, upgrading from 1.0 to 2.0, if all the items referencing this jar package need to be updated, each item must be modified.

The MAVEN repository solves these problems by creating a native repository on each machine, unifying the jar packages that all MAVEN projects rely on on the machine, and uniquely identifying them with the " coordinates" (note: coordinates are another important concept, and later, This is simply understood as a "unique identification of a jar package file name, version number", so that all MAVEN projects do not need to copy the jar package into the Lib directory as before, and the entire MAVEN project looks very refreshing.

4 Configuring proxy servers (optional)

MAVEN projects in the compilation, testing, packaging, will need from MAVEN's central warehouse (ie: the MAVEN organization published on the Internet, a site, which already contains the vast majority of the current mainstream jar package) download jar package, etc., if using a proxy server to surf the Internet, you need to configure a proxy server.

Copy the%maven_home%\conf\settings.xml to the local warehouse C:\Users\ the current user name \.m2\, and then edit the file to find the following

<proxies>    <!--proxy     | Specification for one proxy, to is used in connecting to the network.     |    <proxy>      <id>optional</id>      <active>true</active>      <protocol>http </protocol>      <username>proxyuser</username>      <password>proxypass</password >      

Remove the comments, host here to fill in the address of the proxy server (can use IP) and port ports, if you need username/password authentication, then fill out the Username/password node, otherwise username/password these two nodes removed, Nonproxyhosts indicates that some addresses do not need to go through a proxy server, multiple addresses are separated by |, and wildcard characters are supported, such as 172.156.*

Ii. creating the "skeleton" of the project

Below, create a most basic MAVEN project with a named line

2.1 MVN Archetype:generate

First create the project root directory, such as C:\Test, command-line window under the input

CD/D C:\Test

MVN archetype:generate

During the first run, MVN will download some of the necessary files from the remote "central repository" to the "local repository"-(if you are interested, you can look at the "C:\Users\ Current user name \.m2\repository" in the process of waiting for the download)

After the download is complete (the next one will explain how, in the LAN environment to build a "network", directly from the local area network to download these dependencies), will automatically enter the interactive mode, will let you enter some basic information, similar to the following:

...

[INFO] Generating project in Interactive mode (this will be stuck for a while, because you want to get the project template online)
[INFO] No archetype defined. Using Maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1:remote-Br.com.ingenieux:elasticbeanstalk-service-webapp-archetype (A Maven archetype Encompassing RestAssured , Jetty, Jackson, Guice and Jersey for publishing jax-rs-based Services on AWS ' Elastic Beanstalk Service)
... (many project templates are automatically listed here, with a number ordinal in front of each template)

336:remote-Org.apache.maven.archetypes:maven-archetype-quickstart (an archetype which contains a sample Maven Proj ect.)

...

Choose a number or Apply filter (format: [Groupid:]artifactid, Case Sensitive contains): 336: (here the Eclipse plugin installed on everyone's machine does not The same, probably the default number is not this, regardless, direct return)

Choose Org.apache.maven.archetypes:maven-archetype-quickstart Version:
1:1.0-alpha-1
2:1.0-alpha-2
3:1.0-alpha-3
4:1.0-alpha-4
5:1.0
6:1.1
Choose a number:6: (direct carriage return)
Define value for property ' GroupId ':: Cnblogs (may be temporarily first understood as a package or namespace name, usually we fill in the organization name abbreviation)
Define value for property ' Artifactid ':: Maven-hello-world (component name, can be temporarily understood as project name)
Define value for property ' version ': 1.0-snapshot:: (version number, direct carriage return, default 1.0-snapshot)
Define value for the ' package ': cnblogs:: (packaged jar filename, equivalent to the last assembly DLL name generated by the project in. net)
Confirm Properties Configuration:
Groupid:cnblogs
Artifactid:maven-hello-world
Version:1.0-snapshot
Package:cnblogs
Y:: (Direct enter confirmation)
[INFO]----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from old (1.x) archetype:maven-archetype-quickstart:1.1
[INFO]----------------------------------------------------------------------- -----
[INFO] parameter:groupid, value:cnblogs
[INFO] parameter:packagename, value:cnblogs
[INFO] Parameter: Package, Value:cnblogs
[INFO] Parameter:artifactid, Value: Maven-hello-world
...
[INFO]------------------------------------------------------------------- -----
[INFO] BUILD SUCCESS (see this to show that the project was created successfully!)
[INFO]------------------------------------------------------ ------------------
...

2.2 The directory structure of the MAVEN project

C:\test\maven-hello-world>tree
Folder PATH listing for volume Win7
Volume Serial number is Aa2c-6e70
C:.
├───src
│├───Main
││└───Java
││└───cnblogs
│└───Test
│└───java
│└───cnblogs
└───Target
└───classes
└───cnblogs

Note the Red directory name above, the MAVEN project adopts the " convention better than the configuration" principle, theSrc/main/java convention is used to store the source code,src/main/test for the unit test code,src/ Target is used to store compiled, packaged output files. This is a common convention for MAVEN projects around the world, so keep these fixed directory structures in mind.

Third, compile the project

Go to the root of the project you just created

CD/D C:\test\maven-hello-world

Then execute mvn clean compile

This will be able to compile the project, the compilation will automatically generate a class file in the target directory, if the compilation is successful, will be output similar to the following information

yangjunmingmatomacbook-pro-7:maven-hello-world jimmy$ mvn Clean Compile
[INFO] scanning for projects ...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO] Building maven-hello-world 1.0-snapshot
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]---maven-clean-plugin:2.5:clean (default-clean) @ Maven-hello-world---
[INFO]
[INFO]---maven-resources-plugin:2.6:resources (default-resources) @ Maven-hello-world---
[INFO] Using ' UTF-8 ' encoding to copy filtered resources.
[INFO] Skip non existing resourcedirectory/users/jimmy/desktop/study/maven-hello-world/src/main/resources
[INFO]
[INFO]---maven-compiler-plugin:2.5.1:compile (default-compile) @ Maven-hello-world---
[INFO] Compiling 1 source file to/users/jimmy/desktop/study/maven-hello-world/target/classes
[INFO]------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]------------------------------------------------------------------------
[INFO] Total time:1.515s
[INFO] Finished at:wed Jan 19:08:32 CST 2014
[INFO] Final memory:10m/156m
[INFO]------------------------------------------------------------------------

Iv. Unit Testing

MVN Clean Test

So you can do the unit test, so easy!

If the unit test does not pass, it will prompt an error message and look at the output.

Note: From the output can be found, test, before the execution of compile, that is, compile first, then perform unit testing.

Interested friends can modify the contents of the next/src/test/java/cnblogs/apptest.java, put the TestApp () method in the Asserttrue ( true); change to Asserttrue ( false) and run down the unit test and see what's different.

Five, the project packaging

Typically we package Java projects into jar packages or war packages, and the commands packaged in Maven are

MVN Clean Package

After running, the jar package is generated in the target directory

Note: From the output can be found, the package before the implementation of the compile, and then execute test, and finally packaging packages

Vi. operation of the project

App.java in the project has the main method, you can run directly, in general, if we want to run the class file directly, we have to knock a long command, maven do not have to be so complicated, first open the project root directory with Notepad pom.xml file, add the following section:

<build> <finalName>${project.artifactId}</finalName> <plugins> <plugin > <groupId>org.codehaus.mojo</groupId> <artifactid>exec-maven-plugin&lt                    ;/artifactid> <version>1.2.1</version> <executions>                        <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <co Nfiguration> <executable>java</executable> <arguments&gt                                               ; <argument>-classpath</argument> <classpath> </classpath > <argument>cnblogs. App</argument> </arguments> </configuration> </plugin> </plugins> </build> 

This piece of content is inserted before </project>.

Then, under command line, enter

MVN exec:exec

Can be run directly, the following is the output:

C:\TEST\MAVEN-HELLO-WORLD>MVN exec:exec
[INFO] scanning for projects ...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO] Building maven-hello-world 1.0-snapshot
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]---exec-maven-plugin:1.2.1:exec (default-cli) @ Maven-hello-world---
Hello world!
[INFO]------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]------------------------------------------------------------------------
[INFO] Total time:1.175s
[INFO] Finished At:mon Jan 22:35:02 CST 2014
[INFO] Final memory:6m/111m
[INFO]------------------------------------------------------------------------

VII. Deployment of the project

If this is a Web project, use the command

MVN Clean Jboss-as:deploy

can automatically deploy the Web project to JBoss (provided that JBoss Web server has started successfully), because we just created a most basic MAVEN project, not a Web project, so executing this command should fail, Here's how to deploy a Web project in eclipse with a plug-in, which you can skip first.

Other than that:

Sometimes, our project is a class library, just encapsulate some methods for other project reference, for this kind of project, we can use mvn clean install to install the generated jar package into "local warehouse", so that when the other project needs to use the jar package, As long as you configure the dependencies in the POM, you do not have to copy the jar package into the current project.

Maven Basic Getting Started usage

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.