maven

1.maven的作用

参考地址:http://maven.apache.org/index.html

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Apache Maven是一个软件项目管理和管理工具。基于项目对象模型(POM)的概念,Maven可以中心化管理项目的构建、报告和文档。

2.下载和安装

下载地址:http://maven.apache.org/archetype/download.cgi

安装 :
注意:在maven安装之前,一定要已经安装了jdk。
只需要将maven所在的目录的bin文件夹配置到Path环境变量中,这样做的目的是在命令行的任意目录下,都可以使用mvn命令来操作。

新增环境变量MAVEN_HOME,类比JAVA_HOME,把maven所在的目录配置到这个MAVEN_HOME中
需要把maven目录的bin的目录下下配置到Path环境变量中,添加%MAVEN_HOME%/bin;

3.maven的配置

配置文件一般在maven的目录下的config的目录中,有个setting.xml,这里是全局的配置
但是也可以是在仓库下的setting.xml,默认是没有这个文件,可以拷贝maven的目录下的config的配置文件到仓库下的setting.xml

默认的加载顺序如下:

image.png

4.maven的目录结构

image.png

bin目录:包含了maven运行的一些命令脚本
boot目录:包含的是类加载器的一些东西
conf目录:configuration配置的,保存一些配置文件的
lib目录:保存jar包的

5.maven项目目录结构

项目目录结构,实际上指的是maven要求你的项目必须的一个目录层次。
约定优于配置 (Convention Over Configuration)
要去使用maven帮你进行jar包的管理,以及项目的构建和管理等等,你就要遵循maven的规定/约定
maven要求的你的工程结构必须是哪样哪样的。

image.png

6.maven配置文件之settings.xml

该文件是maven的全局配置文件

mirrors标签,表示配置镜像所在的路径的

一方面国外镜像的地址下载速度比较慢,所以可以考虑国内镜像的地址,比如阿里云的。
下面就配置几个镜像,第一个为阿里云的镜像地址

<mirror>  
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
          <mirrorOf>central</mirrorOf>          
        </mirror> 
        <mirror>
            <id>ui</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://uk.maven.org/maven2/</url>
        </mirror>
        <mirror>
            <id>osc</id>
            <mirrorOf>central</mirrorOf>
            <url>http://maven.oschina.net/content/groups/public/</url>
        </mirror>
        <mirror>
            <id>osc_thirdparty</id>
            <mirrorOf>thirdparty</mirrorOf>
            <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
        </mirror>
localRepository

表示配置仓库的地址
默认路劲下是${user.home}/.m2/repository的地址,就是用户目录下的.m2/repository的地址,如果需要变更,可以指定变更的地址

如果一个jar包已经在本地仓库中有了,注意这个jar包版本一定是要一致的才算有了,如果版本不一致,不能算作有,比如版本3.8.1,3.8.2算不同的jar,如果你自己上传jar到私服了,但是jar有更新了,再次上传到私服中,此时本地仓库的jar不会再更新,需要删除本地的jar,重新拉取最新的远程仓库的jar

其他如果有了,那么就不会再次去重复下载。

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>D:\repository</localRepository>
interactiveMode

标识maven是否需要和用户交互以获得输入
如果maven需要和用户交互以获得输入,那么则设置成true,不然则设置为false

offline

标识maven是否需要在离线模式下运行

pluginGroups

当插件的组织id(groupId)没有显示提供时,供搜寻插件组织Id的列表

proxies

用来配置不同的代理的

servers

仓库的下载和部署是在pom.xml文件中进行定义的。比如说用户名,密码需要访问远程仓库的时候,有时候需要安全认证,这个认证就可以配置在servers标签中

  <servers>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  <servers>
profiles

根据环境参数来调整构建配置的列表

7. maven配置文件之pom.xml

先给出一个缩减版的pom的基本配置

<?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.gp.springmvc</groupId>
    <artifactId>sunkang-springmvc</artifactId>
    <packaging>pom</packaging>
    <version>1.1-SNAPSHOT</version>
    <description>sunkang出品</description>
    <modules>
        <module>sunkang-web</module>
        <module>sunkang-dal</module>
        <module>sunkang-biz</module>
    </modules>

    <properties>
        <project-version>${project.version}</project-version>
        <org.springframework-version>4.3.7.RELEASE</org.springframework-version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>1.8</java.version>
        <java.encoding>UTF-8</java.encoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.gp.springmvc</groupId>
                <artifactId>sunkang-common-lib</artifactId>
                <version>${project-version}</version>
            </dependency>
            <dependency>
                <groupId>com.gp.springmvc</groupId>
                <artifactId>sunkang-dal</artifactId>
                <version>${project-version}</version>
            </dependency>
            <dependency>
                <groupId>com.gp.springmvc</groupId>
                <artifactId>sunkang-biz</artifactId>
                <version>${project-version}</version>
            </dependency>
          
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework-version}</version>
                <exclusions>
                    <!-- Exclude Commons Logging in favor of SLF4j -->
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
        
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${java.encoding}</encoding>
                    <compilerArguments>
                        <extdirs>lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
groupId,artifactId,version

由groupId,artifactId,ersion所组成的就可以唯一确定一个项目,三个标签是必填标签
groupId标识的项目组(填写公司的域名反写)
artifactId项目名称
version版本号

dependencies

这个标签就是用来配置我们项目中具体需要哪些jar包

properties

用来定义pom中的一些属性的,一般指定版本号

build

指定如何构建当前项目的,下面可以有资源标签和插件标签
source: 指定了当前构建的source目录
plugin: 指定了进行构建时使用的插件

packging

指定当前构建项目的类型
类型可以为war,jar,pom
指定为pom类型,说明这个项目包含多个子模块,需要定义父pom的打包类型为pom

modules

表明定义了哪些子模块

dependencyManagement

依赖管理,主要是在父pom声明需要用到的依赖,然后子pom再去引用这里声明的依赖,这样就不需要指明版本号
只能出现在父pom
统一版本号
声明依赖的jar(子POM里用到再引用)

dependency

举个例子

           <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework-version}</version>
                <exclusions>
                    <!-- Exclude Commons Logging in favor of SLF4j -->
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

type

默认为jar,一般可以不写

scope

默认值为compile,这个值决定了是否需要打包到target中,决定什么阶段需要
compile, 编译时需要,会打包到target中年,例如spring-core
test ,表明为测试时需要
provided ,编译时需要,不会打包到target 例如 servlet
runtime ,运行时需要,编译可能不需要,会打包到target, 例如JDBC驱动实现
system , 定义本地一些jar, 搭配systemPath一起使用,指明路劲,例如短信jar

exclusions

排除依赖的包

profiles

profies可以定义不同的环境,比如可以定义开发,测试,以及生产的环境,可以在打包的时候可以指定不同的环境,比如mvn clean package -Pdev

<profiles>  
        <profile>  
            <!-- 开发环境 -->  
            <id>dev</id>  
            <properties>  
                <env>dev</env>
            </properties>  
            <activation>  
                <!-- 默认激活该profile节点-->
                <activeByDefault>true</activeByDefault>  
            </activation> 
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/dev</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>  
        <profile>  
            <!-- 测试环境 -->  
            <id>qa</id>  
            <properties>  
                <env>qa</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/qa</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>    
        <profile>
            <!-- 生产环境 -->
            <id>online</id>  
            <properties>
                <env>online</env>
            </properties>  
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/online</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile> 
    </profiles>

补充知识:
在一个项目中,可能存在不同的不同的子模块,但是子模块之间存在一些依赖,这里就会涉及依赖仲裁,一般会遵循下面的两个原则

最短路径原则
加载先后原则

查看项目中jar的依赖关系

mvn dependency:tree > tree.txt,

8.生命周期以及常用命令

1.A Build Lifecycle is Made Up of Phases
2.A Build Phase is Made Up of Plugin Goals

image.png

可以看出,生命周期由不同的clean,default,site的不同阶段构成,而不同阶段是由插件的goals的构成

image.png
常用命令

compile 编译
clean 删除target/
test 运行test,test case junit/testNG
package 打包
install 把项目install到local repo
deploy 发本地jar发布到remote

一般组合的常用命令

mvn clean compile ,清理并编译
mvn clean package ,清楚并打包
mvn clean package -Dmaven.test.skip=true ,跳过测试,并清理打包

9.archetype 自定义模板

生成一个archetype,这里需要先定义好你的初始化的archetype,可以声明你的jar的依赖,以及子模块的结构,然后切换到父pom.xml的目录中,执行下面的语句

mvn archetype:create-from-project
cd /target/generated-sources/archetype
mvn install
从archetype创建项目 mvn archetype:generate -DarchetypeCatalog=local

·10.私服搭建

如果使用的是国外镜像,那么下载速度比较慢
如果使用的是阿里云镜像,速度还算ok,但是假如网速不好的时候,其实也是比较慢的
如果没有网的情况下更加下载不了
团队开发的时候,是不是每个人在自己的本地仓库都需要下载一份呢?
能不能大家共用一份呢?

当我们某个maven项目需要一份jar包的时候,mysql-connector.jar
先看一下你本地仓库是否有这样jar包(版本也要判断),如果有,则不下载
如果没有,则去下载,并且保存到本地仓库中,然后以后需要的话,就不需要下载了
在项目中进行开发的时候,仅仅就是将本地仓库的jar包导入到工程里面即可,再也不用
像传统原始方式那样,每个项目中都要一份单独的jar包,这样会造成jar包的冗余。

image.png
私服发布

搭建私服自行百度安装,假设我们安装好了之后,想要发布jar到私服该该怎么做
例如搭建的私服的地址为: http://localhost:8081/nexus
账号密码为:admin/admin123

需要修改setting.xml的配置文件

    <server>    
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

需要修改pom.xml

   <distributionManagement>
        <repository>
            <id>nexus-snapshots</id>
            <url>http://localhost:8081/nexus/content/repositories/releases</url>
            <uniqueVersion>true</uniqueVersion>
        </repository>
        <snapshotRepository>
            <id>nexus-releases</id>
            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>
私服下载

方法一:需要配置setting.xml的mirror配置
方法二:配置好pom.xml的profile的仓库下载配置

这里配置在dev的profile的一个仓库下载的地址,也可以配置site和prod的仓库地址,一般来说dev和site的仓库的地址有的公司要分开的,这样可以根据profile的不同配置不同的环境的仓库地址,可以实现根据环境不同拉取不同的仓库的jar

 <profiles>
     <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <!-- 默认激活该profile节点-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/dev</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
            <repositories>
                <repository>
                    <id>maven-net-cn</id>
                    <name>Maven China Mirror</name>
                    <url>http://maven.net.cn/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>

        </profile>
             <id>test</id>
            <properties>
                <env>test</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/dev</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
            <repositories>
                <repository>
                    <id>maven-net-cn</id>
                    <name>Maven China Mirror</name>
                    <url>http://maven.net.cn/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>

        </profile>
 <profiles>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270