Maven knowledge Glance

Maven

Introduction and build

Introduction

Maven is based on a project object model (POM), to manage the project information can be constructed by a short description, reporting, project management tools and software documentation.

Environment to build

URL: https: //maven.apache.org/download.cgi

windows download this package:

Configuration environment variable:

M2_HOME :

path: %M2_HOME%\bin;

Open cmd verification:

Modify the configuration file in a local warehouse location:

Open setting.xml file:

Modify repository for your own folder location:

Save to close.

Small Case

Maven directory structure convention

mvn-project

​ src

​ -main

​ -java

​ -package

​ -test

​ -java

​ -package

​ resources

​ target

pom.xml

small test

Common maven command

mvn -v View maven version

mvn compile compiler

mvn test test

mvn package package

mvn clean delete target

mvn install the installation package to a local warehouse jar

Establishing maven-test directory and establishing a directory structure in which

Establish a Hallo.java last file in the main directory, as follows:

package com.imooc.maven01.model;

public class Hello{
    public String sayHello(){
        return "Hello";
    }
}

TestHello.java establish a test file in the folder last directory, as follows:

package com.imooc.maven01.model;

import org.junit.*;
import org.junit.Assert.*;
public class HelloTest{
    @Test
    public void testSayHello(){
        Assert.assertEquals("Hello",new Hello().sayHello());
    }
}

Establish a pom.xml file in the maven-test folder, as follows:

<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.imooc.maven01</groupId>
    <artifactId>maven-test</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>
</project>

In order to perform maven maven-test directory:

mvn compile

Output is as follows:

mvn test

Output is as follows:

mvn package

Output is as follows:

Performing mvn clean, output:

You can see the target folder does not exist:

Performing mvn install, output:

We can see in our local warehouse jar package already exists:

If the output is correct, on behalf of proper installation and project creation.

Sign up to the warehouse and other projects depend on

Execute maven-test directory: mvn install

After the success of a new project maven-test-2, the following directory:

And establish a Speak.java file in the last directory main, as follows:

package com.imooc.maven02.util;
import com.imooc.maven01.model.*;

public class Speak{
    public String useSayHallo(){
        return new Hello().sayHello();
    }
}

TestSpeak.java establish a test file in the last folder, as follows:

package com.imooc.maven02.util;

import org.junit.*;
import org.junit.Assert.*;
public class SpeakTest{
    @Test
    public void testSayHello(){
        Assert.assertEquals("Hello",new Speak().useSayHallo());
    }
}

pom.xml添加junit和maven-test依赖:

<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.imooc.maven02</groupId>
    <artifactId>maven-test-2</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>com.imooc.maven01</groupId>
            <artifactId>maven-test</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

执行maven test

成功,表示依赖相关操作正确。基于此功能方便在其他原有项目上构建更大的项目。

自动生成maven项目骨架

在命令行输入mvn archetype:generate,然后等待下载完相应插件根据提示输入groupId,artifactId,version然后就会自动帮你创建一个对应的maven骨架项目

结果如下:

或者直接设置所有的属性:

mvn archetype:generate -DgroupId=com.imooc.maven04 -DartifactId=maven-test-4 -Dversion1.0

-Dpackage=com.imooc.maven04.demo

然后一路回车即可完成创建

IDEA中配置Maven

当前项目配置:

为新建项目配置默认maven:

最好再更改一下下面的设置:

pom.xml解析

<project>

​   <modelVersion></modelVersion>

​   <groupId>反写公司网址+项目名</groupId>

​   <artifactId>项目名+模块名</artifactId>

​   <!--大版本.分支版本.小版本 snapshot快照,alpha内测,beta公测,Release稳定,GA正式发布-->

​   <version></version>

​   <!--默认是jar可以是war,zip,pom-->

​   <packaging></packaging>

​   <!--项目描述名-->

​   <name></name>

​   <!--项目网址-->

​   <url></url>

​   <!--项目描述-->

​   <description></description>

​   <!--开发者-->  

​   <developers></developers>

​   <licenses></licenses>

​   <organization></organization>

​   <dependencies>  

​           <dependency>

​               <groupId></groupId>

​               <artifactId></artifactId>

​               <version></version>

​               <type></type>

​               <!--依赖使用范围-->   

​               <scope></scope>

​               <!--依赖是否可选默认是false-->   

​               <optional></optional>

​               <!--排除依赖列表-->   

​               <exclusions>

​                       <exclusion></exclusion>

​               </exclusions>

​           </dependency>

​   </dependencies>

<!--依赖管理(父模块定义给子模块用)--> 

<dependencyManagement>

​   <dependencies>

​           <dependency></dependency>

​   </dependencies>

​   <build>

​       <!--插件列表-->

​       <plugin>

​               <groupId></groupId>

​               <artifactId></artifactId>

​               <version></version>

​       </plugin>

​   </build>

<!--继承父模块-->

<parent></parent>

<!--聚合多个模块编译-->

<modules></modules>

</dependencyManagement>

</project>

依赖范围

三种类路径

编译,测试,运行

scope

compile:默认范围,编译测试运行都有效

provided:在测试和编译时有效

runtime:只在测试和运行时有效

test:只在测试时有效

system:只在测试和编译时有效,与本地环境关联,可移植性较差

import:导入的范围,只使用在dependencyManagement中,表示从其他的pom中导入dependecy的配置

依赖传递

bige<--nange<--shanji(默认依赖bige可以排除)

hongxing-bige的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-bige</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-bige</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>

</project>

hongxing-nange的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-nange</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-nange</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.hongxing</groupId>
      <artifactId>hongxing-bige</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>

</project>

hongxing-shanji的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-shanji</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-shanji</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.hongxing</groupId>
      <artifactId>hongxing-nange</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

hongxing-shanji如果不想依赖hongxing-bige可以用下面的方式引入hongxing-nange

<dependency>
      <groupId>com.hongxing</groupId>
      <artifactId>hongxing-nange</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>com.hongxing</groupId>
          <artifactId>hongxing-bige</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

依赖冲突

短路优先

A-->B-->C-->X(jar)

A-->D-->X(jar)

会优先解析短的路径

上面hongxing-shanji默认的commons-io的版本会是hongxing-nange中的版本

路径长度相同,谁的dependacy先声明,谁先解析

A-->B--X(jar)

A-->D-->X(jar)

如果在A中先声明D就用D中的X.jar版本

聚合

可以建立一个聚合maven项目来管理hongxing-bige,hongxing-nange,hongxing-shanji

建立一个hongxing-allmaven项目,pom.xml如下

<?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>com.hongxing</groupId>
  <artifactId>hongxing-all</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>hongxing-all</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

    <modules>
      <module>../hongxingbige</module>
      <module>../hongxingnange</module>
      <module>../hongxingshanji</module>
    </modules>
</project>

运行该项目,会将三个module的pom.xml都运行

继承

可以建立一个父maven项目来提供依赖给子项目继承,父类提供详细描述,子类只需填写groupId和artifactId即可引入依赖。

继承后各pom.xml(注意要先将父pom注册到仓库中)

hongxing-parent的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>hongxing-parent</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.11</junit.version>
  </properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

</project>

hongxing-bige的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-bige</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-bige</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <parent>
    <groupId>com.hongxing</groupId>
    <artifactId>hongxing-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>

</project>

hongxing-nage的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-nange</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-nange</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <parent>
    <groupId>com.hongxing</groupId>
    <artifactId>hongxing-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>com.hongxing</groupId>
      <artifactId>hongxing-bige</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>

</project>

hongxing-shanji的pom.xml

<?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>com.hongxing</groupId>
  <artifactId>hongxing-shanji</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hongxing-shanji</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <parent>
    <groupId>com.hongxing</groupId>
    <artifactId>hongxing-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>com.hongxing</groupId>
      <artifactId>hongxing-nange</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>com.hongxing</groupId>
          <artifactId>hongxing-bige</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

</project>

Guess you like

Origin www.cnblogs.com/yanshaoshuai/p/12006290.html