Maven Tutorial (1)

The current java project generally manages jar packages and versions through Maven, which itself belongs to dependency inversion, and it is more convenient to manage jar packages through Maven.

Of course, before installing maven, you need to install and configure JDK, this part is omitted.

 

Configure the maven runtime environment.

first step

First find the maven installation package at http://maven.apache.org/download.cgi. Unzip it to your desired location on your hard drive.

Then record the path of maven, for example mine is: D:\mavenFile\apache-maven-3.5.3

second step

We start configuring environment variables. There are a total of three environment variables to configure. They are M2 (running bin directory), M2_HOME (running folder location), and MAVEN_OPTS (running configuration information). For example my configuration is.

M2_HOME:D:\mavenFile\apache-maven-3.5.3

M2:D:\mavenFile\apache-maven-3.5.3\bin

MAVEN_OPTS: -Xms256m -Xmx512m

third step

Add the path of the bin directory under your Path environment variable, which can also be used under win7; %M2% instead

the fourth step

In the CMD interface, run mvn --version. You can view the current maven version, which can be known after the installation is successful.

 

Create maven project

Creating a mave project in eclipse is very simple.

In the project management box, right-click to create other.

Search for maven to create a maven Project.

You can go directly to the next step to fill in the required information to create a mavan. (I suggest you read the pom explanation below before creating maven)

You can also select create a simple project on the way. To create maven with parent class pom.

 

 

POM explanation

POM stands for Engineering Object Model. It is the basic building block when working with Maven and is an xml file. It is placed in the project root directory, and the file is named pom.xml.

The POM contains information about the project and various configuration details that Maven uses to build the project.

POMs also contain targets and plugins.

Before creating a POM, we first determine the project group (groupId), its name (artifactId) and version, these attributes are the unique identification of the project in the warehouse.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dhh</groupId>
  <artifactId>p1802</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>blog</name>
  <description>mySelf blog</description>
</project>
modelVersion: The version format of the current POM.xml, this is used to identify the 
groupId in the background: the company organization representation, it is recommended to use the company domain name. It is also the storage path of the jar package packaged by mave
artifactId: project name, your current project name.
version: version number.

All POMs inherit from a parent POM (whether or not the parent POM is explicitly defined). The parent POM, also called  the Super POM , contains some default settings that can be inherited.
Inheritance allows us to avoid some unnecessary generic configuration. Available CMD is in the project folder, run: mvn help:effective-pom . View the configuration of the parent class pom.


Maven life cycle

The life cycle of maven is generally as follows:
1. Resource copying (you can customize the resources to be copied at
this stage) 2. Compile (complete source code compilation at this stage)
3. Package configuration (create and manage jar package according to pom)
4. Install (use call to install engineering package)

 When Maven starts to build the project, the goals registered for each phase are executed in the order of the defined phase sequence. Maven has the following three standard lifecycles:

  clean

  default(or build)

  site

 

  Where clean includes pre-clean, clean and post-clean stages.

  We can use the tool directly to perform the clean operation, or CMD can execute mvn post-clean in the directory where the pom is located

  

 

  Too many lifecycle phases for default (or build). As shown in the table below, don't look carefully, if you make mistakes

 

life cycle stage describe
validate Check that the project configuration is correct and that all necessary information to complete the build process is available.
initialize Initialize build state, such as setting properties.
generate-sources Generate any source files that need to be included during the compilation phase.
process-sources Process the source code, for example, filter any value.
generate-resources Generate the resource files that need to be included in the project package.
process-resources Copy and process resource files to the destination directory in preparation for the packaging phase.
compile Compile the project source code.
process-classes Processes compiled files, such as Java Class bytecode enhancements and optimizations.
generate-test-sources Generate any test source code that needs to be included in the compilation phase.
process-test-sources Process test source code, for example, filter any values.
test-compile Compile the test source code to the test destination directory.
process-test-classes Process the files generated after compilation of the test code files.
test Run the tests using an appropriate unit testing framework such as JUnit.
prepare-package Do whatever is necessary to prepare for packaging before actually packaging.
package Take the compiled code and package it in a releasable format, such as a JAR, WAR, or EAR file.
pre-integration-test Before the integration tests execute, perform the required actions. For example, set the required environment variables.
integration-test Process and deploy the necessary engineering packages into an environment where integration tests can run.
post-integration-test Perform necessary actions after integration tests are executed. For example, clean up the environment.
verify Run check operations to verify that engineering packages are valid and meet quality requirements.
install Install the project package into the local repository, which can be used as a dependency of other local projects.
deploy Copy the final project package to a remote repository to share with other developers and projects.

  The command we execute at this stage is: mvn compile

 

  The Maven Site plugin is typically used to create new report documents, deploy sites, etc. The execution command is mvn site

 

storehouse

 The maven repository refers to the place where the files are stored. divided into three

  local warehouse. Use locally.

  Central warehouse. The project team uses it by itself.

  Remote repository. Remote call.

 

local warehouse

The Maven local repository is a folder on the machine. It is created the first time you run any maven command.

The Maven local repository holds all your project's dependencies (library jars, plugin jars, etc.). When you run a Maven build, Maven will automatically download all dependent jar files to the local repository. It avoids referencing dependency files stored on remote machines every time you build.

Maven local repositories are created by default in the %USER_HOME% directory. To modify the default location, define another path in Maven's settings.xml file in the %M2_HOME%\conf directory.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>

 

中央仓库

Maven 中央仓库是由 Maven 社区提供的仓库,其中包含了大量常用的库。

中央仓库的关键概念:

  • 这个仓库由 Maven 社区管理。
  • 不需要配置。
  • 需要通过网络才能访问。

要浏览中央仓库的内容,maven 社区提供了一个 URL:http://search.maven.org/#browse。使用这个仓库,开发人员可以搜索所有可以获取的代码库。好像已经废了不太好用

 

远程仓库

如果 Maven 在中央仓库中也找不到依赖的库文件,它会停止构建过程并输出错误信息到控制台。为避免这种情况,Maven 提供了远程仓库的概念,它是开发人员自己定制仓库,包含了所需要的代码库或者其他工程中用到的 jar 文件。

 

项目构建

首先我们按之前所说创建一个maven
然后我们修改pom。xml为
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.projectgroup</groupId>
      <artifactId>project</artifactId>
      <version>1.0</version>
      <dependencies>
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
         </dependency>
      </dependencies>  
</project>

你可以看到项目自动导入了jar包

然后你加入如下代码导入servlet

      <dependency>
            <groupId>javax.servlet</groupId>
                 <artifactId>javax.servlet-api</artifactId>
                 <version>3.0.1</version>
             <scope>compile</scope>
         </dependency>

 

最后导入SpringMVC的Pom.xml如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.projectgroup</groupId>
      <artifactId>project</artifactId>
      <version>1.0</version>
      <dependencies>
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
         </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
                 <artifactId>javax.servlet-api</artifactId>
                 <version>3.0.1</version>
             <scope>compile</scope>
         </dependency>
         <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
            <version>4.1.2.RELEASE</version>
            <scope>compile</scope>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
           <version>4.1.2.RELEASE</version>
           <scope>compile</scope>
         </dependency>
      </dependencies>  
</project>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023535&siteId=291194637