Maven tutorial (1) configure the environment

Source: Internet
Author: User

Today, a senior student gave me a project, but I couldn't. He told me it was maven, So I studied it. So I added my homework =! I 'd like to share with you, but I 'd like to thank the large number of bloggers. Here I will try again:

Maven is an open-source project management tool written in pure Java. Maven adopts a concept called project object model (POM) to manage projects. All project configuration information is defined in a project called POM. in an xml file, Maven can manage the entire declaration cycle of a project, including compiling, constructing, testing, releasing, and reporting. Currently, Maven is used to manage most projects in Apache. Maven also supports multiple plug-ins to facilitate more flexible project control.

1: http://maven.apache.org/download.cgi download the latest version Maven 3.212 (Binary zip)

2: Decompress the package to D:

I put it here: D: \ apache-maven-3.2.1

3: Configure Environment Variables
MAVEN_HOME: D: \ apache-maven-3.0.2
MAVEN: % MAVEN_HOME % \ bin
(Optional) MAVEN_OPTS:-Xms256m-Xmx512m

Add % MAVEN % before path;

3: Configure Environment Variables
MAVEN_HOME: D: \ apache-maven-3.0.2
MAVEN: % MAVEN_HOME % \ bin
(Optional) MAVEN_OPTS:-Xms256m-Xmx512m

Add % MAVEN % before path;


4: verify whether the installation is successful
Enter mvn-version on the command line, and press Enter. If the following information is displayed, the installation is successful:

5. Create a project
On the command line, enter mvn archetype: create-DgroupId = com. mycompany. app-DartifactId = my-app and press Enter,
If this is the first time you run this command (goal), maven will spend some time downloading the latest Toolkit (called artifacts by Maven) to your local repository.


After the command is executed, you will see that maven has generated a directory named my-app, which is the artifactId you specified in the command to enter the directory, you will find the following standard project structure: This is the directory under xp:


The src/main/java directory contains the source code of the project, and the src/test/java directory contains the test code of the project, pom. xml is the Project Object Model or POM of a Project ).
6: POM
The pom. xml file is the core configuration of maven for a project. This file will contain most configuration information about how you want to build the project. POM is big and complex, but you don't need to know it all. You just need to use some common configurations. The content of this POM is listed below:
<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/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.mycompany.app</groupId>   <artifactId>my-app</artifactId>   <version>1.0-SNAPSHOT</version>   <packaging>jar</packaging>   <name>my-app</name>   <url>http://maven.apache.org</url>   <properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   </properties>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>   </dependencies> </project> 

7: What do we do in step 1 Reference mvn archetype: create-DgroupId = com. mycompany. app-DartifactId = my-app
Run the Maven command (goal) archetype: create and set some parameters (-DgroupId = com. mycompany. app-DartifactId = my-app)

In this command, the prefix archetype is a maven plugin that contains the create Command. This goal command creates a simple project based on the project prototype (A maven-compliant Project template.

Now, it can be determined that a maven plugin is a collection of goals commands with the same purpose. For example, the jboss-maven-plugin plug-in is used to process various jboss-related tasks.
8: Build project
Enter "cd my-app" in the command line and press enter to enter the project path.



Enter the mvn package and press Enter. Then, the command line will print various actions and the following information will end:



With the command ( That is a goal)
Reference archetype: create is different. This execution is just a simple command-package. Unlike goal, This is a phase (phase)A phase refers to a phase of building a lifecycle, and a build lifecycle refers to a series of orderly phase. When a phase is given, Maven will execute all the phase before this phase and its own. For example, if we execute the compile phase, the actual execution phases include:
Reference validate
Generate-sources
Process-sources
Generate-resources
Process-resources
Compile
You can use the following command to test the newly compiled and packaged jar package,
Reference java-cp target/my-app-1.0-SNAPSHOT.jar com. mycompany. app. App.
In this case, the most classic one is printed:
Reference Hello World!


9: run the Maven Tool

Although it is difficult to list a very comprehensive table, you can first list the most common default lifecycle stages:
Reference Validate:Verify that the project is correct and that all required resources are available.
Compile:Compile the source code of the project.
Test:Use a suitable unit test framework to test compiled source code. These tests do not need to be packaged and deployed.
Package:Package compiled code into a publishable format, such as jar.
Integration-test:If necessary, process and release the package to an environment that can be integrated for testing.
Verify:Run all the checks to verify whether the package is valid and meets the quality standards.
Install:Installing the package in the local repository can be used by other projects as dependencies.
Deploy:Run the command in the integration or release environment to copy the final version package to a remote repository, so that other developers or projects can share the package.
Clean:Clear previously constructed artifacts (in maven, the packages generated by the project are called artifact ).
Site:Generate a document site for the project.

The various Phases (Phases) listed above actually correspond to potential goals. Special goals executed for each stage are determined by the project type. For example, if the project type is jar, the package stage executes the jar: jar goals by default. If the project type is war, the goals executed in the package stage is war: war.
Note that phases and goals must be executed in a certain order.
Mvn clean dependency: copy-dependencies package
This command First clears the project, copies the dependencies, and finally packs the project. Of course, before packaging, it first executes the stages before this stage, such as compile and test.
Generate site
Mvn site
This stage generates project information based on pom. xml configuration. You can view the generated documents in the target/site directory.




Related Article

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.