因为看到网上诸多的公共Maven仓库,以及自己管理Jar包的不方便,趁这次搭建毕设项目环境,把创建Maven项目涉及到的步骤理一遍。
安装Nexus(以Windows32平台举例):
下载Nexus:http://www.sonatype.org/nexus/;
解压nexus-2.6.4-02-bundle.zip至任意目录;
NOTE:Nexus在一个名为Jetty的servlet容器中运行,它使用一个名为Tanuki Java Service Wrapper的本地服务包裹器启动。这个服务包裹器可以被配置成以Windows服务或Unix守护线程的形式运行Nexus。要启动Nexus,你需要为你的平台找到合适的启动脚本。要查看可用平台的列表,查看${NEXUS_HOME}/bin/jsw目录的内容。【查看原文】
因此,首先需要安装Wrapper:${NEXUS_HOME}/bin/jsw下找到相应平台,以管理员身份启动install-nexus.bat。
管理员身份(不然会有wrapper | OpenSCManager failed - Access is denied. (0x5)之类错误)运行CMD,cd进入${NEXUS_HOME}/bin/目录,nexus start启动nexus。
控制台提示wrapper | nexus started.后,即可进入http://localhost:8081/nexus/进入nexus平台,默认用户名:admin,密码:admin123。
看图快速了解Nexus:

具体的可以参考网上资料,比如这里。
安装Maven:
Nexus是用来管理构建(Artifacts)的,Maven是使用这些构建并搭建项目的;
下载Maven:http://maven.apache.org/download.cgi;
解压安装包至任意目录,在环境变量中加入M2_HOME=D:/apache-maven-3.0.4(解压目录),PATH中加入bin路径:%M2_HOME%\bin;
CMD输入命令:mvn -version检查是否正确安装;
接下来就是配置Maven了,这些操作都没有使用命令行,打开位于%M2_HOME%/conf/目录下的setting.xml文件;
主要更改以下几项,【】符号标注,注意看注释:
<!-- 【localRepository】 | The path to the local repository maven will use to store artifacts. | | Default: ~/.m2/repository --> <localRepository>D:\Maven3.0.4\repository</localRepository><!-- 【servers】 | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. |--> <servers> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | --> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server><!-- 【mirrors】 | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. |【注意:下面的url换成了xx;其实上面我们安装nexus的时候是在本机,但是实际应用时会放在服务器,所以这里用x提醒下。】 --> <mirror> <id>nexus-releases</id> <mirrorOf>*</mirrorOf> <url>http://xx.xx.xx.xx:8080/nexus/content/groups/public</url> </mirror> </mirrors><!-- 还有各种profiles、activeProfiles -->到这里,Maven基本上是就绪状态了,接下来我们为MyEclipse安装Maven插件,然后在MyEclipse支持下,建立Maven项目。
安装MyEclipse的Maven插件(MyEclipse版本为10,不同版本可能会有小差异):
我们采用MyEclipse的在线安装功能安装Maven插件;
打开MyEclipse10,MyEclipse -> MyEclipse Configuration Center -> Software,点击add site,Name:Maven,URL:http://m2eclipse.sonatype.org/sites/m2e;
安装完成之后,可以在MyEclipse的Preferences中看到如下选项:


如图进行相关设置之后,MyEclipse中的Maven插件就生效了,接下来我们就来建一个Maven项目。
MyEclipse中建立Maven项目:
还是看图吧:




接下来,就是利用pom.xml构建你的项目,项目建立会有一个默认的pom.xml,可以先学习下这个文件。pom.xml中的repositories指向刚才搭建的nexus仓库,dependencies则制定需要的具体jar包。初步可以认为pom.xml就是用来管理你需要的jar包,而不用一个个去网上搜,或者在本地自己手动维护一堆jar包;当然这只是初步,Maven还有其他方面的强大功能,不明觉厉。
后记:
从搭建Nexus开始,到安装Maven及MyEclipse插件,然后创建Maven项目,本文基本把这些步骤理了一遍。接下来在具体使用Maven的过程中肯定会有很多问题,包括使用Maven高效搭建管理项目,或者在MyEclipse环境下使用Maven会遇到一些莫名其妙的问题,这些问题希望能够和各位交流,我也会在后续的文章中补充。
如果文章对你有用,请在收藏之余“顶/赞”一下以示鼓励吧 (/ω\)
来源:oschina
链接:https://my.oschina.net/u/551773/blog/173730





