Maven 入门篇

来源:互联网 发布:网络惊魂 百度云 编辑:程序博客网 时间:2024/06/05 14:29

  • Maven简介
  • 下载安装
  • POM
    • 项目坐标
  • 基本使用
    • 创建项目骨架
      • 命令创建
      • 使用m2eclipse插件
    • 基本命令操作
      • clean
      • compile
      • test
      • package
      • install
      • 总结
    • 配置
  • 参考

Maven简介

    提供了项目构建、项目依赖解决方案。例如:jar包依赖,在没有maven之前,通常是在依赖包的官网进行下载,然后复制到lib目录下,人工去处理依赖和管理依赖,非常的麻烦,但是maven之后,只需要引入项目依赖包的坐标就可以自动下载依赖和管理依赖。    本章主要讲述maven的基本使用。具体可以参考:https://maven.apache.org/ 

下载&安装

下载地址:http://maven.apache.org/download.cgi 安装向导:http://maven.apache.org/install.html (包含了环境参数的配置)
关于安装需要注意的问题:1、本地仓库;默认安装本地仓库会安装在 **用户目录/.m2/repository**,2、全局配置/用户配置;1)全局配置:下载的包conf/settings.xml就是全局配置,一旦这里设置了,所有用户目录生效;2)用户配置:将conf/settings.xml copy到.m2/settings.xml 进行配置,只是作用在当前用户目录下;(建议使用);

POM

POM: Project Object Model, maven是通过pom.xml来构建、管理项目的。该文件用于描述项目;

项目坐标

    如何唯一定位一个项目,就像我们的住宅门牌号码,通过省份、城市、街道、门牌号码等进行划分,然后进行定位。maven自己定了一套坐标用于唯一标识项目,通常称之为“**G**(groupId)**A**(artifactId)**V**(version)”。
  • GAV
**groupId**: 一般标识公司的项目;例如:谷歌的账户项目;com.google.account, 推荐命名方式:公司.项目名;**artifactId**: 通常等价于模块,是项目的下一级,例如:账户的邮件模块,com.google.account.account-email, 推荐命名:项目名-模块名;**version**: 项目版本,只要有一套统一的版本管理就可以,不过需要特别注意的是SNAPSHOT(快照)版本;packaging: 打包方式,诸如:jar、pom、warclassifier: 附属构件,例如:spring-core-1.0.0.0-javadoc.jar,其中javadoc就是classifier生成的,不需要用户配置,通常是第三方插件设置的。默认打包:artifactId-version-[classifier].packaging, 例如:spring-core-1.0-SNAPSHOT.jar
  • 如何管理
按照GAV坐标我们可以定位项目,但是这些项目是如何管理,并提供服务 ?1、本地仓库:local环境,也就是用户自己的本地环境,即:.m2/repository2、中央仓库:默认:https://repo1.maven.org/maven2/ ;当在项目中指定了依赖之后,maven会默认先从本地仓库下载,如果没有然后从中央仓库下载,下载完成之后,会存储到本地仓库,这样所有的项目就可以使用了,需要使用的地方,只需要申明依赖引用(后续介绍);

基本使用

创建项目(骨架)

命令创建

mvn archetype:generate Choose a number: 6: Define value for property 'groupId': : study.wzp.mavenDefine value for property 'artifactId': : maven-helloDefine value for property 'version':  1.0-SNAPSHOT: :Define value for property 'package':  study.wzp.maven: : jarConfirm properties configuration:groupId: study.wzp.mavenartifactId: maven-helloversion: 1.0-SNAPSHOTpackage: jarY: : Y

使用m2eclipse(插件)

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

最终生成的目录结构:maven-study|--- src|--- --- main/java (存放java代码)|--- --- main/resources (存放资源配置文件)|--- --- test/java (存放单元测试代码)|--- --- test/resources (存放单元测试配置文件)|--- pom.xml (maven配置文件)

基本命令操作

clean

  • mvn clean
[INFO] Scanning for projects...[WARNING] [WARNING] Some problems were encountered while building the effective model for study.wzp.maven:maven-hello:jar:1.0-SNAPSHOT[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 43, column 15[WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.[WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.[WARNING] [INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building maven-hello 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-hello ---[INFO] Deleting /Users/wangzhiping/idea-workspace/maven-study/target[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 0.361s[INFO] Finished at: Tue Aug 15 15:19:15 CST 2017[INFO] Final Memory: 6M/155M[INFO] ------------------------------------------------------------------------
mvn clean 目的是清楚target目录,使用maven-clean-plugin插件[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-hello ---[INFO] Deleting /Users/wangzhiping/idea-workspace/maven-study/target

compile

  • mvn compile
INFO] ------------------------------------------------------------------------[INFO] Building maven-hello 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-hello ---[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent![INFO] Compiling 1 source file to /Users/wangzhiping/idea-workspace/maven-study/target/classes[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.634s[INFO] Finished at: Tue Aug 15 15:21:23 CST 2017[INFO] Final Memory: 12M/155M[INFO] ------------------------------------------------------------------------
注意mvn compile执行是,首先是执行maven-resources-plugin,然后再执行maven-compiler-plugin插件,因为在编译时,可能会使用资源配置文件,因此需要先将资源配置文件也先引入进来,然后再编译,编译之后将class文件存放在:/Users/wangzhiping/idea-workspace/maven-study/target/classes (target/classes)

test

  • mvn test
INFO] ------------------------------------------------------------------------[INFO] Building maven-hello 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-hello ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-hello ---[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent![INFO] Compiling 1 source file to /Users/wangzhiping/idea-workspace/maven-study/target/test-classes[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-hello ---[INFO] Surefire report directory: /Users/wangzhiping/idea-workspace/maven-study/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running study.wzp.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.699s[INFO] Finished at: Tue Aug 15 15:24:04 CST 2017[INFO] Final Memory: 13M/165M[INFO] ------------------------------------------------------------------------
执行mvn test时,会执行1,maven-resources-plugin: resources2,maven-compiler-plugin: compile3,maven-resources-plugin: testResources;4,mavne-compiler-plugin: testCompile;5,maven-surefire-plugin: test;其实很容易理解,单元测试依赖于主项目代码,因此主项目代码需要先加载资源、编译;然后再执行单元测试,因此在执行单元测试时,再加载资源、编译;最后执行单元测试。

package

  • mvn package
[INFO] ------------------------------------------------------------------------[INFO] Building maven-hello 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-hello ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-hello ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-hello ---[INFO] Surefire report directory: /Users/wangzhiping/idea-workspace/maven-study/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running study.wzp.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.074 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-hello ---[INFO] Building jar: /Users/wangzhiping/idea-workspace/maven-study/target/maven-hello-1.0-SNAPSHOT.jar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.695s[INFO] Finished at: Tue Aug 15 15:28:33 CST 2017[INFO] Final Memory: 8M/155M[INFO] ------------------------------------------------------------------------
执行package,会先执行test阶段,然后再使用maven-jar-plugin打包,并输出到:target/[INFO] Building jar: /Users/wangzhiping/idea-workspace/maven-study/target/maven-hello-1.0-SNAPSHOT.jar (jar包的命名规则就是上文提到的默认命名)

install

  • mvn install
[INFO] ------------------------------------------------------------------------[INFO] Building maven-hello 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-hello ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-hello ---[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 1 resource[INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-hello ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-hello ---[INFO] Surefire report directory: /Users/wangzhiping/idea-workspace/maven-study/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running study.wzp.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-hello ---[INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-hello ---[INFO] Installing /Users/wangzhiping/idea-workspace/maven-study/target/maven-hello-1.0-SNAPSHOT.jar to /Users/wangzhiping/.m2/repository/study/wzp/maven/maven-hello/1.0-SNAPSHOT/maven-hello-1.0-SNAPSHOT.jar[INFO] Installing /Users/wangzhiping/idea-workspace/maven-study/pom.xml to /Users/wangzhiping/.m2/repository/study/wzp/maven/maven-hello/1.0-SNAPSHOT/maven-hello-1.0-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.693s[INFO] Finished at: Tue Aug 15 15:31:08 CST 2017[INFO] Final Memory: 9M/155M[INFO] ------------------------------------------------------------------------
mvn install 的目的是将当前项目安装到本地仓库,这样本地的其他项目都可以引用;wangzhiping@wangzhipingdeMacBook-Pro ~/.m2/repository/study/wzp/maven/maven-hello/1.0-SNAPSHOT-rw-r--r--  1 wangzhiping  staff   195B  8 15 15:31 _remote.repositories-rw-r--r--  1 wangzhiping  staff   2.9K  8 15 15:30 maven-hello-1.0-SNAPSHOT.jar-rw-r--r--  1 wangzhiping  staff   1.8K  8 15 15:30 maven-hello-1.0-SNAPSHOT.pom-rw-r--r--  1 wangzhiping  staff   706B  8 15 15:31 maven-metadata-local.xml可以在.m2/repository查看已经安装。

总结

| --- clean (use maven-clean-plugin)| --- --- delete target/| --- install (use maven-install-plugin)| --- --- package (use maven-jar-plugin)| --- --- --- test (use maven-surefire-plugin)| --- --- --- --- compile (use maven-compile-plugin)| --- --- --- --- --- resources: resources| --- --- --- --- --- compiler: compile| --- --- --- --- resources: testResources| --- --- --- --- compiler: testCompile (to target/test-classes)| --- --- --- --- surefire: test| --- --- install (install to .m2/repository)

配置

<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>  <!--    项目坐标,GAV(groupId, ArtifactId, Version)- Packaging, Classifier(用于构建附属构件的,例如:javadoc.jar,sources.jar之类的)    最佳实践命名:    groupId: 一般是一家公司的项目名,例如:eleme的运单项目;me.ele.order    artifactId: 一般是项目的模块;例如:支付模块; order-payment,格式:[App].[Module]    version: 版本,一般是遵循一套版本管理模范,特别需要注意的是SNAPSHOT;    packaging: jar, pom, war 默认命名是: artifactId-version-[classifier].jar,例如:maven-hello-1.0-SNAPSHOT-javadoc.jar  -->  <groupId>study.wzp.maven</groupId>  <artifactId>maven-hello</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>jar</packaging>  <name>maven-hello</name>  <!-- 依赖管理 -->  <dependencies>    <!-- 依赖junit,使用GAV定位 -->    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>      <scope>test</scope>    </dependency>  </dependencies></project>

参考:

[1] https://maven.apache.org/guides/index.html

原创粉丝点击