Getting Started with maven (top)

Source: Internet
Author: User

This digest is from the Oracle Java Community George Ma

Http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html

Writing this maven primer is because it was found in a developer's hands-on experiment that quite a few people didn't know much about Maven, so that's the idea.
This introductory chapter is divided into two articles. This article focuses on hands-on, using MAVEN to build a running Hellow World program, experience without any IDE, only with Maven. Then the next chapter will explain the core concepts of maven. Writing these two articles deliberately avoids complex examples and does not use the IDE to eliminate distractions and focus on Maven itself.

The source code of this article can be downloaded from here.

What is Apache Maven for?

Maven is a project management and building automation tool. But for our programmers, our greatest concern is its project-building capabilities. So here's how we're going to use Maven to meet the daily needs of our project.
The Maven usage convention is better than the principle of configuration. It requires that all projects have the following structure before they are customized:

Directory

Objective

${basedir}

Storage of Pom.xml and all sub-directories

${basedir}/src/main/java

Java source code for the project

${basedir}/src/main/resources

Project resources, such as property files

${basedir}/src/test/java

Test classes for the project, such as the JUnit code

${basedir}/src/test/resources

Resources used for testing

A MAVEN project produces a jar file by default, and the compiled classes is placed under ${basedir}/target/classes, and the jar file is placed under ${basedir}/target.
Then someone will say, Ant doesn't have that many requirements, it allows you to define the structure of the project freely. Don't want to provoke a spat here. I personally feel that these default definitions of MAVEN are easy to use.
OK, let's install maven next.

Maven's installation

Before installing MAVEN, make sure you have the JDK installed. JDK 6 can be downloaded from Oracle technology online:
Http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html.
The download link for Maven official website is: http://maven.apache.org/download.html.
The installation guide is given at the end of the page.

After the installation is complete, run the following command at the command line:

$ mvn-v
Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven Home:/home/limin/bin/maven3
Java Version:1.6.0_24, Vendor:sun Microsystems Inc.
Java Home:/home/limin/bin/jdk1.6.0_24/jre
Default Locale:en_us, Platform Encoding:utf-8
OS name: "Linux", Version: "2.6.35-28-generic-pae", Arch: "I386", Family: "Unix"


If you see an output similar to the one above, it means the installation was successful.
Next we use MAVEN to build the most famous "Hello world!". Program:)
Note: If you are running Maven for the first time, you need an Internet connection because Maven needs to download the required plugins from the Web.
The first step we have to make is to build a MAVEN project. In Maven, we are doing the MAVEN goal (goal) to do things.
Maven targets are similar to Ant's target. Execute the following command at the command line to build our Hello World project

~ $MVN archetype:generate-dgroupid=com.mycompany.helloworld-dartifactid=helloworld-dpackage= Com.mycompany.helloworld-dversion=1.0-snapshot


The Archetype:generate goal will list a series of archetype to make you choose. Archetype can be understood as a model of the project. Maven provides us with a variety of project models, from simple Swing to complex Web applications. We select the default Maven-archetype-quickstart, which is the number #106, as shown in:


Even hitting two returns, this time lets you determine the configuration of the project properties,


These properties are specified in the command line with the-D option. This option uses the-dname=value format. Enter confirm and the project is completed, as shown in:


Let's take a look at the file directory structure that Maven created for us:



MAVEN's archetype plugin created a HelloWorld directory with the name Artifactid. Under this directory, there is a Project Object Model (POM) file pom.xml. This file is used to describe the project, configure plug-ins, and manage dependencies.
The source code and resource files are placed under Src/main, and the test code and resources are placed under src/test.

Maven has created a App.java file for us:

Java code

    Package Com.mycompany.helloworld;    /**      * Hello world!      * *         /public class App {public  static void Main (string[]        args) {System.out.println ( "Hello world!"); }    }  

Is exactly what we need for the Hello World code. So we can build and run this program. Build with the following simple commands:

~ $CD HelloWorld

~ $MVN Package

When you run Maven for the first time, it downloads the required programs from the online Maven library (repository) and stores them in your computer's local repository, so you need an Internet connection at this time. The default Maven local library is ~/.m2/repository/, which is%user_home%\.m2\repository\ under Windows.



If the build is error-free, you will get a result similar to the following:


At this point, MAVEN builds a new directory target/under HelloWorld and builds the packaged jar file Helloworld-1.0-snapshot.jar in this directory. The compiled class file is placed under the target/classes/directory, and the test class file is placed under the target/test-classes/directory.

In order to verify that our program can run, execute the following command:

~ $java-CP Target/helloworld-1.0-snapshot.jar com.mycompany.helloworld.App


Run successfully!!


Now you may have a lot of problems. So the next article will explain the core concepts of maven and hope to answer some of the questions you might have.

Getting Started with maven (top)

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.