Maven basics and maven Basics

Source: Internet
Author: User

Maven basics and maven Basics

1. Introduction to maven

  • Maven is an open-source project of the Apache organization. It is a construction tool for a project.

2. Advantages of maven

  • Maven is not only a building tool, but also a dependency management tool and project management tool. It provides a central repository to help us automatically download components.
  • To solve problems such as increasing dependencies, version inconsistencies, version conflicts, and bloated dependencies, it uses a coordinate to precisely locate each component.
  • Maven also helps us to scatter project information in various corners, including project description, developer list, version control system, license, and defect management system address.
  • Maven also provides a free central repository for Java developers all over the world, where almost any popular open source software can be found. With the derivative tool (Nexus), we can also quickly search for it.
  • Maven has requirements on the directory structure, and the conventions are better than the configuration. switching between projects saves the learning cost.

3. How can maven be implemented?

  • Maven has two cores:
    • Dependency management: the jar package management process.
    • Project Construction: after the project code is complete, a series of operations such as compilation, testing, packaging, and deployment of the project are implemented through commands.

 

4. install and configure the local repository of maven

4.1 download maven and decompress it to the local disk (the extracted directory does not contain Chinese characters or spaces)

  • : Http://maven.apache.org/download.cgi
  • Decompress the package to a local disk.

4.2 configure Environment Variables

  • Prerequisites for installing maven: maven is developed by a Java program. Therefore, maven depends on JDK for running.
  • Configure JDK

  • Configure maven Environment Variables

 

 

4.3 Test whether maven is successfully installed

 

 

4.4 configure a local repository

  • Repository type: local repository, private server, and central repository.

 

  • Configure local repository
    • Create a folder as a local repository

 

    • Configure the local repository to let maven programs know where the repository is.
      • Configure the local repository in the settings. xml file under maven conf.

 

 

5. maven project standard directory structure

  • Src -------- source code of the project
    • Main
      • Java
      • Resources
      • Webapps
        • Some pages and other resources
        • WEB-INF
          • Web. xml
    • Test
      • Java
      • Resources
  • Target
  • Pom. xml ------ core configuration file of maven Project

 

6. Common maven commands

6.1 clean: Clear

  • Clear the target directory under the root directory of the project.

6.2 compile: compile

  • Compile the. java file in the project into a. class file.

6.3 test: unit test

  • Run the unit test class in the src/test/java directory under the root directory of the project. (The class name for unit test must be XxxTest. java)

6.4 package: package

  • For a web project, package it into a war package.
  • For a Java project, package it into a jar package.
  • Package the project in the target directory.

6.5 install: install

  • If multiple local projects share one jar package. So we need to package the current project to the local repository to make multiple projects public. If a bug occurs in this jar package, then, we only need to change the jar package in the Local warehouse. We do not need to replace the jar package in each project according to the traditional web Project.

6.6 maven project lifecycle

  • In maven, there are "three sets of lifecycles". Each set of lifecycles is independent of each other and does not affect each other. During the lifecycle of a set of commands, the preceding operations are automatically executed.
    • CleanLifeCycle: lifecycle of cleanup:
      • Clean
    • DefaultLifeCycle: Default lifecycle:
      • Compile, test, package, install, deploy
    • SiteLifeCycle: site Lifecycle
      • Site

 

7. maven integration web project

7.1 configure maven environment in MyEclipse

  • Configure the m2e plug-in. MyEclipse comes with the maven plug-in. If there is no maven plug-in, you can install it online or offline.
  • Configure the maven program.

  • Configure User Settingis to let MyEclipse know the location of the maven Repository

 

  • Index construction

 

 

 

7.2 integrate maven Servlet

 

An error occurs when you create a web project:

1. web. xml missing

 

2. JDK build 1.5: Add a plug-in to the pom. xml file.

<Build> <plugins> <! -- Set the compilation version to 1.8 --> <plugin> <artifactId> maven-compiler-plugin </artifactId> <configuration> <source> 1.8 </source> <target> 1.8 </ target> <encoding> UTF-8 </encoding> </configuration> </plugin> <artifactId> maven-war-plugin </artifactId> <version> 2.4 </version> <configuration> <version> 3.0 </version> </configuration> </plugin> </plugins> <finalName> webserver </finalName> </build>

3. Create Servlet, Servlet compilation error, cause: Missing servlet-api.jar package.

 

 

7.2.1 search for Dependencies

 

 

7.3 dependency Scope

 

  • The default value for adding a dependency range is compile.
  • Provided: it is not required to deploy Tomcat.
    • For example: the servlet-api.jar is not needed when running, because Tomcat lib has a servlet-api.jar package.

 

  • Summary: if you use the jar package that comes with Tomcat, set the scope of dependencies in the project to provided.

7.4 run the project

 

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.