George ma
I wrote this Maven entry article because I found that many people are not familiar with Maven in a hands-on experiment by a developer, so I had this idea.
This article is divided into two chapters. This article focuses on using Maven to build and run the hellow World Program. Let's take a look at how Maven is used instead of any IDE. Next, I will explain the core concepts of Maven. These two articles deliberately avoided complicated examples and did not use ide. They aim to eliminate interference and focus on maven itself.
The source code of this article can be downloaded from here.
What is Apache Maven used?
Maven is an automated tool for project management and building. But for our programmers, what we are most concerned about is its project building function. So here we will introduce how to use Maven to meet the daily needs of our projects.
Maven is better than configuration. It requires that all projects have the following structure before customization:
Directory |
Purpose |
$ {Basedir} |
Store Pom. xml and all subdirectories |
$ {Basedir}/src/main/Java |
Java source code of the project |
$ {Basedir}/src/main/Resources |
Project resources, such as property files |
$ {Basedir}/src/test/Java |
Project testing class, such as JUnit code |
$ {Basedir}/src/test/Resources |
Resources used for testing |
A Maven project will generate jar files by default. In addition, the compiled classes will be placed under $ {basedir}/target/classes, the jar file will be placed under $ {basedir}/target.
At this time, some people will say that ant does not have so many requirements, it allows you to freely define the project structure. I don't want to get involved here. I personally think Maven's default definitions are very convenient to use.
Now let's install Maven.
Maven Installation
Make sure that you have installed JDK before installing Maven. JDK 6 can be downloaded from Oracle:
Http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html.
Maven official website download link is: http://maven.apache.org/download.html.
The Installation Guide is provided at the end of this page.
After the installation is complete, run the following command on the command line:
$ MVN-V
Apache Maven 3.0.3 (r00005438; 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 the above output, it indicates that the installation is successful.
Next we will use Maven to build the most famous "Hello World !" Program :)
Note: If you are running Maven for the first time, you need to connect to the internet because Maven needs to download the required plug-in from the Internet.
The first step is to build a Maven project. In Maven, we execute the maven target (GOAL) to do things.
The Maven target is similar to ant target. Execute the following command in the command line to create our hello World project.
~ $ MVN archetype: generate-dgroupid = com. mycompany. helloworld-dartifactid = helloworld-dpackage = com. mycompany. helloworld-dversion = 1.0-Snapshot
Archetype: The generate target lists a series of archetype types for you to choose from. Archetype can be understood as a project model. Maven provides many project models, from simple swing to complex web applications. Select the default Maven-Archetype-Quickstart, Which is #106, as shown in:
Press two keys to confirm the configuration of the project properties,
These attributes are specified using the-D option in the command line. This option uses the format of-dname = value. Press enter to confirm the establishment of the project, as shown in:
At this time, let's take a look at the file directory structure created by Maven:
The Archetype plug-in of Maven creates a helloworld directory named artifactid. Under this directory, there is a project Object Model (POM) file Pom. xml. This file is used to describe the project, configure the plug-in and manage dependencies.
Source code and resource files are placed under src/main, and test code and resources are placed under src/test.
Maven has created an 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!" ); } }
It is exactly the Hello world code we need. So we can build and run this program. Build with the following simple command:
~ $ CD helloworld
~ $ MVN package
When you run Maven for the first time, it downloads the required program from the maven Library (repository) on the Internet and stores it in the local repository on your computer, therefore, you need an Internet connection. The default local Maven library is ~ /. M2/Repository/, which is % user_home % \. m2 \ repository \ in windows \.
If the build is correct, the following result is returned:
At this time, Maven creates a new directory target/Under helloworld, And the JAR file helloworld-1.0-SNAPSHOT.jar after the build is stored 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.
To verify that our program can run, run the following command:
~ $ Java-CP target/helloworld-1.0-SNAPSHOT.jar com. mycompany. helloworld. app
Running successful !!
Now you may have a lot of problems. So the next article will explain the core concepts of Maven, hoping to answer some questions you may have.
Apache Maven (Part 1)