Get started with Maven-teach you how to get started with Maven

Source: Internet
Author: User
Abstract: maven1.0 has been around for a few years and has been accepted by developers as a substitute for ant. However, it does not enable developers
Release the build. xml file. Maven1.0 is slow and clumsy. It is no less difficult to use than ant projects. In fact, its core is Ant-based.
After almost thorough rewriting, maven2.0 was born.

The most difficult part of a Java project is how to start it. We must configure
All logical relationships are well established. For example, where should the Java source code be stored? Where should unit tests be conducted? How should I place the dependent jar packages? How to build a project, how to form documents, how to test and deploy
Project? In this case, different processing options of developers will affect the rest of the project. Your choice may cause you to get into trouble and prove that you are a master of Java architecture in the future. We
Assuming that the latter is our goal, let's start with our question.

You can use many tools to build a Java project, including ant. Ant as a revolutionary
Has always been the first choice among many developers to use tools. It can free developers from using a large number of make commands. For those who are not familiar with the make command, they have plenty of reasons
It indicates that using commands is not the best tool to build a Java project, because it is not platform independent and is not easy to use. Ant solves the above problems. It uses a platform-independent tool.
It can parse the xml configuration file, that is, build. xml. Ant has many advantages, but it also has some disadvantages. The build. xml file is extremely simple.
The short description method requires developers to learn its syntax in advance. Although the learning curve is not steep, Java developers should put their time on development.

Maven is
The new generation of force is like ant a few years ago. Maven1.0 has been used for several years and has been accepted by developers as a substitute for ant.
Developers are not largely freed from the ant build. xml file. Maven1.0 is slow and clumsy. It is no less difficult to use than ant projects. Things
In fact, its core is Ant-based. After almost thorough rewriting, maven2.0 was born.

Copyright Disclaimer: Any website authorized by matrix must retain the following author information and links for reprinting.
Author: Chris Hardin; mycj (author's blog: http://blog.matrix.org.cn/page/mycj)
Original article: http://www.onjava.com/pub/a/onjava/2006/03/29/maven-2-0.html
Matrix: http://www.matrix.org.cn/resource/article/44/44475_Maven2.html
Keyword: maven2

Advantages of maven2.0

Maven2.0 has many excellent features that not only help you build projects. If you have just started a Java project and want it to be launched quickly, maven2.0 can meet your requirements within several minutes. The following are some advantages of maven2.0:
-- Standard project layout and project structure Builder
-- Standard dependency management mechanism
-- Multi-project support
-- Download new plug-ins and feature parts as needed
-- Generate the website with the latest project information
-- Integrated source code control software: CVs and subversion

The above list shows only a small part of the features of maven2.0. However, this is enough to make maven2.0 a reliable choice for building a management system. Now that we know what Maven is, let's see how to use it.

Getting started

Me
The first thing we need to do is to set the directory structure, but this does not need to be set manually. MAVEN will do this for you based on the project type you developed. Once you download and decompress the latest released
Maven 2.0, you should add the bin directory under the maven directory to your system path. You can run the MVN-version command to test your installation.

Now that the tool has been installed, let's take a look at the example of creating a simple Java project. Maven uses the prototype to determine how the directory structure is displayed. Maven comes with several built-in prototypes. You can also customize the prototype.

MVN archetype: Create-dgroupid = com. oreilly-dartifactid = My-app

As you can see, this generates our project layout.
My-app
---- SRC
---- Main
---- Java
---- Com
---- Oreilly
---- Test
---- Java
---- Com
---- Oreilly

Yes,
That's simple. This directory structure can be overwritten by creating a new prototype, but this is not recommended because one of the advantages of Maven is to use a standard directory structure. The directory structure contains two source generations
Code tree. One is the source code of a Java application, and the other is the unit test code. At the same time, you may notice that when you run Maven for the first time, it will perform some download work. When you start calling
Maven updates some of its required functions based on the plug-in you use. Maven is updated from the ibiblio repository by default. You can
Modify the maven remote repository selection in the conf directory or in the project itself.
Maven creates a pom. xml file in the my-app directory. This is an item
The most basic part of the goal. The Pom. xml file contains a set of commands that tell Maven how to build a project and contain other special commands (POM is short for "project Object Model ).
By default, Maven includes JUnit dependencies to encourage unit testing.

<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/maven-v4_0_0.xsd"> <modelversion> 4.0.0 </modelversion> <groupid> COM. oreilly </groupid> <artifactid> my-app </artifactid> <packaging> jar </packaging> <version> 1.0-Snapshot </version> <Name> Maven Quick Start Archetype </Name> <URL> http://maven.apache.org </URL> <dependencies> <dependency> <groupid> JUnit </groupid> <artifactid> JUnit </artifactid> <version> 3.8.1 </ version> <scope> test </scope> </dependency> </dependencies> </Project>

After creating a project, we can add code to the project and use all the new skills of Maven. Note that the following command must be run in the directory where the Pom. xml file is located.
-- MVN test: run the unit test in the application.
-- MVN package: generate a jar file based on the project
-- MVN install: add the JAR file of the project to the library for use when dependent on this project.
-- MVN site: the website that generates project-related information
-- MVN clean: clears the generated results in the target directory.
-- MVN Eclipse: generate an Eclipse project file

Next, let's take a look at the complexity. We know that it is more time-consuming to start a Java Web project manually than to start a simple Java project. However, it is difficult to use Maven. The following example (actually a line of commands) shows the construction of the project structure.
MVN archetype: Create-dgroupid = com. oreilly
-Dartifactid = oreilly
-Darchetypeartifactid = Maven-Archetype-webapp

The structure of the generated result is as follows:
Oreilly
---- SRC
---- Main
---- Resources
---- Webapp
---- WEB-INF

This
Once, our project supports setting different web resources included in the war file. The Pom. xml file contains a line indicating that the project should be packaged as a war file.
<Packaging> war </packaging>. Now you can use the MVN package command to generate a war file. No
Worried about how to get Dependencies from the WEB-INF/lib directory, Maven automatically contains dependencies when the Dependency Property value is set to compile. You can also add the following code
Change the name of the war file in the Pom. xml file:

<Build> <finalname> promotec </finalname> </build>

Dependency Management

Create
After creating the project structure, adding some code, testing and compiling the application, let's take a look at how Maven handles dependencies. To add a dependency to a project, you must add this dependency.
To the Pom. xml file. The next time you run Maven, it will obtain the dependency from the ibiblio repository and add the dependency to the Project Build path.

Off
There are several important things worth noting about dependency. When writing this article, the biggest headache in Maven is that Sun's JAR file cannot be obtained from the maven repository. This problem
This is due to the license restrictions set by Sun in its code. There are two solutions to this problem: one is to download the code and install it in your local repository, the other is to make an external Declaration and
This statement points to the location of the dependency in the file system. We hope sun can create its own repository as soon as possible. Despite this, MAVEN will also be upgraded to make it able to download these resources.
It will prompt the user to accept the license agreement.

Another trouble is that the latest library files used may not exist in the remote repository. Another possibility is that
Internet. All dependencies must be obtained locally. The best solution to these problems is to install the JAR file in the local repository. Place the local repository on a Web server
It is also convenient, so that the entire development team can benefit from this, and everyone does not have to manage their own repository. To change the path of the maven repository, you only need to edit the installation directory.
The settings. xml file in the conf folder.

Using dependencies in Maven is simple. Let's take a look at adding
Dependencies. Although JUnit has been used, let's add the powerful quartz Library to the project. Quartz is a time schedule written in pure Java.
Open-source project, which is a good choice for the demand side of your schedule.

<Dependency> <groupid> quartz </groupid> <artifactid> quartz </artifactid> <version> 1.5.1 </version> <scope> compile </scope> </dependency>

Me
Maven only needs to add the <dependencies> element to download quartz and use it as a dependency in the project. Don't worry about quartz
A Maven repository contains the resource information on which the dependent items depend. When Maven downloads quartz, its dependent resources are also downloaded. To verify the version
Quartz, Which is 1.5.1, is stored in the ibiblio library. You can browse the maven repository. Notice the use of the scope parameter, which tells the stage of the maven dependency
Yes. When JUnit is used, we set the scope parameter value to test to tell Maven that this dependency is only required in the test phase, not the resources required during runtime.
The following describes the scope parameter values:
-- Compile: default value. Indicates the resources required for all tasks.
-- Test: resources required to run all test cases
-- Runtime: indicates the resource required for running.
-- Provided: resources required by the JDK or classpath of the Application Server

Now
In, how to deal with the troublesome sun jar packages and the jar packages that need but cannot be found in the remote repository? We must use Maven to manually install these jar packages to the local storage.
In the repository. Don't worry, it doesn't seem that difficult. For example, we will install the jar package of the Java activation framework. First, we must download
Jar package, then we use Maven to import it to the local repository. You can also install the missing jar package to ibiblio according to the instructions in the maven upload Resource Guide.
MVN install: Install-file-dfile = Activation. Jar
-Dgroupid = javax. Activation-dartifactid = Activation
-Dversion = 1.0-dpackaging = jar

Now
In, the new jar package is installed in the local repository like other project dependencies. After adding the dependency Declaration, we are ready. Required when adding jar packages and declaring them as dependencies
Make sure that the version information is correct. Version mismatch may cause Maven to fail in searching for resources. When importing Sun's jar package, if you need help with standard naming parameters, refer to Sun
Name the standard jar package. Remember, at present you cannot publish these jar packages through the repository, which violates Sun's terms of use.

<Dependency> <groupid> javax. activation </groupid> <artifactid> activation </artifactid> <version> 1.0 </version> <scope> compile </scope> </dependency>

You
You may want to store dependencies in a source code controller library. The source code controller cannot execute this task. Dependencies change frequently, and there is usually a set of digital schemes to indicate their versions. That is to say, you
You really want to have a backup of an internal remote repository. If you have one, this will ensure that when the repository server crashes and cannot be recovered, you will not lose all custom resources. Do not place dependencies in the source generation
The Code controller also saves a lot of disk space on the repository server of the source code controller.

Configure the Repository

Each developer of the project must be in the conf directory.
It is inconvenient to configure the repository, so Maven can view multiple repositories at the same time and configure them all in the Pom. xml file. Let's take a look at an example that shows how to use
Use multiple repositories. In the following excerpt from the Pom. xml file, we set two repositories for Maven to find dependencies. Ibiblio has always been the default repository.
Use planet mirror as the backup repository. We can also use the local web server used by the team as the second repository.

<Repositories> <repository> <ID> ibiblio </ID> <Name> ibiblio </Name> <URL> http://www.ibiblio.org/maven/ </URL> </Repository> <repository> <ID> planetmirror </ID> <Name> planet mirror </Name> <URL> http://public.planetmirror.com/pub/maven/ </URL> </Repository> </repositories>

Use the Pom. xml parent file to build multiple projects

Soft
A common practice of a software company is to build multiple projects into the main products. Maintaining the dependency chain and building the entire product at one time can be a challenge, but if Maven is used, things will become
Simple. If you create a pom. xml parent file pointing to another sub-module, MAVEN will process the entire Build Process for you. It analyzes the Pom. xml file of each sub-module and
Sub-modules are dependent on each other to build projects. If each project explicitly specifies their dependencies, the placement order of sub-modules in the parent file will not be affected. However, considering other developers,
It is best to ensure that the sub-module placement order in the Pom. xml parent file is the same as the expected sub-project construction order. The following is an example.
The main Pom. xml file is as follows:

<Project> <modelversion> 4.0.0 </modelversion> <groupid> COM. oreilly </groupid> <version> 1.0-Snapshot </version> <artifactid> my-app </artifactid> <packaging> pom </packaging> <modules> <module> common </module> <module> utilities </module> <module> application </module> <module> webapplication </module> </modules> </Project>

Me
Make sure that the webapplication sub-module contains all three jar packages. Therefore, declare these jar packages as dependencies. In this example, the utilities project
You must add a dependency on the common project in the utilities project. The same applies to application submodules because they depend on
Common and utilities projects, utilities and others common. If there are 60 sub-modules in this example and they all depend on each other, this will make it difficult for new developers
To determine which project depends on other projects, this is exactly the reason that the project placement order in the Pom. xml parent file needs to be cleared.

The following are the dependencies of the utility module:

<Dependencies> <dependency> <groupid> COM. oreilly </groupid> <artifactid> common </artifactid> <version> 1.0-Snapshot </version> </dependency> </dependencies>

The following describes how to declare the dependencies of the application module:

<Dependencies> <dependency> <groupid> COM. oreilly </groupid> <artifactid> common </artifactid> <version> 1.0-Snapshot </version> </dependency> <groupid> COM. oreilly </groupid> <artifactid> utilities </artifactid> <version> 1.0-Snapshot </version> </dependency> </dependencies>

Finally, the dependency of the webapplication module:

<Dependencies> <dependency> <groupid> COM. oreilly </groupid> <artifactid> common </artifactid> <version> 1.0-Snapshot </version> </dependency> <groupid> COM. oreilly </groupid> <artifactid> utilities </artifactid> <version> 1.0-Snapshot </version> </dependency> <groupid> COM. oreilly </groupid> <artifactid> application </artifactid> <version> 1.0-Snapshot </version> </dependency> </dependencies>

Now, we only need to add an element to the Pom. xml file of each sub-module to indicate that they are part of a logical build:

<Parent> <groupid> com. oreilly </groupid> <artifactid> my-app </artifactid> <version> 1.0-Snapshot </version> </parent>

In the same directory where the Pom. xml parent file is located, there are project directories: Common, utilities, application, and webapplication. When we run the MVN package command in this directory, these projects are built in the order of dependency.

Plug-ins and reports

Maven2.0
A large number of plug-ins are available. Unfortunately, due to Maven rewriting, The maven1.0 plug-in cannot be used in 2.0. Even so, there are some available maven2.0
. The following example of plug-in configuration in the Pom. xml file is obtained directly from the maven2.0 website. This plug-in is used to configure compilation options.

<Plugins> <plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-compiler-plugin </artifactid> <configuration> <source> 1.5 </source> <target> 1.5 </Target> </configuration> </plugin> </plugins>

The Maven report plug-in can be used to generate different reports. These reports are generated when you use the MVN site command to generate a project site. The following example shows how to use the <reporting> element to configure one of these plug-ins.

<Reporting> <plugins> <plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-project-Info-Reports-plugin </artifactid> </plugin> </plugins> </reporting>

Maven plugin matrix is a very practical tool that provides which Maven plug-ins are suitable for which versions of Maven.

Maven and eclipse

For example
How can we make the world's best ide better? The answer is to use the maven2 plug-in to help you find dependencies and automatically add them to the Pom. xml file. Although the best method is
First, use Maven to create your project, and then use the command MVN Eclipse: eclipse to generate the Eclipse project file, so that you can get a good directory
Maven can also use its Eclipse plug-in to manage any project.

You can enter the site

Http://m2eclipse.codehaus.org/to install the plug-in. After the installation is complete and the IDE is restarted, You need to configure this plug-in the eclipse Parameter options,

Set the location of the local repository. This is an important step, because if the default eclipse repository does not match your default requirements, MAVEN will re-download your dependencies. After the configuration is complete
Import eclipse, right-click the project, and select Maven 2-> enable. Now you can go back to the previous steps and have more options, such as adding dependencies,
This will pop up a search box where you can search for dependencies and add them. The plug-in will edit the Pom. xml file for you.

The plug-in uses Maven to build your project, just as Eclipse uses ant to build the project. For more information about eclipse integrated Maven, see the eclipse integrated Maven 2.x User Guide on the maven site.

Another
On the one hand, if you are an intellij idea enthusiast, you can run the command MVN idea: idea to complete the same task. These ide tools can
Saves developers time. For example, if a developer adds some features to a project, other developers in the team only need to re-obtain the project file from the repository of the source code controller.
Saves the time required for each developer to configure the IDE.

Conclusion

Maven2.0 has many practical features and has excellent tasks. Maven
The most commendable part is the use of standard directory structure and deployment. This allows developers to adapt to different projects, and do not need to learn anything new about the structure or to build with special instructions.
Structure. Maven can be implemented through a pure script. In terms of documentation, because the project site build tool is used, you can view the current status of all development after the project build is complete.

No
Without a doubt, maven2.0 can replace ant with the scalability, ease of use, and project management of building configurations. In the next few years, we will see Maven as a standard structure
Building more technologies until some people bring the well-recognized "Advanced mouse traps ". You can download Maven from the maven project site listed below.

Resources
Matrix: http://www.matrix.org.cn
Onjava: http://www.onjava.com
Maven project site

Chris Hardin is a senior Java engineer at McLeod software.

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.