Maven应用1-项目jar的管理及项目发布
目标:通过maven实现项目lib内容管理,并实现项目的maven包管理
一、安装maven环境
1、下载maven文件(这里用的是apache-maven-3.3.3-bin.tar.gz),下载页面:http://maven.apache.org/download.cgi
2、解压后将文件放到某个安装位置如:/Users/firefly/app/
3、使用终端执行vi ~/.bash_profile 编辑当前用户的环境变量加入以下内容:
#maven –start—
export PATH=$PATH:/Users/firefly/app/apache-maven-3.3.3/bin
export MAVEN_OPTS=”-Xms256m -Xmx512m”
JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME
#maven –stop–
4、在终端执行 mvn
–version 提示下面代码说明安装成功
Apache Maven 3.3.3(7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
二、建立Eclipse与maven的关联
1,在Eclipse中找到Maven->Installations 添加本地安装的maven,并在User Settings中选择全局配置文件(一般在.m2/settings.xml或maven安装目录的conf/settings.xml)
2,在settings.xml中修改或添加下面内容:
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>zj-nexus</id>
<name>local private nexus</name>
<url>http://172.16.1.12:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
==================
<activeprofiles>
<activeprofile>nexus</activeprofile>
</activeprofiles>
==================
<mirrors>
<mirror>
<id>zj-nexus</id>
<mirrorof>central</mirrorof>
<name>central repository</name>
<url>http://172.16.1.12:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
==================
<servers>
<server>
<id>user-release</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>user-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
三、将项目与maven关联
1,项目右键->Configure->Convert to Maven Project 并设置group Id,Artifact Id,Version,Packaging
2,设置pom.xml添加与nexus服务器的关联(一个是发布版,一个是快照版)
<distributionManagement>
<repository>
<id>user-release</id>
<name>release</name>
<url>http://172.16.1.12:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>user-snapshots</id>
<name>inqORM snapshots</name>
<url>http://172.16.1.12:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
3,在项目pom.xml中的dependencies节点下添加以下内容来获取jar资源:
4,再右键项目执行Run As -> Maven Install获取jar
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
四、发布项目到maven
1,在Eclipse中Run Configuration中配置Maven Build 设置如下内容
name为 deploy
Base directory为 ${workspace_loc:/${project_name}}
Goals为 deploy -e
2,选择要发布的项目执行Run deploy实现将项目发布到快照(发布到哪个下面取决于pom中的version,
当version为xx–SNAPSHOT时发布为快照版本)