Introduction to Maven

Maven project management integration

1. Maven concept

Maven is a project management and integration tool.

The purpose is to construct a reusable, maintainable and easier-to-understand engineering comprehensive model through a central information management module (a small description file) based on the concept of Engineering Object Model (POM).

Convention over configuration: Maven provides reasonable default configuration

配置项 默认值
source code ${basedir}/src/main/java
resources   ${basedir}/src/main/resources
Tests   ${basedir}/src/test
Complied byte code  ${basedir}/target
distributable JAR   ${basedir}/target/classes

2. Purpose

as follows:

构建
文档生成
报告
依赖
SCMs
发布
分发
邮件列表

3. Principle (not yet understood)

4. Use steps

下载完maven,配置好环境变量,再eclipse中
新建项目
配置pom.xml
pom.xm->run as install

5. Knowledge points

A project can only have one pom.xml whose root tag is project

Maven uses three coordinates to locate a project: groupId, artifactId, version

SuperPOM

All POMs inherit from a parent POM (whether or not the parent POM is explicitly defined)
SuperPOM demo:

0. Environment variables
1. Create a 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.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>  
</project>
2. Execute mvn help:effective-pom (after 5 minutes...)
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:

<?xml version="1.0" encoding="GBK"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2018-04-24T22:14:00+08:00            -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project                                              -->
<!-- 'com.companyname.project-group:project:jar:1.0'                        -->
<!--                                                                        -->
<!-- ====================================================================== -->
<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>
...此处省略几百行  
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:35 min
[INFO] Finished at: 2018-04-24T22:14:01+08:00
[INFO] ------------------------------------------------------------------------

Repository:
There are three types of Maven repositories:
local (local),
central (central), and remote
(remote) .

Build a simple java project (quickStart)

mvn archetype:generate
Then enter the relevant configuration as required:
(1) You can skip the filter:
(2) Then enter as required:
a. Version number

b.groupId

c:artifactId

d:version

e:package:skipable

f: Y: Confirm

The constructed pom is as follows:

<?xml version="1.0" encoding="UTF-8"?>

<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>luhao</groupId>
  <artifactId>MyFirstMavenProject0424</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>MyFirstMavenProject0424</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


Folder structure description
consumerBanking contains src folder and pom.xml
src/main/java contains java code files under package structure (com/companyName/bank).
src/main/test contains test code files under the package structure (com/companyName/bank).
src/main/resources contains the image/properties files (in the above example, we need to create this structure manually).

Then execute: C:\Users\43608\Desktop\mavenTest\test1\MyFirstMavenProject0424>mvn clean package

external dependencies

Maven dependency management

Functional Description
Dependency reconciliation determines which dependency version will be used when multiple manually created versions are present at the same time. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.
Dependency management directly specifies that a manually created version is used. For example, when a project C includes project B in its own management module, that is, B depends on A, then A can specify the version used when B is referenced.
Dependency scopes contain dependencies at each stage of the build process.
Dependency Exclusions Any transitive dependencies can be excluded via the "exclusion" element. For example, A depends on B, and B depends on C, so A can mark C as "excluded".
Dependency Optional Any transitive dependency can be marked as optional by using the "optional" element. For example: A depends on B, B depends on C. Therefore, B can mark C as optional, so that A can no longer use C.

Guess you like

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