Maven getting started

Source: Internet
Author: User
1. What is Maven?

Maven English name: Expert, visible by name, this tool is ambitious. Someone said that he is a "build tool", a tool used to build source code into a publishable component. Some people also say it is a project management tool.

Official definition:

Maven is a project management tool that contains a project object model, a set of standard sets, and a project life cycle ), A dependency management system and logic used to run the plug-in (GOAL) defined in the lifecycle phase (phase.

I understand:

Maven built-in software repository to help build packaging management projects.

Ii. Install Maven in Linux 1. Java environment.

Set java_home

In ~ /. Add to bashrc

Export java_home =/usr/lib/JVM/java-1.6.0-openjdk-1.6.0.0.x86_64/

Ensure that Java commands are available. If not, install Yum install openjdk-devel.

2. Download Maven.

Http://maven.apache.org/download.html

Unzip to:/opt/apache-maven-3.0.3/

Set environment variables and add ~ /. Bashrc

Export m2_home =/opt/apache-maven-3.0.3/

Export Path = $ path:/opt/apache-maven-3.0.3/bin/

Execute .~. /Bashrc.

3. Check whether the test is successful.

4. Install Maven in eclipse

On the official download page, you can find the address to update m2eclipse in eclispe: http://download.eclipse.org/technology/m2e/releases

Open eclipse -- help --> install new software --> Add --> name input Maven, location input http://download.eclipse.org/technology/m2e/releases

Click Next and select the part to be installed.

Verify whether the installation is successful:

File, enter the filtering condition Maven, and check the maven option, indicating that the installation is successful.

Let eclipse use Maven runtime installed externally

Windows --> preferences --> Find Maven project --> Installation Project, click Add, and find the local Maven location.

Iii. Use 1. Conventions are better than configurations

Convention over configuration

The source code is assumed to be in/src/main/Java,

The resource file is assumed to be in src/main/Resources

The test code is assumed to be in/src/test.

Project assumes that a jar file is generated.

Maven assumes that you want to put the compiled byte code to/target/classes

Create a distributable JAR file in/target.

It seems nothing, but it is much easier to use than ant. Ant needs to configure each directory. Of course there are more than these differences.

Shows the default structure:

2. Create a Maven Project Create with command

MVN archetype: Create-dgroupid = WZ. Package-dartifactid = helloworld

Added a WEB Project:-darchetypeartifactid = Maven-Archetype-webapp

Groupid group name, dartifactid project name.

Generate Pom. XML as follows:

 

  1. <Modelversion> 4.0.0 </modelversion>
  2. <Groupid> mygroup </groupid>
  3. <Packaging> jar </packaging>
  4. <URL> http://maven.apache.org </URL>
  5. <Properties>
  6. <Project. Build. sourceencoding> UTF-8 </Project. Build. sourceencoding>
  7. </Properties>
  8. <Dependencies>
  9. <Dependency>
  10. <Groupid> JUnit </groupid>
  11. <Artifactid> JUnit </artifactid>
  12. <Version> 3.8.1 </version>
  13. <Scope> test </scope>
  14. </Dependency>
  15. </Dependencies>
  16. </Project>

 

Write a helloworld

• MVN clean compile and generate target

Use eclipse Maven plug-in to create project 1.new project select Maven project2 to create simple project3. enter basic information and click Finish. The generated directory structure is as follows: 3 test:

@ Test

Public void testsayhello (){

App = new app ();

String result = app. sayhello ();

Assert. assertequals ("Hello Maven! ", Result );

}

• MVN clean test

4. Search for available versions

• Http://mvnrepository.com/

• For example, you can find many versions of JUnit and click to view them.

5 packaging and Installation

MVN clean package

MVN clean install

MVN jetty: Run (web project running)

Complex example:

Org. springframework. samples. jpetstore (complex)

6. Scope of Project Dependencies

Compile (compilation range) Compile is the default range;

Provided (provided scope) such as Servlet

Runtime (runtime range), such as JDBC Implementation Package

Test (test scope)

System)

4. FAQs 1. Web project Conversion

MVN Eclipse: eclipse is converted into an Eclipse project and cannot be identified as a web project. Use the following command:

MVN Eclipse: eclipse-dwtpversion = 1.0

2. Do not recognize m2_repo

The m2_repo parameter is not recognized by eclipse and needs to be added to it:

Eclipse-> Windows-> preferences-> JAVA-> build path-> classpath Variables

The name is m2_repo, and the property is folder, pointing to c: \ Documents and Settings \ kaseyxiong \. m2 \ Repository

3. Dependency packages are not imported during eclipse deployment.

This problem is usually not encountered. If so, the project -- properties -- deployment assembly --> Add --> build path entries --> select the required package --> finish

4. eclipse not compiled

Project --.> just clean

5. Download the source code of the dependent package

Add the parameter-ddownloadsources = true or MVN dependency: sources.

Maven getting started

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.