Using MAVEN to manage Java projects

Source: Internet
Author: User
Tags maven central

Beginner maven, a simple summary of learning experience, if there is no place, welcome to the great God to me correct ~

Summary divided into 6 parts

    • MAVEN Overview
    • MAVEN Installation
    • MAVEN project structure and how to create it
    • Maven configuration file Settings.xml
    • Pom.xml parsing of Project object files
    • Eclipse Plug-in installation

I. Overview of MAVEN

To implement a backend system in Java, you may need to involve many modules.

Web application server, file server, DB, and so on. To develop these modules, we need to package the jar or project download that they need to rely on, and then configure it to the classpath of the project.

These applications need to rely on local configuration, such as JDK, web container, etc. when running Unit test PR compilation or deployment, so that when we share the project, someone else will have a certain set of thresholds to use.

Maven can help us do all of the above, without having to do it by hand. 、

MAVEN considers the project an object.

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

Maven's warehouses are divided into local warehouses and remote warehouses (the central repository that MAVEN provides).

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

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

For example, arrows indicate dependencies.

Part of the underlying properties of the MAVEN project object:

① Project coordinates

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 projects from the warehouse system based on the project coordinate properties and is automatically configured to project dependencies.

② Packaging method.

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

③ the project dependency list.

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

④ Build Plugin list.

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

There are also some properties that are described behind.

Some methods of MAVEN project objects:

①MVN Compile compiling the project

②MVN Package Project

③MVN clean clears the resulting file before compiling and packaging

④MVN install the project to the local warehouse

⑤MVN Test Run code

Second, the installation of Maven

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

② extract to Maven working directory, such as D packing directory

③ Configuring 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 that the installation was successful:

Third, MAVEN project structure and project creation method

Project structure:

The first level directory src is the source directory

Secondary directory Main/java for project main code directory

The level three directory is the source directory for the project.

Second-level catalog Test/java test code catalog for the project

Level three catalog test code source Directory

The directory where the first-level directory compiles and packages the resulting files

Pom.xml file--The Project object description file, the property 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 tube.

② Fill in QuickStart parameters

  

Press ENTER to successfully create a MAVEN project.

We fill in several parameters:

GroupID is to help project objects locate in the warehouse

Artifactid determines the name of the generated project folder directory

Version is the project revision number

Package is the top-most pack name under the project source file

③ Project Creation succeeded

 

④ can also finish the command once.

Iv. maven configuration file settings.xml

This file is used by some of the warehouse-related configurations when using MAVEN, so I want to talk about this file here. Location: Maven\conf\settings.xml.

Modifying the local warehouse location

The location of the MAVEN default local repository is the user \.m2 directory. This directory is under the C disk, and our workspace is separate and inconvenient to manage. In addition, if the system has a problem, the C-disk format, our files will not be found back.

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

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

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

Configuring the MAVEN Central warehouse location

Because the motherland is very wall, sometimes access to the Central Warehouse network speed will be particularly slow or simply not available, all we have 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 at full speed.

<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 parsing

Pom.xml is a file of the MAVEN project's core, so this part of the content can be considered as a start. I'm going to describe this file in a way that describes an object.

①pom.xml Object

<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 Pom object.

② Coordinate properties

If we think that the MAVEN repository is a space, then the coordinate attribute is the x, y, and Z coordinates of the project in this space (there are just three labels that determine the coordinates, so the analogy is appropriate:)).

<groupId>com.sogou.hi</groupId><!--Anti-write company website + project name--><artifactid>hi</artifactid>< !--is typically a project name + module name  such as Mcloud.db--><version>0.0.1-snapshot</version><!--Large version number. branch version number. minor Version number  Snapshot snapshot  Alpha  beta release  stable GA release  version--

Any time you want to find an item, you just need to have these three attributes on it.

③ Project Dependency List Properties

As stated 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 also have some other properties that are actually used when the following are true:

  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId> junit</artifactid>      <version>3.8.1</version>      <scope>test</scope><!-- The scope property determines the phase of the dependent project, and test indicates that the project is only dependent on the specific http://maven.apache.org/guides/introduction/in the test code. 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> <!--to exclude dependencies on transitive relationships. For example, Ml-c relies on ml-b,ml-b to rely on ml-a, then we find that Maven relies on both A and b for ml-c, which can exclude C-a dependency-          <exclusion>              < groupid>com.sogou.ml</groupid>              <artifactId>ml-a</artifactId>          </exclusion>      </exclusions>    </dependency>  </dependencies>

④ using plug-in properties during build

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

    <!--support Project building behavior--    <build>        <!--plug-in list-        <plugins>            <!--packaged source plug-ins--            <plugin>                <!--plug-in project coordinates--                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>                <version>2.4</version>                <!-- At what stage of execution-                <executions>                    <execution>                        <phase>package</phase>                        < goals>                            <goal>jar-no-fork</goal>                        </goals>                    </execution>                </ executions>            </plugin>        </plugins>    </build>

The following configuration allows the project to start running the Web container after it is packaged, and 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 running the Web container when it's successfully packaged--                   <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 and packaged together, it is cumbersome to pack them up, and this time we can use aggregate properties.

<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 method to Pom.

Add the Modules property, this property is an array, there are some project folder path, compile and package this POM project, the modules inside the project will be compiled and packaged.

⑥ Parent-Child project Properties

To create a new MAVEN project, POM modifies the following

<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> <gro Upid>junit</groupid> <artifactId>junit</artifactId> <version>${junit.version}</ver sion> <scope>test</scope> </dependency> </dependencies></Dependencymanagement> </project> 

The same modification of the packaging method is Pom. Add the <dependencyManagement> node, under which is the project dependency property.

Dependent projects under this node do not take effect in this project, but you can specify dependencies in the subproject dependencies, which 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 rely on the project configured in the parent project.

Vi. Installing the Maven plugin for Eclipse

 If we are using a Maven plugin in eclipse, you can download and install one yourself.

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

Download and unzip to the Eclipse\dropins directory.

Open windows-preference to see Maven stating that the installation was successful.

  

Now we can create MAVEN projects, compile packages, and so on, all with the eclipse plugin.

  

Forwarding Please specify the source: http://www.cnblogs.com/tzyy/p/4768859.html

Using MAVEN to manage Java projects

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.