maven学习笔记

来源:互联网 发布:人工智能方向 研究生 编辑:程序博客网 时间:2024/06/09 20:27

1、基本概念
maven是项目管理工具,遵从插件思想的设计
具有跨平台、自动化、标准化等特点

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

解压,配置环境变量
执行mvn -v测试是否安装成功

3、eclipse中配置maven
可能已经自带,推荐使用自己安装的
window--preferences--maven--installations,配置自己的maven

可能还需要一步就是Window-Preferences-Java-Installed Jars-(选中使用的jdk) - Edit 
添加虚拟机参数:-Dmaven.multiModuleProjectDirectory=$MAVEN_HOME

4、常见命令
创建maven项目:mvn  archetype:generate
编译源代码:mvn compile
编译测试代码:mvn test-compile
运行测试:mvn test
打包:mvn package
安装jar:mvn install
清除项目:mvn clean
生成eclipse项目:mvn eclipse:eclipse  
生成idea项目:mvn idea:idea  

5、中央仓库:http://repo1.maven.org/maven2
私服:局域网内的中央仓库

本地仓库:中央仓库的缓存

6、生命周期
maven定义了三个相互独立的生命周期:deploy、clean、site

deploy:validate--compile--test--package--install--deploy
clean:pre-clean--clean--post-clean
项目文档::site:pre-site--site--post-site--site-deploy
生命周期阶段和插件目标(插件可以做的事)相绑定,方便书写

7、一个<dependency>元素定义一个依赖
type:依赖类型,也就是打包方式,默认为jar

scope:指定该依赖的生命周期作用域,即该依赖会被用在生命周期的哪些阶段
(1)test:只在测试时用到,如junit
(2)runtime:只在运行时用到,如数据库驱动jar
(3)provided:编译、测试时使用临时的,运行时使用JDK或web容器提供的,如servlet-api.jar
(4)compile:默认值,在编译、测试、运行时都用到

8、依赖传递冲突的解决方法
(1)深度小的优先
(2)深度相同,先配置的优先
(3)直接在pom.xml中指定

9、聚合
使用module标签整合子模块

10、继承
子项目使用parent标签继承父项目

11、私服
maven仓库管理工具
nexus是一个maven私服软件

开启nexus中央仓库自动下载插件索引

在maven的settings.xml中配置和nexus私服的关联,主要覆盖maven默认的中央仓库

<profiles>      <profile>            <id>nexus</id>            <repositories>                  <repository>                        <id>xxx</id>                        <url>http://localhost:8081/nexus/content/groups/public/</url>                        <releases><enabled>true</enabled></releases>                        <snapshots><enabled>true</enabled></snapshots>                  </repository>            </repositories>            <pluginRepositories>                  <pluginRepository>                        <id>xxx</id>                        <url>http://localhost:8081/nexus/content/groups/public/</url>                        <releases><enabled>true</enabled></releases>                        <snapshots><enabled>true</enabled></snapshots>                  </pluginRepository>            </pluginRepositories>      </profile></profiles><activeProfiles>      <activeProfile>nexus</activeProfile></activeProfiles>
说明:
profile:直译为侧面,其实就是不同的配置策略,比如Linux系统下一套配置,windows系统下另一套配置,
具体那套配置有效呢,就使用activeProfile来指定
id(profile):每套profile的标识id
repository:一般构件的仓库
id(repository):仓库标识,值任意,但不能没有
url(repository):仓库的url地址
releases:仓库中构件是否可以是releases版本
snapshots:仓库中构件是否可以是snapshots版本
pluginRepository:由于maven插件比较特殊,就单独拉出来配置,其实配置方式一样

在maven的settings.xml中配置可以访问nexus私服的用户名和密码
<servers>    <server>      <id>admin</id>      <username>admin</username>      <password>admin123</password>    </server></servers>

在pom.xml中配置nexus私服仓库
<distributionManagement><repository><id>admin</id><url>http://localhost:8081/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>admin</id><url>http://localhost:8081/nexus/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>
说明:
id:指定部署时登录仓库(nexus私服仓库)的用户名和密码
url:指定目标仓库url地址
repository:指定release仓库
snapshotRepository:指定snapshot仓库


让我们一起遨游在代码的海洋里!

原创粉丝点击