首页 > 其他 > 详细

Maven

时间:2019-11-11 15:40:00      阅读:82      评论:0      收藏:0      [点我收藏+]

一、Maven简介

Maven是自动化构建工具。包含:项目关系的处理,项目打包,项目发布等功能;

Maven是基于POM(项目对象模型)模型进行管理jar包。

Maven获取依赖的流程。

1.在项目中配置POM、添加其他项目(jar包)的坐标。

  Group Id(组织名) Artifact Id(项目名) version(版本)

2.项目先去本地仓库中找指定坐标项目(jar包),如果有就把这个jar包以来进行,如果没有,就去中央仓库去进行下载到本地仓库中。

二、创建第一个Maven项目。

1.手动构建不用模板

技术分享图片

 

2.配置Maven的镜像和jdk版本

由于中央仓库默认是国外的。把中央仓库地址配置成国内提升下载速度。多配置到阿里巴巴的镜像

技术分享图片

 

 在settings、xml中添加 

  mirror配置镜像

  profile配置jdk

 

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
</settings>

 

3.配置pom.xml

所有依赖的jar都放在<dependencies>标签中

每个jar就是一个<dependency>标签

所有jar的坐标可以在https://mvnrepository.com

 <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.8.RELEASE</version>
    </dependency>
</dependencies>

三、Maven项目结构

技术分享图片

 

 

四、 Maven中项目和项目之间的关系

Maven基于POM模型。把每个项目都当做一个对象进行看待,每个对象包含三个属性:Group IdArtifact IdVersion。项目和项目之间具有三种关系:依赖、继承、聚合。

依赖

所谓的依赖就是在Maven中导入其他项目的jar包。可以导入中央仓库中包含的jar,也可以导入当前自己的其他项目jar

1.1 关于依赖本机其他项目步骤

新建maven项目。

双击install把当前项目打包成jar,并安装到本地仓库中。同时也可以在当前项目的target文件夹中看见

技术分享图片

 

 

 

在另一个项目的pom.xml中添加刚才项目的依赖就可以。

 

 

 

1.2 依赖的传递性

假设项目B依赖了项目A,如果项目C依赖了项目B,项目C也可以使用项目A中内容。这也是为什么导入Spring-context时出现了很多其他jar的原因。

 

 

 

1.3 取消依赖传递性

很多情况会出现依赖的版本是我们不能使用的版本,通过在pom.xml中通过<exclusions>取消依赖传递性。在通过<dependency>重新进行依赖其他版本。

 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.8.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>

 

 

继承

如果项目A继承了项目B,在项目Bpom.xml中配置的内容会被继承(父项目可以控制是否被继承)。

继承关系只能在子项目中体现出来。在子项目的pom.xml中会有<parent>标签。

2.1 父项目对继承的控制

可以通过<dependecyManagement>控制里面的内容是子项目可能使用的jar包。里面的内容在子项目中不会自动被依赖,如果子项目需要依赖此jar,需要手动进行<dependecy>但是坐标中可以没有<version>.

在父项目中一旦使用<dependecyManagement>多结合<properties>标签进行版本控制

<!-- 统一的版本管理 -->
<properties>
    <spring-context-version>5.1.7.RELEASE</spring-context-version>
</properties>

<!--子项目可能使用 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-context-version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<!-- 所有子项目一定会被导入-->
<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
</dependencies>

在子项目中不需要写version

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
</dependencies>

3 聚合

聚合是继承的特例。聚合的前提是继承。

聚合是父项目直接把子项目包含在当前项目中。而继承只是Maven控制项目和项目直接具有继承关系,父项目文件夹中没有子项目。

聚合关系时不仅仅子项目中有<parent>父项目中还具有<modules>

 

<modules>
    <module>child</module>
    <module>child2</module>
</modules>

五、 项目类型

Maven中项目一共分为3中类型。pomjarwar默认是jar类型。可以通过<packaging>进行配置。

 

jar

 

表示项目打包后为jar类型。表示当前项目为普通java项目。

 

war

 

表示项目打包后为war类型。表示当前项目为web项目。如果是web项目必须修改这个值为war,否则无法打包webapp文件夹。

 

pom

 

当项目是父项目时项目类型为pom。在idea中一旦给一个项目创建module后会自动把父项目的<packaging>修改为pom 

 

六、 scope

 

<scope><dependency>子标签,控制这个jar什么时候生效。

 

可取值:

 

compile:默认值,编译运行等都需要

 

provided:只有编译时使用

 

runtime:只有运行时使用

 

test:只有在测试的使用使用

 

七、 资源拷贝插件

 

Maven项目,Maven会对java文件夹中.java进行编译,其他文件不编译。resources里面内容会直接放入到 classes中,Maven认为所有的资源文件(.java文件)应该都放入到resouces下。

 

一旦配置了资源拷贝插件后,Maven将不自动把src/main/resources下的内容进行编译拷贝。

 

资源拷贝插件是配置在<build>下的

 

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

 

 

 

 

 

 

 



Maven

原文:https://www.cnblogs.com/wzh-java/p/11834801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!