transferred from: http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.htmlApache Maven Introductory article
George Ma
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!!
Next we introduce the following core concepts:
- POM (Project Object Model)
- Maven Plugin
- Maven life cycle
- Maven Dependency Management
- Maven Library
POM (Project Object Model)
All configurations for a project are placed in the POM file: Define the type of project, name, manage dependencies, customize plugin behavior, and so on. For example, you can configure the compiler plugin to use Java 1.5来 to compile.
Now look at the POM for the example in the first article
XML Code
<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.helloworld</groupId>
<artifactId>helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld</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>
In POM, GroupId, Artifactid, packaging, and version are called Maven coordinates, and it uniquely identifies a project. With maven coordinates, we can use it to specify other projects, plugins, or parent projects on which our project depends. The general MAVEN coordinates are written in the following format:
GroupId:artifactId:packaging:version
Examples like ours can be written as:
Com.mycompany.helloworld:helloworld:jar:1.0-snapshot
Our HelloWorld example is simple, but a large project is generally divided into several sub-projects. In this case, each subproject will have its own POM file, and then they will have a common parent project. This makes it possible to build all of the child projects as long as the parent project is built. The Pom of the subproject inherits the parent project's POM. In addition, all POM has inherited a super-pom. Super-pom set some default values, such as the default directory structure mentioned in the first article, the default plug-ins, and so on, which follow the rules of Convention over configuration. So while our POM is simple, it's just a part of what you can see. The POM at run time is much more complex. If you want to see the full contents of the POM at run time, you can run the following command:
$MVN Help:effective-pom
Maven Plugin
In the first article, we used the MVN archetype:generate command to build a project. So what does the archetype:generate here mean? Archetype is the name of a plugin, generate is the name of the target (goal). This command means to tell Maven to execute the generate target of the archetype plugin. Plug-in targets are often written as Pluginid:goalid
A target is a unit of work, and a plug-in is a collection of one or more targets. such as Jar plug-ins, compiler plug-ins, surefire plugins. As you can see from the name, the jar plugin contains the target of building the jar file, and the Compiler plug-in contains the target of compiling the source code and unit test code. Surefire plug-ins, it is run unit test.
See here, it is estimated that you can understand that MVN itself does not do too much, it does not know how to compile or how to package. It gives the built-in task to the plugin to do. Plug-ins Define common building logic that can be reused. The advantage of this is that once the plugin has been updated, all MAVEN users will be updated.
Maven life cycle
In the previous article, we used the second command: MVN package. The package here is a Maven life cycle phase (lifecycle phase). Life cycle refers to the construction process of a project, which contains a series of orderly phases (phase), and a stage is a step in the construction process.
What is the relationship between the life cycle phase and the plug-in target mentioned above? The plug-in target can be bound to the life cycle phase. A life cycle phase can bind multiple plug-in targets. When Maven passes through each phase gradually during the build process, all plug-in targets for that phase are executed.
Maven can support different lifecycles, but the most common is the default Maven life cycle (lifecycle). If you do not have any plug-in configuration or customization for it, then the above command MVN package will execute the plug-in target in the default life cycle until all phases before the package stage are included:
Process-resources Stage: Resources:resources
Compile stage: Compiler:compile
Process-classes Stage: (Default no target)
Process-test-resources Stage: Resources:testresources
Test-compile Stage: Compiler:testcompile
Test stage: Surefire:test
Prepare-package Stage: (Default no target)
Package Stage: Jar:jar
Maven Dependency Management
As we said before, MAVEN coordinates can determine a project. In other words, we can use it to resolve dependencies. In POM, dependencies are defined in the Dependencies section. In the above POM example, we define a dependency on JUnit with dependencies:
XML Code
<dependencies> <dependency> <groupId>junit</groupId> <artifactId> junit</artifactid> <version>3.8.1</version> <scope>test</scope> < /dependency>
That's a simple example, but in real development we have a much more complex dependency, because the dependent JAR file has its own dependencies. So do we need to define the jar files that are indirectly dependent on the POM? The answer is no, because MAVEN provides the characteristics of transitive dependencies.
The so-called transitive dependency is that Maven examines the dependent jar file, incorporating its dependencies into the final resolved dependency chain. For the above junit dependencies, if you look at Maven's local library (we'll explain the MAVEN library soon) ~/.M2/REPOSITORY/JUNIT/JUNIT/3.8.1/,
You will find that Maven not only downloaded the Junit-3.8.1.jar, but also downloaded its POM file. MAVEN will then be able to check the dependencies of JUnit and include the dependencies it needs.
In the Dependencies section of the POM, scope determines the scope of the dependency relationship. In our example, JUnit's scope is test, and it will only be added to the classpath when executing the compiler:testcompile and surefire:test targets, in the execution of the Compiler:compile target You can't get junit.
We can also specify scope as provided, which means that the JDK or container will provide the required jar files. For example, when doing Web application development, we need the servlet API jar file when compiling, but we don't need to put the jar file in the WAR when we are packing, because the servlet container or the application server will provide it.
The default value of scope is compile, which is included in the classpath at any time and is included in the package.
Maven
Library
When you run the MAVEN command for the first time, you need an Internet connection because it wants to download some files from the Web. So where does it download from? It is downloaded from the MAVEN default remote library (HTTP://REPO1.MAVEN.ORG/MAVEN2). This remote repository has MAVEN's core plug-in and the jar files available for download.
But not all jar files can be downloaded from the default remote library, such as the one we developed ourselves. At this point, there are two options: either set up a custom library within the company or manually download and install the required jar files to the local library.
A local library is a copy of maven that is stored on the local machine after the plugin or JAR file has been downloaded. On Linux, it's located on ~/.m2/repository, on Windows XP, on C:\Documents and Settings\username\.m2\repository, on Windows 7, in C:\Us Ers\username\.m2\repository. When Maven looks for the jar file that it needs, it looks for it in the local library and only finds it in the remote library if it can't find it.
Run the following command to install our HelloWorld project to a local library:
$MVN Install
Once a project has been installed in the local library, your other projects can build dependencies through MAVEN coordinates and the project. For example, if I now have a new project that needs to use HelloWorld, then after running the MVN install command above, I can build the dependencies as follows:
XML Code
<dependency> <groupId>com.mycompany.helloworld</groupId> <artifactId> Helloworld</artifactid>
Well, the core concept of MAVEN is simply introduced here. As for how Maven is used in Eclipse, there's a lot of this online, and Google is on the line.
Apache Maven Introductory article