Maven Project creation: CMD Create project and import Eclipse development tool _maven Project creation

Source: Internet
Author: User
Tags pack

Recently, I wanted to build a simple framework of spring mvc+mybatis+oracle based on MAVEN management, but I was frustrated when I was working on eclipse, and then I found that we could start with the MAVEN project in the folder and then import Eclipse. To say nothing, step through it until I have the MAVEN project I need in Eclipse.

I. Download the MAVEN compressed file

Green version, download down, decompression can be used, no installation. Download address: Maven File download address

Friendship tip: Download the Maven file to put the directory structure of the best in English, to avoid some machine configuration environment variables caused problems.

such as: D:\work\apache-maven-3.2.5

Keyword: Maven local warehouse dependency management lifecycle

Two. Configure Environment variables

* Install the JDK and configure environment variables prior to configuring the MAVEN environment, "JDK environment Configuration channel"

* Open the Environment Variable dialog box;

* First configure the variable name M2_home, the key value is the directory location where the Maven file resides

* and add m2_home to the path.

* After the configuration is completed, enter mvn-version in the cmd window with a MAVEN hint indicating that the configuration was successful


Three. OK, I'm going to build the MAVEN project, and I'm going to build the MAVEN project in the D:\work\workspace directory. Open your CMD small black window, the next most of the operations are done in this area.

* Switch to the directory where the project is to be built, I'm d:\work\workspace.

1 Creating a Project

Type MVN Archetype:generate-dgroupid=com.test.maven-dartifactid=test-dpackagename=com.test.maven Press ENTER


You'll wait a long time after typing, and this is where the MAVEN repository interacts, patiently

After a while will be a string of return value code, observation stopped press ENTER. This step is to let you confirm the version of what, directly press ENTER is to choose the default (seemingly in the middle of a step to choose the number, decisive choice 2)


Probably explain the meaning of this sentence:

MVN perform MAVEN class operations using this, which can be understood as identification, MAVEN's ID

Archetype:generate The statement that creates the project, followed by the specific contents of the project

-dgroupid=com.test.maven when there are multiple projects that make up a project, ignore the meaning, and then talk about it.

-dartifactid=test defines the package name for the project, where the package is named Test

-dpackagename=com.test.maven the structure of a package created by default when creating a directory structure

Here need to pay attention to is: Some students will also use archetype:create this syntax to create, after maven3.0 version has abandoned this instruction, to archetype:generate, use create will be an error

The error message is: See this quickly change generate became


2. After successful project creation, come to D:\work\workspace directory will see MyTest project, the directory structure is mainly:

MyTest

----SRC

Code written--------Main

Java Code------------Java

------------resources to put the configuration file

Testing Code--------Test

Java Code------------Java
------------resources to put the configuration file

----Pom.xml core files, why use the back to say, in short, a large role


Next, get to know some of Maven's common instructions to help understand what MAVEN is.

3.MVN Install

Get to know maven with questions: What does this command do? Under what circumstances it is necessary to use this instruction.

When using mvn install, the cmd window must be indexed to the level of the Pom.xml folder.

* The first step is to read the Pom.xml core configuration file, according to the information in sequence to execute the process. Here's a concept: The life cycle of MAVEN, the steps that Maven runs.

* The second step is to perform the MAVEN lifecycle: Read the resource in the main directory > compile Java Classes in Java in the main directory into class files > read the configuration files in resource in the test directory > The Java class in Java in the test directory is compiled into a class file.

Note: A understanding of the lifecycle can help us to locate the problem faster in which directory file is the problem of the XML configuration file or the Java code

b The compiled class file will generate a target folder under the MyTest folder, which has a classes subfolder underneath it, which is the compiled file in the Java directory, and the Test-classes folder is the compiled file in the test directory. The target folder and the Src folder belong to the sibling directory.


* Third Step test error. The main is to perform the life cycle process whether the error


* Fourth step packing.


After the jar pack is placed in the target directory, you can of course not hit the jar pack to reach a war or other package, which is configured in the Pom.xml, followed by

* The fifth and final step, involving the concept of Maven warehouse, will be a good jar bag to throw a copy into the Maven warehouse, as for what to do, the personal understanding is as follows:

The concept of the MAVEN repository is a container that holds the jar package.

> Maven has a large Maen warehouse (inside basically what jar package has), on the network, but we can not use every time to go to the network to lead the jar pack it, which requires a local temporary MAVEN warehouse, the local warehouse does not have the network on the large library, The role of the local library is that I only need to use the jar package for my project, do not need not. This allows local projects to take jar packages directly from the local MAVEN repository at run time without impacting performance.

> Local maven warehouse in the beginning there will be some default jar package, this process is downloaded from the Internet when mvn archetype:generate this instruction, and speaking of which, you should know the reason for the long time before executing this command, of course, Only the required jar packages are downloaded here. Local projects are configured in Pom.xml by using the jar packages in the warehouse.

> seems to have picked it up, we also made a jar bag at mvn install and threw a copy into the Maven warehouse. The idea is that if other projects also refer to Java classes in the current project, simply configure the current project's jar in the Pom.xml in the other project. If the local warehouse is not in advance, the configuration is also white blind.

> Maven Warehouse The address of the current project is [GroupId + Artifactid + version] Mosaic address

Summary: The core purpose of MVN install is to pack and drop it into the Maven warehouse.

4.MVN install:install-file A separate jar package into the local repository with the MAVEN rule

In front of the MAVEN local repository concept, it contains jar packages. Now, for example, I need a spring bean jar package, but not in the local warehouse, I have a spring-bean.jar from the Internet, throw directly into the MAVEN warehouse can be used? The answer is No.


This is the directory in a Antlr-2.7.7.jar folder that is already in the warehouse. Each jar pack has a folder of its own, with a jar file for the jar package and something else in the folder. It is not possible to simply throw the Spring-bean.jar from the net into the Maven warehouse. We have to follow the rules of Maven.

Assuming I put a spring-bean-3.0.0.jar from the Internet and placed it under D, execute the following instructions to put the jar in the MAVEN repository:

MVN install:install-file-dfile=d:\spring-bean-3.0.0.jar-dgroupid=spring-bean-dartifactid=spring-bean-dversion= 3.0.0-dpackaging=jar

5.MVN Test

6.MVN Compile

Processing the configuration files in the resources file in the main directory and compiling the Java files in the main directory

7.MVN Test-compile

Process the configuration files in the resources file under the test directory and the Java files in the test directory

However, because test is dependent on the main directory, it also executes the configuration files in the resources file in the main directory and the Java files in the main directory under compilation.

8.MVN Clean

Clear the target folder

9.MVN Package

Generate the target folder and package it into

Note: The difference between MVN package and mvn install.

Both generate the target directory and package them into it. The difference is that package is simply packaged and does not throw a jar bag into the MAVEN repository.

10.MVN Eclipse:eclipse

Convert to MAVEN project that can import Eclipse development tools

If you add a dependent jar package to pom.xml at this time, eclipse will not take effect immediately because the path in the classpath is not updated. The workaround is to remove the newly generated classpath and project two files through the MVN eclipse:clean directive, and then regenerate the Maven file through MVN eclipse:eclipse ( A new Classpath and project folder is regenerated now)

This is a cumbersome operation and will be done later by the Eclipse development tool.

11. Introduction of MAVEN project in eclipse

For more information see another article: Import a maven project in eclipse




Related Article

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.