Maven初识

定义

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. => Maven是一个软件项目管理和综合的工具,基于POM,从一块中心信息管理项目的构建,报告,以及文档。

目标

那么,大神们创造Maven的目的是什么呢?

  1. 1Making the build process easy 使工程build更容易
  2. Providing a uniform build system 提供一个统一的build系统
  3. Providing quality project information 提供高效的项目信息
  4. Providing guidelines for best practices development 为最好的开发实践提供准则
  5. Allowing transparent migration to new features 提供向新特性透明迁移

安装方法
1. 设置Java环境,包括JAVA_HOME参数
2. 解压maven安装包,并将根目录设置到path


Maven本身的配置
1. MAVEN_OPTS environment variable:用于更改Maven默认启动参数,比如占用内存范围 -Xms256m -Xmx512m
2. settings.xml file: 位于USER_HOME/.m2,为全局配置
3. .mvn folder:位于具体project根目录,包括maven.config and extensions.xml

有关setting的详细说明,请参考: https://maven.apache.org/settings.html


运行

mvn [options] [<goal(s)>] [<phase(s)>]

 整体顺序如下:

  1. clean - pre-clean, clean, post-clean
  2. default - validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy
  3. site - pre-site, site, post-site, site-deploy

除了mvn package,mvn validate也是比较重要的


常识概念

1. 什么是SNAPSHOT version?

是开发中的版本,发布为release版本时,会去除SNAPSHOT字样


 依赖 - dependency

Dependency Scope

1. compile: 默认范围,编译时可用,包含的所有class path可在编码时使用.
2. provided:跟compile相似,不同的是只在编译,测试时使用,实际上,jdk或者将要发布的容器环境中已经存在,所以打包时不会包含
3. runtime: 运行时使用,编译时并不检查
4. test: 测试时使用
5. system:跟provided类似,jar包环境中存在,不同的是需要明确指定位置
6. import (2.0.9之后引入):用于dependencyManagement节点,一般用于版本集中管理, 这个方案可以解决parent依赖只能有一个的问题

传递依赖: 可向被依赖主体传递(transitivity)的包括compile,runtime

type: 依赖的type属性默认为jar,也可以为war,pom等。可以通过插件来自定义工程生成的依赖类型

classifier:用于一个pom根据不同目的生成不同的包,比如支持的jdk版本不同。还有就是可以帮助生成附属产物,比如javadoc,source等

完整信息请参考:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

有关pom的完整说明,请参考:https://maven.apache.org/pom.html


dependencyManagement节点

一般用于项目的parent中,主要用于版本。

当然,dependencyManagement里包含的dependency仍然需要在子项目的依赖中明确写上,只是不用写上版本号而已。

 


 

插件 - Plugin

举例如下,使用jdk1.5来编译工程

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>  --- 插件参数配置
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
  </plugins>
</build>

具体配置插件方法请参考:https://maven.apache.org/guides/mini/guide-configuring-plugins.html


 依赖包找不到的一般原因&解决途径

1. Maven默认repository在:https://repo.maven.apache.org/maven2
2. 如果默认repository没有,就需要添加repository,或者手动下载安装(特别是对于老版本jar)
3. 手动安装方法:
mvn install:install-file -DgroupId=jnuit -DartifactId=junit -Dversion=3.8.1 -Dpackaging=jar -Dfile=/path/to/file

然后,要在pom依赖里加入该jar包属性:

<scope>system</scope>
<systemPath>${project.basedir}/lib/my-jar.jar</systemPath>

 


高阶:如何搭建Repository Manager

用处:https://maven.apache.org/repository-management.html

方法:搜集资料中


 高阶:如何发布到自定义的的repository

pom中添加

  <distributionManagement>
    <repository>
      <id>mycompany-repository</id>
      <name>MyCompany Repository</name>
      <url>scp://repository.mycompany.com/repository/maven2</url>
    </repository>
  </distributionManagement>

setting.xml中添加认证信息

  <servers>
    <server>
      <id>mycompany-repository</id>
      <username>jvanzyl</username>
      <!-- Default value is ~/.ssh/id_dsa -->
      <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
      <passphrase>my_key_passphrase</passphrase>
    </server>
  </servers>

 



另外,有以下几点需要知道:
1. Maven是基于Java的,所有Java环境变量配置是必须的

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

未完待续...

posted @ 2017-12-03 09:37  栖息之鹰  阅读(277)  评论(0编辑  收藏  举报