<parent> <groupId>cn.itcast.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent>
主要的目的:
备忘;
主要配置:
1.配置maven基本环境,熟悉maven的基本操作(clean,compile,test,site,install,deploy),
2.maven与IDE的配置
3.maven与tomacat的配置
4.maven与neuxs的配置
5.maven中的继承和聚合
主要步骤和截图
1.配置maven基本环境,熟悉maven的基本操作
下载maven的bin包和src包,可以选择直接解压到jdk所在目录,配置MAVEN_HOME并将%MAVEN_HOME%/bin加入到path系统变量中;
测试:doc窗口下 “mvn -v”
操作:在创建好的规定的目录下(HelloWorld->src->main || test->java&resource||test&resource)HelloWorld下分别执行以上命令,
会自动下载所需jar包和插件;完成了自动构建和jar的依赖,可以观察仓库repository文件夹下的变化;
2.maven与IDE(myEclipse)的配置(构建外部的安装包)
maven的安装目录
settings配置文件
3.maven与tomacat的配置
在pom.xml配置文件中添加配置以下代码,这样IDE会自动把构建后的war包放到指定的tomcat服务器中,并启动服务器进行解压;我们可以直接访问;
不用再手动去打开服务器;
<build> <!-- <finalName>ROOT</finalName> <plugins> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.3</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>E:apache-tomcat-6.0.43</home> </container> <configuration> <type>existing</type> <home>E:apache-tomcat-6.0.43</home> </configuration> </configuration> <executions> <execution> <id>cargo-run</id> <phase>install</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> --> </build>
4.maven与neuxs的配置(搭建私服)
第一步:下载nexus-webapp-1.9.2.4.war包,然后拷贝到tomcat下的webapps目录中
第二步:启动tomcat
第三步:访问http://localhost:8080/nexus/显示如下:
第四步:点击右上角“log in” ,输入username:admin 和Password:admin123登录
第六步:点击Views/Repositories 中Repositories
Nexus内置仓库说明:
(1)Maven Central:该仓库代理Maven中央仓库,其策略为Release,因此只会下载和缓存中央仓库中的发布版本构件。
(2)Releases:这是一种策略为Release的宿主类型仓库,用来部署组织内部的发布版本构件。
(3)Snapshots:这是一个策略为Snapshot的宿主类型仓库,用来部署组织内部的快照版本构件。
(4)3rd party:这是一个策略为Release的宿主类型仓库,用来部署无法从公共仓库获得的第三方发布版本构件。
(5)Public Repositories:该仓库组将上述所有策略为Release的仓库聚合并通过一致的地址提供服务。
第七步:创建宿主目录和代理仓库
- Hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库。
- 包括3rd party仓库,Releases仓库,Snapshots仓库
- Proxy:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
- Group:仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组。
第八步:创建仓库组
点击Public Repositories仓库,在Configurations栏中选取需要合并的仓库,点击箭头加到左边保存即可
第九步:下载Index索引并进行构建搜索(GAV搜索)
第十步:配置所有构件均从私服下载,在~/.m2/setting.xml中配置如下
<settings> <mirrors> <mirror> <!--此处配置所有的构建均从私有仓库中下载 *代表所有,也可以写central --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://192.168.1.100:8000/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <!—所有请求均通过镜像 --> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <!--make the profile active all the time --> <activeProfile>nexus</activeProfile> </activeProfiles>
第十一步:部署构建到Nexus,包含Release和Snapshot, 在项目根目录中pom.xml中配置:
<distributionManagement> <repository> <id>releases</id> <name>Internal Releases</name> <url>http://localhost:8080/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url>http://localhost:8080/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
第十二步:Nexus的访问权限控制,在~/m2/setting.xml中配置如下:
<!-- 设置发布时的用户名 -->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
查看我们项目发布后的结果
5.maven中的继承和聚合
类似于J2ee中 我们项目进行分层的概念,maven以面向项目对象的方式来进行对项目进行管理,我们通过建立不同的仓库或者私服来完成项目开发中所需的依赖和资源;
每个人负责不同的模块,各个模块中间是需要进行层级调用的,我们直接通过pom文件进行配置继承和聚合;
继承:
<dependencies> <dependency> <groupId>cn.itcast.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
通过这个标签,我们可以把正在开发的模块中需要的另一个模块加入到本模块中,我们对本模块进行操作的时候,会对依赖模块也进行同样的构建;
聚合:
在字模块中需要添加父模块的标识
<parent> <groupId>cn.itcast.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent>
在父模块中需要添加字模块的moddle,通过构建paren模块的时候,就会 一块构建所有的子模块;聚合更偏向于一对多,省去了对重复依赖的配置;(所以对重复的依赖或者资源的有效识别会有一个优先机制)
<modules> <module>src/Hello</module> <module>src/HelloFriend</module> <module>src/MakeFriends</module> </modules>