Article From:

Reprinted from:


I remember watching several repetitions of Maven’s teaching videos before I understood Maven. I don’t know if I am too low in understanding or how to drop it. I don’t know clearly. Now I get it clear. To write this blog is to help those who are obsessed with Maven as I do.Fortunately, after reading, you will find it so simple. It

      Reference blog: popular understanding of maven

      The article is long, and the general idea is as follows

          mavenA preliminary understanding of the three key attributes of the jar package, –&gt, and the warehouse (where the jar packages come from) –&gt, the Maven project (creating the Maven project) –&gt with the command line, and the MyEclipse to create MAV.EN project –> details of the dependency relationship in pom.xml (coordinate acquisition, positioning jar package of various attributes to explain.



A simple small problem?

    Before the explanation, 1 small questions.

      1.1、If you are developing two Java projects under Eclipse and call them A, B, and some of the functions in the A project depend on some of the classes in the B project, how do you maintain this dependency?

        It’s very simple. It’s not just as we wrote the program before, which of the items you need to use, that is, using the functional code that someone else has written, and importing the jar package. So here too, you can type the B project into a jar package, and then import the JA of B under the Library of the A project.The R file, so that the A project can call some classes in the B project.

        Do several defects in this way

          If the bug in B is discovered in the development process, the B project must be modified, and the B is repackaged and recompiled to the A project.

          After the development of the A project, in order to ensure the normal operation of A, we need to rely on B (as in the use of a jar package must rely on another jar), two solutions, the first, choose to pack B into A, the second, the B also released, when others need to use A when they need to be told.Developers, who want to use A, must be importing the Bjar package. The two is very troublesome, the former may cause a waste of resources (for example, developers may be developing other projects that rely on B, B has been stored locally, and two B jar) in the A jar package, which we often do.It’s very troublesome to find all kinds of jar packages, which is different from Maven.


      1.2、We develop a project, or do a small demo, such as the SSH framework, so we have to find and import dozens of dependent jar packages used in the SSH framework, and the super tedious.  


    The description of the above two questions is actually a matter between projects and projects.[AThe project uses all jar of SSH, that is, A projects depend on SSH].,Manual manual solution is cumbersome and inconvenient, so we use Maven to help us manage.


Two, what is Maven exactly?

    MavenIt is based on the POM project object model, a software project management tool for managing the construction, reporting and documentation of a project by a small segment of information (configuration).[Baidu encyclopedia]

    This is a big dialect, and if you don’t understand what Maven is, it doesn’t say it, and I myself think that the core function of Maven is to describe the dependencies between projects and, in general, to get the jar package through the configuration of the pom.xml file, instead of moving it hand.Add the jar package, and here the pom.xml file is a bit familiar to people who have learned a bit of Maven. How can you get the jar package through the configuration of the pom.xml? Where does the pom.xml configuration file come from? And so on and so on, we need to make clear, ifYou need to use pom.xml to get the jar package, so first the project must be the Maven project, and the Maven project can think about it, that is, the Java project and the web project are wrapped up in a layer of maven, essentially java project or Java project, w.The EB project is also a web project, but after wrapping the maven, you can use some of the functions provided by Maven (by adding jar packets through pom.xml).

    So, according to the previous paragraph, our ultimate goal is to learn how to configure the jar package we want in pom.xml, and before we have to know how to create a maven project, what is the structure of the Maven project, and the common Java, web projectWhere is the difference, and how to configure pom.xml to get to the corresponding jar package and so on, let’s know in advance how we get the desired jar through the pom.xml file, and the configuration file will be explained in detail later.

    pom.xmlGet the writing of the jar package for JUnit.


    Why can we locate a jar package through three attributes of groupId, artifactId and version?

        Adding the above pom.xml file belongs to the A project, then the A project is definitely a maven project, and through these three attributes can find the jar package for the corresponding version of the JUnit, then the JUnit project is definitely a maven project, JUnit’s mavenThe pom.xml file in the project will have three identifiers, such as the lower figure, and then other Maven projects can find the jar package for the JUnit project through these three properties. Therefore, when each Maven project is created, these three attribute values are required.





Three, Maven installation

    In this step, I think it is necessary to install the Maven environment configuration in order to create the Maven project using the command line and to use the command line to operate the Maven project. Here is not a detailed description, giving links, similar to the installation of JDK environment, Maven installation tutorial and configuration.

 Also note that I use the maven3.0.4 version below (lower). You can download the latest version, preferably using jdk1.7..

Four. The concept of warehouse

    With the configuration in pom.xml, you can get the desired jar package (without explaining how to configure the concept of a repository first), but where are these jar? Where do we get these jar packages from? The answer is the warehouse.

    The warehouse is divided into: local warehouse, third party warehouse (private clothes), central warehouse.

      4.1、Local warehouse

          MavenThe components that are dependent on the project (Jar package) will be downloaded from remote to the local directory, and the default storage of each computer is under $user.home/.m2/repository.


              For example, mine is in: C:\Users\Administrator\.m2\repository

          In general, we will modify the location of the local warehouse, create a folder, download a combination of all jar packages that have a relatively complete network from the Internet, throw them into the local warehouse, and then write the project each time directly from the local warehouse.

                There are many kinds of jar packages that we need.

          Modify local library location: modify in $MAVEN_HOME/conf/setting.xml file.



          D:\java\maven\repository:It is the local warehouse that we created, all the jar packages that are downloaded on the Internet, and all of them are lost to the directory. We can directly take the maven pom.xml file directly.


      4.2、Third party warehouse

          The third party warehouse, also known as the internal center warehouse, is also known as private clothing.

          Private clothes: generally set up by the company itself, only for internal use. It can be used as an internal component of the company to collaborate and archive, or can also serve as a public class library mirror cache, reducing the frequency of external access and downloading. (using private clothes to reduce access to central warehouses)

          Private servers can use LAN, central warehouse must use external network.

          That is, a third party warehouse is created by a general company to ensure that the project needs to be taken from the warehouse when the project is developed, and the version of each person is the same.

          Note: connect the private suit and need to be configured separately. If no private service is configured, it is not used by default


      4.3、Central warehouse

          MavenBuilt in remote public warehouse:

          This public warehouse is maintained by Maven itself, which contains a large number of commonly used class libraries, and contains the most popular open source project components in the world. At present, it is based on Java

          Project dependent jar packages are downloaded from the central repository if there is no local repository.


      Summary: the process of getting the jar package



Five. Use the command line to manage the Maven project

      5.1、Create the Maven java project

          You create a folder, click shift+ right under this folder, and click open the command line mode, so the maven[java] project created is under the folder.

          Command: MVN archetype:create -DgroupId=com.wuhao.maven.quickstart -DartifactId=simple -DarchetypeArtifactId=maven-archetype-quickstart

            mvn:Core command

            archetype:create:Create projects, and now a higher version of Maven abandoning the create command and using the generate command.

            -DgroupId=com.wuhao.maven.quickstart :What groupId is created when the Maven project is created has been explained above. The general use of the name of the package. Because the package name is unique in the domain name of the company.

            -DartifactId=simple:What is artifactId when creating the Maven project, that is, the project name.

            -DarchetypeArtifactId=maven-archetype-quickstart:It means that the [maven]java project is created

           Premise of operation: networking is needed, and a small file must be downloaded online.


          After the success of the operation


          Under D:\java\maven\demo, a simple file will be generated, which is our Maven java project.


      5.2、maven javaProject structure

            —pom.xml    Core configuration, project root
                  —java    javaSource directory
                    —resources   javaConfiguration file directory
               —java    Test source directory
               —resources   Test configuration directory


          There is a target directory in the diagram because the Java project is compiled, the source code under src/main/java will be compiled into the.Class file into the target directory, and the target is the output directory.

      5.3、Create the Maven web project

          Command: MVN archetype:create -DgroupId=com.wuhao.maven.quickstart -DartifactId=myWebApp -DartifactId=myWebApp -DarchetypeArtifaCtId=maven-archetype-webapp -Dversion=0.0.1-snapshot

          The rest is pretty much the same. If you create the Maven web project, -DarchetypeArtifactId=maven-archetype-webapp will have a -Dversion=0.01-sna more than creating Java projects.Pshot, when you create Java projects, you can add this. If you do not write, you will help 1.0-snapshot by default.


           Under D:\java\maven\demo, a myWebApp file will be generated.


      5.4、maven webProject structure

          pom.xml                 Core configuration
          src/main/java                javaSource code
          src/main/resources            javaTo configure
          src/main/webapp            myeclipse webWebRoot directory in the project
                |– WEB-INF
                   |– web.xml
          src/test                    test
          target                    Output directory


      5.5、Command operation Maven Java or web project

          Compile: MVN compile –src/main/java directory java source code compile and generate class (target directory)

          Test: MVN test –src/test/java directory compilation

          Clean up: MVN clean – delete target directory, that is, delete class files, etc.

          Packaging: MVN package – generate compressed files: Java project #jar package; Web project #war package, also placed in target directory.

          Install: MVN install — upload compressed files (jar or war) to local repository.

          Deployment release: MVN deploy — uploading compressed files to private servers

      5.6、Example: manipulate the Maven java project with commands

          Note: when using commands, you must see the pom.xml in the root directory of Maven java project.

          Description: upload the Maven java project to the local warehouse for others to call.

            Using MVN install


            See if there is a project in a local warehouse



            Through the directory in the local warehouse, you can find why groupId, artifactId, version can be located to the jar package in the warehouse, and you can also know why groupId uses the company domain name back write (because this is unique, not with other items. “The rename causes the inaccurate content to be found.


      5.7、mavenThe complete life cycle of a project, when executing the command behind the life cycle,The command of the previous step is executed automatically


          The meaning of the red label is that when we upload the project directly to the local warehouse using the MVN install command, all the steps ahead will be automatically executed, such as the compilation of source code, packaging, and so on.


      5.8、Other orders

          maven javaOr web project conversion Eclipse project

             mvn eclipse:eclipse

             mvn eclipse:clean  Clearly set eclipse information and transform from eclipse project to Maven native project.

          …Conversion of IDEA Engineering

             mvn idea:idea

             mvn idea:clean  At the same time


Six. Create a maven custom project with Myeclipse

    Before using myeclpse to create projects, you need to configure some information about Maven in MyEclipse.

      For example, configure the local warehouse, install the custom Maven (the high version of MyEclipse comes with Maven) and so on.


          1、Select Maven project, if the right button is not newly created, get it through other.


          2、Create a simple project (skip the skeleton selection)


          3、Setting up the project parameters to create a java project




          4、Create the results of the Java project



      6.2、Create the Maven web project



          3、Setting the parameters of the project is the same as other packages.


          4、Create the results of the web project


          5、It may be wrong to make a mistake in 1:pom.xml


             give the result as follows


             Then you need to update the project without making any mistake.



          6、False 2, write the JSP times wrong


            The error information is the lack of serlvet-jar and jsp-jar packages


            Solution: import the corresponding jar package with pom.xml location


              Find the jsp-jar package. The function of scope is to indicate the scope of the jar package. Provided indicates that it is valid during compile and test periods, and is invalid during runtime. It will be explained when the pom.xml is explained later.      


              Find the servle-jar package


              pom.xmlCheck, modify


              After modification, it is found that two jar packages are loaded in. The project is in good condition and no longer reported wrong.



      6.3、Create a maven project

          All the same, modify the project parameters




          mavenThe project is generally useless, in developing a project into multiple items, you need to use the Maven project (POM project) to integrate other subprojects, and the next chapter is important. Very important。


      6.4、myeclipse mavenoperation


        6–9 It’s all shortcut

          9 Test, equivalent to the command line MVN test

          8 The installation is equivalent to the command line MVN install function: upload it to the local warehouse, and explain it in detail.

          7 Related source code, this does not need to explain, usually we use other jar packages are also associated with the source code.

          6 Clean up, MVN clean

        5 maven bulid  Execute the Maven command, equivalent MVN


        4 maven build  5Quick operation

          If there is no operation, it is the same as 5

          If operated once, it will execute the last 5 command directly.

          If the operation is repeated many times, the selection box will be provided


      6.5、For example, publish the Maven web project to Tomcat

          Command: tomcat:run


          It can be accessed through the web site and will be uploaded to the local warehouse.


Seven, pom.xml’s dependence explanation (focus)

    Before we have been using pom.xml to find the most critical three attributes of jar package, groupId, artifactId, version, we should have some impression, and also know why we can find the corresponding jar packages through these three, but not a few small knowledge of them.Some attributes, such as the dependency on servlet-jar and jsp-jar above, are less understood, so this chapter makes a comprehensive analysis of the dependencies.

    7.1、How to get the coordinates (that is, three key attribute values)

      Way 1: use a web site search [from the central warehouse]

        Step one: Baidu search keyword “Maven repository”


        Step two: input keyword search to get content and confirm the required version.



        Step three, get the coordinates



       Mode 2, using the local warehouse, get coordinates through MyEclipse.

         The above has introduced how to get the corresponding jar from the local warehouse. Here is a brief description.

         Step one: add dependency, right click in pom.xml file.


         Step two: get the coordinates



      7.2、Common configuration of dependency (coordinate)

         In order to avoid any configuration attributes which do not know what to say, see the following diagram is clear, that is, under the dependency configuration of the property, there are 9, about 7 of them.


         groupId、artifactId、versionIt is essential to rely on the basic coordinates. These three can be used without knowing. The important thing is that besides these three, the configuration attributes need our understanding.

         type:Types of dependency, such as jar packages or war packages, etc.

            The default is jar, which represents the dependency of the jar package

            Note: < type> pom.lastUpdated< /type> this is the case when we add servlet-jar on it, and see that lastUpdated means using the update description information,Placeholder function, popular point, select this type, jar package will not be loaded, just load some of the description information of the jar package, so that other jar packages can see some related hints when referencing him, that’s all, so that he is a placeholder, just remember his jarThe package will not be loaded.

         optional:Whether the tag dependency is optional. Default value false

            For example, in Struts2, the built-in log4j log log function is to insert log4j into the jar package of Struts2, and Struts2 has nothing to do with log4j, and there is more information to prompt, no it, but it can run, just can run. JustThe information of the hint is relatively less, so this time, it can be optional to it. If you want it to be, do not want it, set it to false.

         exclusions:Eliminate transfer dependence and solve jar conflict problem

            The meaning of dependency transfer is that the A project relies on the B project, the B project relies on the C project, and when the A project is used, the B will be loaded in, which is the transfer dependency, and the C will be loaded in order.

            This dependency transfer is good and bad, and the bad is the conflict of jar packets. For example, A relies on B (B version 1), C depends on B (version 2 of B), and if a project needs A and C at the same time, A, C will pass the dependency and load the B. The problem isHere, the two versions of B are different. If you load two, you will cause conflicts. At this point, you need to configure the exclusions attribute. Maven will also have a mechanism to avoid loading all two, Maven default configuration in front of the priority use, but we alsoYou need to use exclusions to configure it more reasonably. Here use spring bean and Struts2 spring plugin to illustrate this problem and use exclusions to solve this problem. (springBean and Struts2 spring plugin all need to rely on spring-core, but the version is different.

            Find the two jar packages from the local warehouse


            mavenThe solution is as follows

              maven The default configuration is given priority in the front, and the following is proof.

                Load spring-beans first, so you will load the version of spring-beans dependent spring-core.


                Load struts2-spring-plugin first, then load the version of spring-core that it relies on.



            Use exclusions to configure

               Even if struts2-spring-plugin is configured in the front, you need to use the 3.2.0 version. You need to exclude dependency for struts2-spring-plugin (not using 3.0.5 dependency).


               Note: in this way, the version of the struts2-spring-plugin dependent spring-core is excluded, that is, the dependent spring-core will not be loaded, look at the code, see if it meets the requirements, and if it doesn’t meet the requirements, it needs to be done.Manual modification



         scope:Depending on the scope, it means to use the jar package loaded by pom.xml to take effect, including compile time, runtime, and testing time.


            compile:Default value, if this value is selected, indicates that the current jar is compiled, tested and run.

            test:Indicates that the current jar is valid only when testing, and the jar package can not be used in other areas. For example, JUnit. It is not written nor reported here, because the default is compile, and the compile package expands the test.

            runtime,Indicates that the current jar is used at the test and runtime, and that the jar package is not used at compile time. For example: JDBC drive. JDBC drive, at compile time (that is, when we write code, we use interface programming, the root does not use anything in the JDBC driver package, only to use at runtime, “So this is a typical example of using the value of runtime. It is neither written nor reported here, for the same reason.

            provided,Indicates that the current jar is used when compiling and testing, and the jar is not used at runtime. For example, servlet-api, jsp-api and so on. [must be filled in]

               What does it mean? When we created the web project before writing servlet or JSP, we didn’t import jar packages, because MyEclipse or other ide helped us to provide these two jar packages, built in, so we used serv during the compilation period.Let will not report the error of the lack of jar packages, and in the runtime, leaving MyEclipse or other IDE is equivalent to missing these two jar packages, but at this point Tomcat will provide us with these two jar so that we will not make a mistake, so the two are very special.Look at the picture

               1、The development phase (provided by MyEclipse) is shown below to prove what we said.

                  java web 5.0Project: 

                  java web 6.0Project:

               2、Running phase (Tomcat provided)


               So, based on this, if the Maven development project is not a web project, then MyEclipse will not provide us with the two jar packages, and we have to manually get them from the warehouse manually, but for the above analysis, when running, tOmcat will provide us with these two jar packages, so the jar package we get from the warehouse can’t conflict with the tomcat, so you can just set the scope of the two jar through the provided property, in the variation period and the test period.It will take effect.

               This example can explain the errors and solutions generated when creating Maven web above.


            system:The jar package, which we manually add, does not belong to the Maven repository (local, third parties, etc.), belonging to such a jar package that is different from the class library, which is valid only in the compilation and test period and is invalid at run time. Generally do not use


      7.3、The principle of dependence on regulation

          This is the way that Maven solves the conflict of jar packets when transferring dependencies. According to the two principles, the following second principles have been introduced.

            1、First principle: the principle of priority of the path



               Use X (2) because its path is closer

            2、The second principle: first declarations of the principle of priority. That is, if the path is the same, Maven default configuration is preferred in the front.

                    A–>B –> X(1.6)

                    C–>D–> X(2.0)

               This is the same path, so if A is in front and C is behind, then use X (1.6).

            mavenWe will first choose according to the first principle. If the first principle fails, we will deal with it according to the second principle.


Eight. Summary

      The length of this article is a little long, and it also consumes me a lot of time, because it meets some bug, can not find the reason, once want to give up, but still stick to it, this is just the introduction of maven, know how to use it, see other should understand, the real use of the real project is still or not.The next section is to explain.

      How to build a private suit?

      How to get a jar package from a private suit

      Use Maven to split and aggregate parent projects and sub modules.

      The next section is talking about these things and refuel.

Link of this Article: Maven (1) what is Maven?

Leave a Reply

Your email address will not be published. Required fields are marked *