MAVEN (ii) creation of MAVEN projects (command, MyEclipse) and life cycle

Source: Internet
Author: User

The previous article introduced the concept of MAVEN and some information about the warehouse, and then I'll share with you the use of commands and MyEclipse to create MAVEN projects

I. Using commands to manage MAVEN projects1.1. Create a MAVEN Java project

 1) Create a folder (Mavenproject)--Press shift+ right click here to open the command window here--Create a Maven[java] project right under the folder

2) command: MVN archetype:generate-dgroupid=com.zyh.maven.quickstart-dartifactid=simple-darchetypeartifactid= Maven-archetype-quickstart

MVN: Core command

Archetype:Create: Creates a project, and now the MAVEN higher version discards the Create command and uses the Generate command.

-dgroupid=Com.zyh.maven.quickstart: What is the groupid when creating the MAVEN project, which is explained above. The general wording of the package name is used. Because the package name is the reverse of the company's domain name, unique

-dartifactid= Simple: What is the Artifactid when creating the MAVEN project, which is the project name

-darchetypeartifactid=Maven-archetype-quickstart: Represents the creation of a [maven] Java project

3) Operation premise: Need to network, must download a small file on the Internet

    

4) After successful operation

    

A Mavenjava file is generated under D:\MavenProject, which is our Maven Java project

    

1.2. Create a MAVEN Web project

  1) Command: mvn archetype:generate-dgroupid=com.zyh.maven.quickstart-dartifactid=mywebapp-darchetypeartifactid= Maven-archetype-webapp-dversion=0.0.1-snapshot

The other way around, creating a Maven Web project -darchetypeartifactid=Maven-archetype-webapp is a lot more than creating a Java project-dversion= 0.01-snapshot.

You can add this when you create a Java project, and if you do not write it, it will add 1.0-snapshot to the default.

    

2) A MyWebApp file is generated under D:\MavenProject, which is our MAVEN Web project

    

3) Maven Web project structure

    

1.3. Command operation maven Java or Web project

Note: When using the command, you must be in the root directory of the Maven Java project, and you can see the Pom.xml

1) Compile: mvn compile--src/main/java directory Java source code compilation generated class (Target directory)

    

After the compilation is successful, you can see a "target" folder in the root directory of the Mavenjava project, which is the folder that Maven helped us build after the compilation was successful.

Open the "target" folder, you can see there is a "classes" folder, "Classes" folder is the maven we compiled Java class.

    

2) Test: MVN Test--src/test/java Directory compilation

After the test is successful, you can see a "target" folder in the root directory of the Mavenjava project, which is the folder that Maven helped us build after the test was successful.

Open "target" folder, you can see there is a "classes" and "test-classes" folder

    

3) Packaging: MVN package-Generate compressed Files: Java project #jar package; Web project #war packages, also placed in target directory

Description: Packages The MAVEN Java project to the local repository for others to call

    

After the package is successful, you can see a mavenjava-1.jar in the "target" folder in the root directory of the Mavenjava project, and this mavenjava-1.jar is the jar file that Maven helped us build after the package was successful.

    

4) Installation: mvn install-Upload a compressed file (jar or war) to the local repository

    

After successful installation, the "target" folder is generated at the root of the Mavenjava project and the "target" folder is opened. You can see that there will be Mavenjava-1.jar, and this mavenjava-1.jarr is the jar file that Maven helped us build after the installation was successful.

In addition, there will be a hello-0.0.1-snapshot.jar in the warehouse where we store the jar packages that Maven downloads, so the process of MAVEN setup is actually to "clean up" → "compile" → "Test" → "pack" Then put the packaged jar in the MAVEN repository we specified to store the jar package .

See if there is a project in the local warehouse

    

You can find out why the groupId, Artifactid, and version can be used to locate a jar package in the warehouse by using the directory in the local repository, or why GroupId uses the company domain name's anti-write Does not have the same name as other items to cause the found content to be inaccurate)

5) Cleanup: mvn Clean-delete the target directory, i.e. delete the class file, etc.

6) Deployment | Release: MVN deploy--Upload compressed files

7) Use the MAVEN command in combination

MAVEN's compilation, cleanup, testing, packaging, deployment commands can be combined with several commands at the same time, common command combinations are as follows:

MVN clean compile (first cleaned in packaging)

MVN clean Install

MVN Clean Test

MVN Clean Package

Ii. Creating a Maven project using MyEclipse

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

Write an article I'll show you how to configure Maven in MyEclipse, Eclipse, and idea

2.1. Create a MAVEN Java project

  1) Select Maven Project, if right-click New No, get through other

2) Create a simple project

    

3) Set the project parameters to create the Java project

    

    

4) Create a Java project result

    

2.2. Create a MAVEN Web project

  1) The first and second steps in the front are the same.

2) Set the project parameters, the other way, choose the packaging method is different.

    

3) Create Web project results

    

4) Error Pom.xml file

    

Modify

    

5) Update Project

Project Right click Select Maven4myeclipse

    

2.3. Create a MAVEN project

  1) The first two steps are the same as above

2) Set the project parameters, the other way, choose the packaging method is different.

    

3) Results

    

MAVEN projects are generally useless and splitting a project into multiple items in development requires the use of a MAVEN project (POM Project) to consolidate other sub-projects.

2.4. MyEclipse maven Operation

  

Iii. The life cycle of Maven

The MAVEN lifecycle is designed to abstract and unify all build processes, including project cleanup, initialization, compilation, packaging, testing, deployment, and almost any build step

MAVEN has three sets of independent lifecycles, please note that this is said to be "three sets", and "independent", these three sets of life cycle are:

1) CleanLifecycle Some cleanup before making a real build.

2) The core part of theDefault Lifecycle build, compile, test, package, deploy and so on.

3) SiteLifecycle generate project reports, sites, publishing sites.

Again, they are independent, and you can simply call clean to clear the working directory and simply call site to build the site. Of course you can also run mvn clean install site to run all of these three sets of lifecycles directly .

3.1. Clean life cycle

The clean life cycle consists of a set of stages (Phase), and the commands we normally enter at the command line always correspond to a specific stage. For example, running mvn clean, this clean is a phase of the clean life cycle . There is a clean life cycle, and there is a clean phase.

The clean life cycle consists of three phases:

1) Pre-clean perform some work that needs to be done before clean

2) Clean removes all previous build-generated files

3) Post-clean perform some work that needs to be done immediately after clean

The clean in "mvn clean" is the clean in the above, and in one life cycle, all phases before it run, that is to say, "mvn clean" is equivalent to MVN pre-clean clean,

If we run MVN Post-clean, then Pre-clean,clean will be run. This is a very important rule for MAVEN, which can greatly simplify the input of the command line.

3.2. Default life cycle

The default life cycle is the most important one in the Maven life cycle, and most of the work happens in this life cycle. Here, only some of the more important and common stages are explained:

Validate Generate-sources Process-sources Generate-Resources Process-resources Copy and process the resource files to the target directory, ready to be packaged. Compile the source code of the compiled project. Process-classes Generate-test-sources Process-test-sources Generate-test-Resources Process-test-resources to copy and process the resource files to the target test directory. Test-compile compile the test source code. Process-test-classes Test runs the tests using the appropriate unit test framework.    These test codes are not packaged or deployed. Prepare- The packages package accepts compiled code and is packaged into a published format, such as a JAR. Pre-integration-Test Integration-Test Post-integration-Test Verify Install installs the package to the local repository for other projects to rely on. deploy copies the final package to a remote repository for other developers to share with the project. 

 At any stage of the run, all the stages in front of it will be run, which is why when we run MVN install, the code is compiled, tested, packaged. In addition, MAVEN's plug-in mechanism relies entirely on the life cycle of maven, so understanding the lifecycle is critical.

3.3. Site Life cycle

Site life cycle Pre-site perform some work that needs to be done before the site document is generated

1) Site-generated Project Web document

2) Post-site performs some work that needs to be done after the site document is generated and prepares for deployment

3) Site-deploy Deploy the generated site document to a specific server

The site phase and the Site-deploy phase are often used here to generate and publish Maven sites, which is a pretty powerful feature of Maven, and the manager prefers that documents and statistics are generated automatically and beautifully.

  

MAVEN (ii) creation of MAVEN projects (command, MyEclipse) and life cycle

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.