maven入门

来源:互联网 发布:淘宝儿童电动车 编辑:程序博客网 时间:2024/06/05 19:41

一、什么是maven

Maven是apache的一个顶级项目,用于项目的构建(代码的编译、运行单元测试、生成文档、打包和部署)、依赖管理和项目信息管理。

Maven最主要的体现在于2个词,“项目”和“管理”。Maven提供了一种思想让团队更科学的管理、构建项目。用配置文件的方式对项目的描述、名称、版本号、项目依赖等等信息进行描述。使之项目描述结构清晰,任何人接手的成本比较低。而且一个项目可能依赖于其他的项目和第三方的组件才能顺利完成,Maven提供了仓库的概念,让这些依赖项放进仓库中,不必每个人去开源项目的站点去苦苦搜寻了。如此人员的成本、软件维护的成本、沟通的成本、硬件的成本都降下来了。

二、maven安装

安装Maven之前需确保已经安装好jdk,并且配置好环境变量JAVA_HOME

1、apache官网下载maven安装包(目前最新版本已到3.3.9),解压安装包;

2、配置系统环境变量,新增环境变量M2_HOME = <解压路径>,path变量追加%M2_HOME%\bin;

3、添加完成后,运行mvn  -version确认是否安装成功

三、maven目录结构

maven安装完成后,会对应生成四个目录:


bin二进制文件,里面是maven的一些可执行命令;boot里面是maven的类加载器;conf配置文件,里面有个settings.xml就是maven的总配置信息;lib是maven运行时需要的类库。

下面重点介绍settings.xml

maven提供两种配置信息,一种是针对单个用户的局部配置,另一种针对所有用户的全局配置。

This is the configuration file for Maven. It can be specified at two levels:

1. User Level. This settings.xml file provides configuration for a single user,and is normally provided in ${user.home}/.m2/settings.xml.

2. Global Level. This settings.xml file provides configuration for all Maven  users on a machine (assuming they're all using the same Maven installation). It's normally provided in ${maven.home}/conf/settings.xml.

主要元素:

localRepository:表示Maven用来在本地储存信息的本地仓库的目录。

< -- localRepository

the path to the local repository maven will use to store artifacts.

Default: ${user.home}/.m2/repository-->

<localRepository>E:/maven_repo</localRepository>

interactiveMode:表示是否使用交互模式,默认是true;如果设为false,那么当maven需要用户进行输入的时候,它会使用一个默认值。

< -- interactiveMode

This will determine whether maven prompts you when it needs input. If set to false,maven will use a sensible default value, perhaps based on some other setting, for the parameter in question.

Default: true

<interactiveMode>true</interactiveMode>  -->

offline:表示是否离线,默认是false。这个属性表示在Maven进行项目编译和部署等操作时是否允许Maven进行联网来下载所需要的信息。

< -- offline

Determines whether maven should attempt to connect to the network when executing a build.This will have an effect on artifact downloads, artifact deployment, and others.

Default: false

<offline>true</offline>  -->

pluginGroups:在pluginGroups元素下面可以定义一系列的pluginGroup元素。表示当通过plugin的前缀来解析plugin的时候到哪里寻找。pluginGroup元素指定的是plugin的groupId。默认情况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。

<pluginGroups>

<pluginGroup>org.apache.tomcat.maven</pluginGroup>

<pluginGroup>org.mortbay.jetty</pluginGroup>

</pluginGroups>

proxies:其下面可以定义一系列的proxy子元素,表示Maven在进行联网时需要使用到的代理。当设置了多个代理的时候第一个标记active为true的代理将会被使用。下面是一个使用代理的例子:

<proxies>

<proxy>

<id>optional</id>

<active>true</active>

<protocol>http</protocol>

</proxy>

</proxies>

servers:其下面可以定义一系列的server子元素,表示当需要连接到一个远程服务器的时候需要使用到的验证方式。这主要有username/password和privateKey/passphrase这两种方式。以下是一个使用servers的示例:

<servers>

<server>

<id>Snapshots</id>

<username>dev</username>

<password>123456</password>

</servers>
原创粉丝点击