What is maven-1-?

Source: Internet
Author: User

Background

1.1.

    • Scene

If you are developing two Java projects under Eclipse, let's call them a, B, where some of the features in a project depend on some of the classes in the B project, so how do you maintain that dependency?

Very simple, this is not the same as when we wrote the program, we need to use which of the project's class, that is, with someone else's written function code, import jar package. So here it is, you can make the B project into a jar package and then import the B jar file under the A project's library, so that the a project can invoke some of the classes in Project B.

    • Defects

If you find a bug in B during development, you must modify the B project and re-package B and recompile the A project after the development of project A, in order to ensure the normal operation of a, you need to rely on B (as if you must rely on another jar when using a jar), two solutions , the first option is to pack B into a, the second, to publish B, and when someone else needs a, tell the developer that you want to use a to import the Bjar package. Two are troublesome, the former may cause a waste of resources (for example, the developer may be developing other projects dependent on B, B has been stored locally, in the import of a jar, there will be two B jar), which we often encounter, find a variety of jar packages, very troublesome (with Maven is not the same)

1.2.

    • Scene

We develop a project, or do a small demo, such as using the SSH framework

    • Defects

Then we have to find and manually import the dozens of dependent jar packages that are used by the SSH framework, which is extremely cumbersome.

Defined
    • Official

MAVEN is a software project management tool that is based on the Project object Model (POM project), which can manage the construction, reporting, and documentation of a project through a short description of the information (configuration).

Popular

Get the jar package with the configuration of the Pom.xml file instead of manually adding the jar package.

Detailed

Example of writing a jar package with Pom.xml for JUnit

Join the Pom.xml file above is a project, then a project is definitely a MAVEN project, through the above three properties can find junit corresponding version of the jar package, then the JUnit project is certainly a MAVEN project, JUnit's MAVEN project Pom.xml file will have three identifiers , like this, and then other MAVEN projects can find the JUnit project's jar package through these three properties. Therefore, the three attribute values are required to be written on each MAVEN project that is created.

          

MAVEN's installation

This step of the MAVEN environment configuration, I think it is necessary to install, in order to use the command line to create a MAVEN project, and use the command line to manipulate the MAVEN project. This is not detailed here, given the link, similar to installing the JDK environment, MAVEN's installation tutorials and configuration. Also note that I use the maven3.0.4 version below (lower), you can download the latest version, preferably using jdk1.7.

Concept of Warehouse

With the configuration in Pom.xml, you will be able to get the jar package you want (you don't have to explain how to configure the repository first), but where are these jars? Where do we get these jar packages? The answer is the warehouse. Warehouses are divided into: local warehouses, third-party warehouses, central warehouses.

    • Local Warehouse

MAVEN will manage the artifacts (jar packages) that are dependent on the project from a remote download to a local directory, and the default repositories for each computer are in $user. Home/.m2/repository, for example mine is: C:\Users\Administrator\.m2\ Repository

  

              

In general, we will modify the local warehouse location, create a folder on its own, in the download from the Internet a relatively complete set of all the jar package to the local warehouse, and then write the project every time, directly from the local warehouse to take the line

There are a lot of different jar packages that we need.

Modify the local library location: modified in the $maven_home/conf/setting.xml file,

                

   

D:\java\maven\repository: The local repository we created ourselves, and all the jar packages we downloaded on the web were dropped to that directory, and we were able to get directly through MAVEN's Pom.xml file.

    • Third-party warehouses

Third-party warehouses, also known as internal central warehouses,

It is usually set up by the company itself and is only used for internal sharing. It can be used as both a corporate internals collaboration and archiving, as well as a common class library mirroring cache, reducing the frequency of external access and downloads. (used to reduce access to the central warehouse

You can use a LAN, the central warehouse must use the external network

That is, the general company will create such a third-party warehouse, to ensure that the project development, the project needs to take the jar from the warehouse, everyone's version is the same.

Note: Private, you need to configure it separately. If not configured, the default is not to use

    • Central Warehouse

Maven has a built-in remote public repository: HTTP://REPO1.MAVEN.ORG/MAVEN2

This public repository is maintained by MAVEN itself, which has a large number of common libraries and contains the most popular open source project artifacts in the world. Java is currently the main

Project-dependent jar package if the local repository is not available, it is downloaded from the central repository by default

Command

Commands to manipulate Maven Java or Web projects

    • Compilation: MVN compile--src/main/java directory Java source code compilation generated class (Target directory)
    • Test: MVN Test--src/test/java Directory compilation
    • Packaging: MVN package-Generate compressed Files: Java project #jar package; Web project #war packages, also placed in target directory
    • Cleanup: mvn Clean-delete the target directory, i.e. delete the class file, etc.
    • Installation: mvn install-Upload a compressed file (jar or war) to the local repository
    • Deployment | Release: MVN deploy--Upload compressed files
    • Freq used: mvn clean install

Other commands

    • MVN eclipse:eclipse maven's Java or Web Project Transformation Eclipse Project
    • MVN Eclipse:clean clears eclipse setup information and transitions from Eclipse project to MAVEN native project
    • MVN Idea:idea maven's Java or Web Project Transformation Idea Project
    • MVN Idea:clean clears idea setup information and transforms from idea project to MAVEN native project.
Create a MAVEN custom project using MyEclipse

Using Myeclpse to create an item you now need to configure some of Maven's information in MyEclipse, such as configuring a local repository, installing a custom maven (MyEclipse with a high version of Maven), and so on, omitted here.

1. Java Project

    • Select Maven Project, if you right-click New No, get through other

   

    • Create a simple project (skip skeleton selection)

   

    • Set up project parameters to create a Java project

   

   

    • Create a Java project result

  

2. Create a MAVEN Web project

    • Ditto
    • Ditto
    • Set the project parameters, like others, choose a different packaging method.

  

    • Create Web Project results

   

    • Possible error 1:pom.xml error

   

The results are as follows

       

Then need to update the project, will not error.

  

    • Error 2, writing JSP times wrong

      

Error message for missing Serlvet-jar and Jsp-jar packages

    

Workaround: Import the appropriate jar package using Pom.xml targeting

       

Finding the Jsp-jar package, scope's role is to represent the scope of the jar package, provided indicates that it is valid during compile and test periods, and is not valid at run time. After the detailed explanation pom.xml will explain.

   

Find the Servle-jar bag

   

Pom.xml Check, modify

   

After the modification, found that two jar packages are loaded in, the project is intact, not the error.

    

3. Create a MAVEN project

Are the same, you can modify them in the project parameters

   

Results

  

MAVEN projects are generally useless, splitting a project into multiple items in development requires the use of a MAVEN project (POM Project) to integrate other sub-projects, which is important in the next section. Very important.

4. MyEclipse maven operation

6--9 is a shortcut.

9 test, equivalent to command line MVN test

8 installation, equivalent to the command line mvn install role: upload it to the local warehouse, see the above explained

7 related source code, this does not need to explain, usually we use other jar package also associated with the source code

6 Cleaning, mvn clean

5 maven bulid execute maven command, equivalent MVN

   

4 Maven Build 5 fast operation, if not done, and 5 the same, if the operation once, will be executed directly the last 5 command, if the operation multiple times, will provide a selection box

5. Example, publish the MAVEN Web project to Tomcat run

Command: Tomcat:run

   

Accessed via the URL, and uploads the item to the local repository.

Pom.xml's dependency relationship

1. How to get coordinates (i.e., three key attribute values)

Mode 1: Use site search [from central warehouse]

    • Step 1:100 Degree Search keywords

  

    • Step Two: Enter the keyword query to get the required content, determine the required version

  

        

    • Step three, get coordinates

  

Mode 2, use the local warehouse, obtain coordinates by MyEclipse

The above has been described how to get the corresponding jar from the local warehouse, here is a brief explanation

    • Step One: Add dependency, Pom.xml file, right-click

    

    • Step two: Get coordinates

   

Common configurations for dependencies

In order to avoid not knowing what configuration properties to say, see the figure below to understand, is dependency under the property configuration, all have 9, say 7 of them.

   

GroupId, Artifactid, version is dependent on the basic coordinates, indispensable, these three can not be, all know, it is important except for these three configuration attributes need our understanding

    • Type: dependent types, such as jar package or war package, default to jar, indicates dependent jar package

Note:<type>pom.lastupdated</type> This we have encountered when adding Servlet-jar above, see lastupdated meaning is to use update description information, placeholder role, popular point, Select this type, the jar package will not be loaded, just load some of the description information of the jar package, so that other jar package in reference to him, can see some relevant hints, that's all, so he is a placeholder, just remember that his jar package will not be loaded in.

    • Optional: Whether the tag dependency is optional. Default Value False

For example, the STRUTS2 built-in log4j this logging function, is to embed the log4j inside the Struts2 jar package, and struts2 there is no log4j this thing does not matter, has it, the information more, not it, can run, Just the hint of information is relatively small, so this time, it can be optional operation, want it to, do not want, set to false.

    • Exclusions: Exclude transitive dependencies, resolve jar conflict issues

Dependency transfer means that a project relies on B projects, B projects rely on C projects, when using a project, B is also loaded in, this is the transfer of dependencies, and so on, C will also be loaded in. This has the benefit of relying on the transfer, there are disadvantages, the downside is the jar package conflict, for example, a depends on B (version 1 of b), C depends on B (b version 2), if a project requires both A and C, then A,c will pass the dependence of B to load, the problem is here, two B version is different, Loading all two will cause a conflict, and you will need to use the Exclusions property configuration. MAVEN will also have a mechanism to avoid both loading, maven default configuration in front of priority, but we still need to use exclusions to configure more reasonable, using spring bean and struts2 Spring plugin Give an example to illustrate the problem and use exclusions to solve the problem. (Spring bean and struts2 Spring plugin all need to rely on spring-core, but not the same version)

Find these two jar packages from your local repository

  

MAVEN's own solution is as follows, MAVEN default configuration in front of priority use, as follows

The Spring-beans is loaded in first, so the version of Spring-core that Spring-beans relies on is loaded in.

Load the Struts2-spring-plugin in first, then load the version of the Spring-core it relies on.

   

Use exclusions to configure, even if the Struts2-spring-plugin configuration is in front, you need to use the 3.2.0 version. You need to exclude dependencies for struts2-spring-plugin (do not use 3.0.5 dependencies)

   

Note: In this way, the version of Struts2-spring-plugin dependent Spring-core is excluded from dependency, that is, the dependent spring-core will not be loaded in, see the code to see if it meets the requirements, if not meet the requirements, need to manually modify

  

    • Scope: The scope of dependency, which means that the jar package is loaded through pom.xml, to what extent the use takes effect, including compile time, runtime, test

  

Compile: Default value, if selected, means that the current jar is used for compilation, Test, and run

Test: Indicates that only the current jar is in effect at the time of testing and that the jar package cannot be used in any other scope. For example: JUnit. Do not write here or error, because the default is the Compile,compile package expanded the test

runtime, which indicates that the current jar is used for testing and runtime, and that the jar package is not compiled. For example: JDBC driver. JDBC driver, at compile time (that is, we write code when the interface programming, there is no use of the JDBC driver package of anything, only at run time to use, so this is a typical use of the runtime This example), here do not write or error, the reason ditto

provided, which means that the current jar is used when compiling and testing, and the runtime is not using the jar. For example: Servlet-api, Jsp-api and so on. What do you mean "must be filled in"? When we created the Web project before, we wrote the servlet or JSP, we didn't import the jar package, because MyEclipse or other Ides provided us with these two jar packages, so we didn't use the servlet to report the missing jar errors during the compile-time test period. At runtime, leaving MyEclipse or another IDE is the equivalent of missing these two jar packages, but at this point Tomcat will provide us with these two jars so that we don't get an error, so these two are special. Look at the picture

1, the development stage (MyEclipse provide), see to prove that we say

Java Web 5.0 Project:

Java Web 6.0 project:

2. Operation phase (Tomcat provided)

  

So, according to this feature, if the MAVEN development project is not a Web project, then MyEclipse will not provide us with these two jar packages, we have to manually get the coordinates from the warehouse, but for the above analysis, when running, Tomcat will help us to provide these two jar packages, so we can not get the jar from the warehouse to conflict with Tomcat, then it is possible to set the scope of these two jars by provided this property, that is, during the mutation period and the test period to take effect.

This example explains the errors and solutions that were generated when you created the MAVEN Web.

System: Indicates that we manually join the jar package, not belonging to the MAVEN repository (local, third party, etc.), such a jar package belonging to the class library, only in the compilation and testing period, the runtime is invalid. Generally not

Dependency adjustment principle

This is the way Maven solves the problem of jar package collision when passing dependencies, according to two principles, the above has introduced a kind of, is the following second principle

    • First principle: path near Priority principle

A-->B-->C-->D-->X (1.6)

E-->d-->x (2.0)

Use X (2.0) because its path is closer

    • The second principle: first declaration of priority principle. That is, if the path is the same, the MAVEN default configuration is used first in the previous

A-->b--X (1.6)

C-->d--> X (2.0)

This is the same path, then if a in front, C in the back, then use X (1.6), MAVEN will first choose according to the first principle, the first principle is not, then the second principle of processing.

What is maven-1-?

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.