由于看到网上诸多的公共Maven仓库,以及本身管理Jar包的不方便,趁此次搭建毕设项目环境,把建立Maven项目涉及到的步骤理一遍。 apache
安装Nexus(以Windows32平台举例): 服务器
下载Nexus:http://www.sonatype.org/nexus/;app
解压nexus-2.6.4-02-bundle.zip至任意目录;eclipse
NOTE:Nexus在一个名为Jetty的servlet容器中运行,它使用一个名为Tanuki Java Service Wrapper的本地服务包裹器启动。这个服务包裹器能够被配置成以Windows服务或Unix守护线程的形式运行Nexus。要启动Nexus,你须要为你的平台找到合适的启动脚本。要查看可用平台的列表,查看${NEXUS_HOME}/bin/jsw目录的内容。【查看原文】maven
所以,首先须要安装Wrapper:${NEXUS_HOME}/bin/jsw下找到相应平台,以管理员身份启动install-nexus.bat。ide
管理员身份(否则会有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。this
看图快速了解Nexus: url
具体的能够参考网上资料,好比这里。spa
安装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会遇到一些莫名其妙的问题,这些问题但愿可以和各位交流,我也会在后续的文章中补充。
若是文章对你有用,请在收藏之余“顶/赞”一下以示鼓励吧 (/ω\)