MAVEN manages Java Project Instance tutorial

Source: Internet
Author: User

In addition to its feature of program building, Maven provides advanced project management tools. Because MAVEN's default build rules are highly reusable, it's often possible to build simple projects with two or three-line maven build scripts. As a result of MAVEN's project-oriented approach, many Apache Jakarta projects use Maven in their postings, and the proportion of company projects using MAVEN continues to grow.

First, MAVEN overview

To implement a backend system in Java, many modules may be involved.

Web application servers, file servers, DB, and so on. To develop these modules, we need to first package the jar packages or project downloads that they need to rely on, and then configure them into the classpath of the project.

These applications need to rely on local configurations such as JDK, web containers, etc. when running Unit test PR compilation or deployment, so that when we share the project, there is a certain threshold for others to use.

Maven can help us do all of the above work without us doing it manually. 、

Maven sees a project as an object.

MAVEN has established a warehouse system to manage these project objects.

Maven's warehouses are divided into local warehouses and remote warehouses (the central repository provided by Maven).

We can put our project a in the local warehouse through MAVEN's command, and if other project B needs to rely on project A, it can be found directly in the local warehouse.

If our project depends on some other people's projects, such as JDBC, Mybatis,maven will be based on our project attributes in the local warehouse to find, if we can not find the central warehouse to find.

In the following illustration, the arrows represent dependencies.


Some of the underlying properties of the MAVEN project object:

① Project coordinates

The project coordinates are used to locate the location of an item in the warehouse. When Project B relies on project A or JDBC, MAVEN looks for the project from the warehouse system based on the project's coordinate properties and automatically configures it to the project dependencies.

② packing method.

This property determines what file the project will be packaged after we execute the package method of Maven.

③ List of project dependencies.

The value of this property is an array with some coordinates for the other items.

④ build a list of plug-ins.

This property is an array that defines some of the plug-ins that the project needs, such as the source Pack tool, the Web container

There are also some attributes that are described behind.


Some methods of the MAVEN project object:

①MVN Compile compilation Project

②MVN Package Packaging Project

③MVN clean Cleanup before compiling and packaging the resulting files

④MVN Install install the project to the local warehouse

⑤MVN test to run testing code


Ii. installation of Maven

① Access http://maven.apache.org/download.html Download the latest version

② extract to Maven working directory, such as D-Packing directory

③ Configure MAVEN Environment variables

maven_home:d:\apache-maven-3.0.2
MAVEN:%maven_home%\bin
Add%maven% in front of path;

④ Open cmd knock mvn-version, enter.

If you see the following information indicating successful installation:


III. MAVEN project structure and project creation methods

Project structure:


First level directory SRC as the source directory
Second-level directory Main/java for the project main code directory
Level Three directory is the source directory of the project
Second level directory Test/java for Project test code directory
Level three directory test Code source directory
Directory of compiled, packaged files generated by the first level directory

Pom.xml file--Project Object description file, the attribute and method configuration of the project object in front of it is inside this file.

Maven Project Creation Method

① open cmd, switch to working directory, run


Run this command for the first time maven will download a lot of dependent files, this is not a pipe.

② Fill in QuickStart parameters


Press ENTER so that a MAVEN project is created successfully.

We fill in several parameters:

GroupID is the Help project object is positioned in the warehouse

Artifactid determines the name of the generated project folder directory

Version is the project edition number

Package is the topmost package name under the project source file

③ Project created successfully


④ can also knock the order out at once.


IV. MAVEN config file settings.xml

Some of the configuration related to the warehouse when using MAVEN is used in this file, so I want to talk about it here. Location: Maven\conf\settings.xml.

① Modify Local Warehouse location

The location of the MAVEN default local warehouse is the user \.m2 directory. This directory under C disk, and our work area is separate, inconvenient management. In addition, if there is any problem with the system, C disk format, we can not find the file back.

<localRepository>D:/java/maven_repo</localRepository>

Modify this property to modify the location of the local warehouse.

After modifying the settings.xml, it is best to copy one in the local warehouse to avoid the configuration file loss after the Maven reload


② Configure MAVEN Central warehouse location

Because the motherland is very wall, sometimes access to the central warehouse speed will be particularly slow or simply not available, all we want to configure a MAVEN central warehouse in the domestic mirror server.

I am using the mirror server provided by Oschina, and the project is quite fast.

<mirrors>
<mirror>
<id>nexus-osc</id>
<mirrorOf>central</mirrorOf>
<name>nexus osc</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
</mirrors>

v. Maven Project object configuration file Pom.xml resolution

Pom.xml is one of the core files of the MAVEN project, so this part of the content can be a start. I'd still describe the document in terms of the description of an object.

①pom.xml objects

<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" >
<!--Specifies the version of the current POM-->
<modelVersion>4.0.0</modelVersion>
</project>

This is Pom.xml's shell, I'll call it the Pom object.

② Coordinate properties

If we think that the Maven warehouse is a space, then the coordinate property is the x, y, z coordinates of the project in this space (just three of the labels that determine the coordinates, so the analogy is right:)).

<groupId>com.sogou.hi</groupId><!--the company URL + project name-->
<artifactId>hi</artifactId><!--is generally the project name + module name such as Mcloud.db-->
<version>0.0.1-SNAPSHOT</version><!--version number. Version number. Minor version number SNAPSHOT snapshot Alpha beta test release stable GA version- ->

Any time you want to find a project, you just need to have these three attributes.

③ Project Dependencies List Properties

As described in the overview, this property is an array, so the representation in the XML file is as follows:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.sogou.ml</groupId>
<artifactId>ml-b</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

This dependency list property indicates that the project relies on two projects, JUnit and Ml-b. Project dependencies There are other attributes that are really used in the following way:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope><!--The Scope property determines at what stage of the dependent project, test indicates that the project relies only on specific see http://maven.apache.org/guides/in the test code Introduction/introduction-to-dependency-mechanism.html#dependency_scope-->
</dependency>
<dependency>
<groupId>com.sogou.ml</groupId>
<artifactId>ml-b</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions> <!--Exclude the dependency of the pass-through relationship. For example, Ml-c relies on ml-b,ml-b to rely on ml-a, then we find that Maven lets Ml-c rely on both A and B, which excludes C to a dependency-->
<exclusion>
<groupId>com.sogou.ml</groupId>
<artifactId>ml-a</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

④ properties for using Plug-ins during build

This configuration will allow our project to run MVN package packaging while packaging the source code, the original target directory will only appear Xxx.jar, now there will be a Xxx-source.jar

<!--provide appropriate support for project building behavior-->
<build>
<!--plugin list-->
<plugins>
<!--packaged source code plugin-->
<plugin>
<!--plug-in project coordinates-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<!--at what stage to execute-->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

The following configuration allows you to start running the Web container after the project has been packaged, if the Web project can access the site in the browser.

<plugins>
<plugin>
<!--<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.15.v20140411</version>-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
< start the web container--> when the package is successful!--
<execution>
<phase>package</phase>
<goals>
<goal>
Run
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

⑤ Project Aggregation Properties

If we have several projects that need to be compiled together and packaged and packaged in a cumbersome package, we can use aggregation attributes at this time.

<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.sogou.ml</groupId>
<artifactId>ml-all</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>ml-all</name>
<url>http://maven.apache.org</url>
<modules>
<module>
.. /ml-a
</module>
<module>
.. /ml-b
</module>
<module>
.. /ml-c
</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Create a new Pom file and modify the packaging packaging to Pom.

Add Modules property, this property is an array, there are some path to the project folder, compile package This pom project, will be modules inside the project compiled package.

⑥ Parent-Child project Properties

To create a new MAVEN project, the POM is modified as follows

<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.sogou.ml</groupId>
<artifactId>ml-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>ml-parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>

The same method of packing is the POM. Add <dependencyManagement> node, under this node is the project dependency property.

The dependencies under this node do not take effect in this project, but you can specify dependencies in the subproject dependencies, and the subprojects are configured as follows:

<parent>
<groupId>com.sogou.ml</groupId>
<artifactId>ml-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

This allows the subproject to depend on the project that is configured in the parent project.

Vi. Installing the Maven plugin for Eclipse

If we are using the Maven plugin in eclipse, we can download and install one ourselves.

http://download.csdn.net/detail/cnclenovo/5181671

After downloading, extract to Eclipse\dropins directory.


Open Windows-preference See MAVEN instructions installation was successful.


Now we create MAVEN projects, compile packages, and so on, and we can do it with the Eclipse plug-in.

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.