Maven Learning Primer--2016-2-17

Source: Internet
Author: User

First, the basic concept of Maven

What does 1.1Mawen do???

  Our first contact with Maven is generally a handy way to add a jar package to our project with Maven.

What the hell is maven doing?? To put it bluntly, MAVEN is used to manage projects, just as simple as managing projects.

A project before development we need to be prepared, including the jar package we need, and Maven has a warehouse, to use those jar package directly to use, do not run to run that to download, verry good!!! After we have developed the project, we need to package the project and get it done directly with Maven.

This is a convenient tool for us to manage the project!!!

1.2Maven function Explanation

  1. Project Construction

    

The project build process consists of "cleanup project" → "Compile project" → "Test Project" → "Generate test Report" → "Package Project" → "Deploy project" steps, which are the complete building process of a project.

  

The ideal project build is highly automated, cross-platform, reusable components, standardized, and using MAVEN can help us complete the project building process described above.

 2. Dependency Management

    is the ability to manage the dependencies between jar packages, and the relationship between jar packages in dependency graph in the pom file of the MAVEN project in MyEclipse

is not feeling very good look!!! second, maven download 2.1myeclipse comes with Maven4myeclipse  
    MyEclipse's own maven does not need to be downloaded, but needs to be activated and configured. Let's take a look at the actual operation:
activation method : window-->preference-->myeclipse-->maven4myeclipse, tick check box (Enable mave4myeclipse Features).                   

Configuration Method : Just under the node that the activation method opens, there is a MAVEN node, which expands the node, primarily configuring two nodes under it, as follows:

Installations: This node uses the built-in MAVEN by default, we need to use our installed Maven, point "add", add a maven that we externally install, note that after adding it, tick on.

User Settings: The default is the. m2/settings.xml file in the C-drive user directory, where you configure the externally installed MAVEN usage profile, as I am using the MAVEN installation directory under the Conf /settings.xml file, select the "Update Settings" button below and don't forget to click on it.

                       

  Download for 2.2maven

: http://maven.apache.org/download.cgi

  

After the download is complete, get a zipped package, unzip it, and you can see the MAVEN composition directory

  

Maven Directory Analysis

    • Bin: script containing mvn run
    • Boot: Contains the Plexus-classworlds class loader framework
    • Conf: contains settings.xml configuration file
    • LIB: Contains the Java class library required by the MAVEN runtime
    • LICENSE.txt, NOTICE.txt, README.txt a brief introduction to the MAVEN version, third-party software, etc.

Third, the installation of Maven

1, first of all to ensure that the computer has installed JDK(to JDK 1.6+ version), configure the JDK environment variables, using the following two commands to check the JDK installation.

1 Echo%java_home%2 Java-version

  

2. Unzip the Apache-maven-3.2.3-bin.zip

Unzip the Apache-maven-3.2.3-bin.zip, for example extract to the following directory ( unzip the directory is best not to have Chinese )

  

3. Set the SYSTEM environment variable: maven_home

  

  4, set the environment variable path, add%maven_home%\bin to the path, be sure to use a semicolon, separated from other values, as shown in:

  

%maven_home%\bin stands for the "E:\apache-maven-3.2.3\bin" directory.

Here I have a question is why to give MAVEN configuration environment variables, what is the role???

  I thought about the configuration of the JDK environment variable, configuring the JDK environment variable to tell the Java file where to find our JDK, and we can find it no matter which disk we are on. When we configured the MAVE environment variable, we used MAVEN to manage the project, and we could use MAVEN regardless of which disk our Maven project was on.

  5. Verify that MAVEN installation is successful

Open cmd window

  

Enter the "mvn–v" command to see information about MAVEN, as shown in:

  

The ability to present this information indicates that MAVEN's installation has been successful.

6. Set maven_opts environment variable (optional configuration)

Because the MAVEN command actually executes the Java command, the maven run parameter can be set by the java command parameter . Maven_opts environment variables are precisely used for this purpose

Maven_opts

-xms128m-xmx512m, set the minimum and maximum memory for the JVM, as shown in:

  

Iv. Simple application of Maven

(i) Use of Maven through MyEclipse

Here's how:

1, create a MAVEN project, new project, right-click New Project, tick the Quick Create option, this option can quickly create a simple MAVEN project.

2, after the project is successfully created, the project directory mainly includes: main code directory, master resource directory, test code directory, test resource directory, output directory (code compiled directory).

3, add project dependencies, find project dependencies there are usually two ways: 1. Find directly on Eclipse, 2. Locate and then copy dependent information into Pom.xml on the Nexus . Find right-click items or Pom.xml in Eclipse, enter key information lookup, select the artifacts to be introduced, and click OK to introduce them automatically.

(about the server nexus: The server is mainly used for some companies that do not have an extranet, only the local area network, we can be on a network of computers to engage in a server nexus, the need for the jar package and other things from this computer to obtain, convenient for our daily development, improve the efficiency of development. )

4, find the dependency in the Nexus, open the Nexus input key information click the Find button, find the corresponding jar in the search results, copy the lower right corner of the information to Pom.xml dependencies,.

5, run the MVN command on MyEclipse, right-click the project select Run as--"Maven clean." MVN commands have many, simple instructions under the more commonly used commands:

1. Clean is used to clear the output directory target/

2. Compile for compiling project Master code

3. Test code for compiling and running tests

4. The package accepts compiled code and packages it into a ready-to-publish mode

5. Install installs the package to the MAVEN local repository

6. Deploy to copy the final package to the remote repository

6. When you execute the above command, clean is not automatically executed because clean and the other 5 commands are different lifecycles, so you need to use the combo command.

7, run the combination command in MyEclipse, right-click Project Select Run as--"Maven build ...

Here's how to run some commands, and now let's talk about how to run the instance. Add code to the main code directory and the test directory.

8, run the command clean install, run the results, from the results can be seen, the command runs successfully, run a test and run through, at the same time, the jar is packaged into the target directory of the project, also installed on MAVEN's local repository.

9. Deploy the jar package on the Pom.xml, add some configuration to the system, and finally publish the Run command

Clean deploy, which can be used to locate any newly deployed JAR packages after publishing.

(ii) using a DOS command to operate MAVEN

4.1. Directory conventions for MAVEN projects

Mavenprojectroot (Project root directory)
|----src
| |----Main
|         | |----java--. java files that store the project
|         | |----resources--to store project resource files, such as spring, hibernate configuration file
| |----Test
|         | |----java--Store all tests. java files, such as JUnit test classes
|         | |----resources--to store project resource files, such as spring, hibernate configuration file
|----target--Project OUTPUT location
|----pom.xml----used to identify the project as a MAVEN project

4.2. Manually create a MAVEN project and use MAVEN to compile

1. Create the project root folder, for example Maven01

  

2. Create a "pom.xml" file in the Maven01 folder, as shown in:

  

The contents of the Pom.xml file are as follows:

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <project xmlns= "http://maven.apache.org/POM/4.0.0"  3 xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"  4 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0  5 http://maven.apache.org/xsd/maven-4.0.0.xsd "> 6     <!--all MAVEN projects must be configured with these four configuration items--7     < Modelversion>4.0.0</modelversion> 8     <!--groupId refers to a project group with the project name, which by default is the package name--9     <groupId> Cn.gacl.maven.hello</groupid>10     <!--Artifactid refers to one of the modules in the project, the default naming method is "project name-module name"-->11     < Artifactid>hello-first</artifactid>12     <!--version refers to versions, which use the snapshot version of Maven-->13     < Version>snapshot-0.0.1</version>14 </project>

3. To write Java class files, the MAVEN project stipulates that all *.java files must be placed in the Java directory under the main directory in the SRC directory, create a src directory in the Maven01 project root directory, and then create the main directory in the SRC directory. In the main directory, create the Java folder again, as shown in:

  

Create a Hello.java file under the Java folder, as shown in

  

Write the following code in the Hello.java file:

1 public class Hello{2 public     static void Main (string[] args) {3         System.out.println ("Hello Maven"); 4     }5}

4. Using MAVEN to compile the Hello.java, first go to the project root and then compile with the command "mvn compile" as shown in:

  

  

After using MAVEN compilation, a target folder is generated under the project root directory, as shown in:

  

Open the target folder and you can see that there is a classes folder in it, as shown in:

  

The Classes folder is the. class file generated after the successful compilation, as shown in:

  

5. Use the "mvnClean" command to clear the compilation result, which is to delete the target folder compiled by the build, as shown in:

  

After executing the "mvnClean" command, the target folder will be deleted.

Maven Learning Primer--2016-2-17

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.