maven 实用插件以及搭建本地仓库服务器

使用maven插件:






下面的${user.home}是一个系统变量,指向我们的用户目录,windows系统默认是C:\Users\Administrator;Linux默认在~或者/home/ubuntu。

maven的安装与maven本地仓库的搭建过程

maven的安装

-----------------------

         1.下载并解压apache-maven-3.3.9-bin.tar.gz到/soft下.

         2.创建符号连接

                   $>cd /soft

                   $>ln -s apache-maven-3.3.9maven

         3.配置maven的环境变量

                   [/etc/environment]

                   M2_HOME=/soft/maven

                   PATH=...:/soft/maven/bin

        

win7宿主机上搭建maven本地仓库服务器

---------------------------------------

         1.安装tomcat

                   下载tomca压缩包t解压即可

         2.安装nexus.war文件.

                   a.复制nexus.war到${tomcat_home}/webapps/

                   b.启动tomcat

                   c.http://localhost:8080/nexus/

        

         3.配置maven的settings.xml文件,连接到本地仓库服务器.

                   注意:maven安装在ubuntu上

                        nexus安装在win7上.

                   [/soft/maven/conf/settings.xml]

                   <?xmlversion="1.0" encoding="UTF-8"?>

                   <settingsxmlns="http://maven.apache.org/SETTINGS/1.0.0"

                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                                      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">

                     <pluginGroups>

                     </pluginGroups>

                     <proxies>

 

                     </proxies>

                            <servers>

                                     <server>

                                     <id>releases</id>

                                     <username>admin</username>

                                     <password>admin123</password>

                                     </server>

                                     <server>

                                     <id>snapshots</id>

                                     <username>admin</username>

                                     <password>admin123</password>

                                     </server>

                            </servers>

                            <mirrors>

                                     <mirror>

                                     <id>nexus</id>

                                     <mirrorOf>*</mirrorOf>

                                     <url>http://192.168.231.1:8080/nexus/content/groups/public/</url>

                                     </mirror>

                            </mirrors>

                            <profiles>

                                     <profile>

                                     <id>nexus</id>

                                     <repositories>

                                     <repository>

                                               <id>central</id>

                                               <url>http://192.168.231.1:8080/nexus/content/repositories/central/</url>

                                               <releases>

                                                        <enabled>true</enabled>

                                               </releases>

                                               <snapshots>

                                                        <enabled>true</enabled>

                                               </snapshots>

                                     </repository>

                                     </repositories>

                                     <pluginRepositories>

                                     <pluginRepository>

                                               <id>central</id>

                                               <url>http://192.168.231.1:8080/nexus/content/repositories/central/</url>

                                               <releases>

                                                        <enabled>true</enabled>

                                               </releases>

                                               <snapshots>

                                                        <enabled>true</enabled>

                                               </snapshots>

                                     </pluginRepository>

                                     </pluginRepositories>

                                     </profile>

                            </profiles>

                            <activeProfiles>

                                     <activeProfile>nexus</activeProfile>

                            </activeProfiles>

                   </settings>

 

maven本地仓库服务器的配置

-----------------------------

         1.编辑tomcat/webapps/nexus/WEB-INF/classes/nexus.properties

                   nexus-work=h:/maven-repo/nexus

                   runtime=${bundleBasedir}

                   nexus-app=${runtime}


    在工作中可能存在有些开发机器不能上网,大量的机器获取jar包会导致公司网络很慢,还有时候公司会自己发布自己的项目版本,其他的项目可能会依赖引用到,所以就需要用到Maven的本地仓库。

     下面就这个构建本地Maven仓库进行阐述。

Nexus

一:介绍

  Nexus是Maven的仓库管理器,用来搭建一个本地仓库服务器,这样做的好处便于管理,节约网络资源, 速度快,可以通过SNAPSHOT版本控制,开进行模块间的高效依赖开发。

二:下载

  我们从http://nexus.sonatype.org/downloads/ 来获取最新版本。这里使用的是nexus-2.12.0-01-bundle.zip。

三:解压

  解压到本地后,目录结构。

  在bin里我们可以看到nexus.bat。到时候我们可以通过这个命令来安装卸载,启动停止nexus服务。

四:配置环境变量

  由于是用java开发的。所以需要安装jdk,并配置好java的环境变量,这里就不表了。

  新建环境变量Nexus

  值为解压的目录 F:\nexus-2.12.0-01\ 

  path中加入;%Nexus%\bin

  可以看到

   这样即可。从图中我们可以看到命令:start,stop等。

五:安装服务,启动服务

  我们先安装服务。

 nexus.bat install

  这样就可以安装了,提示安装好后。我们可以启动服务。

nexus.bat start

  提示成功后我们可以输入127.0.0.1:8081/nexus可以访问到。

六:使用

  右上角有个Log in。点击这个可以登录。用户名是admin 密码默认的是 admin123

  登录后,点击左边Views/Repositores下的Repositories

 

  hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。 
  proxy:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。 
  group:仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

  Central:是Maven的中央仓库,我们这里就那这个举例。

  选择仓库的类型

  Repository Policy 是Release 还是Snapshot

  下载远程索引文件

  点击Central这个仓库,然后点击Configration。在Download Remote Indexes改成True保存即可。

  由于索引比较大,所以得过段时间才能下载完毕。

  Routing选项可以看到当前仓库与远程仓库的状态。并且制定更新频率。

  将设置好的仓库放进仓库组里。

  点击Public Repositories->Configuration

  将Central放到左边即可。

  点击Browse Index我们就可以看到很多仓库的资源了。

  配置Maven,打开\apache-maven-3.3.3\conf\settings.xml配置文件。

  在<mirror>节点配置如下即可

  <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.
     | -->
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://127.0.0.0:8081/nexus/content/groups/public/</url>
    </mirror>
    
  </mirrors>

  我们在Elipse里就可以使用这个配置到的本地仓库了。

-----------------------------------------------------------------------------------------------------------------------------------------------------

为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上,这样的话就可以通过这台搭建了nexus私服的电脑访问maven的远程仓库。

1.首先确定我们的环境安装好maven,jdk等必须的环境

2.这些都准备好之后,去下载最新版本的nexus    下载地址:http://www.sonatype.org/nexus/go 

    我本地安装的是 nexus-2.2-01-bundle,最新的版本是nexus-2.4.0-09-bundle

3.打开目录nexus-2.4.0-09-bundle\nexus-2.4.0-09\bin\jsw 这个目录下面你会发现有很多系统版本的nexus环境

   我的电脑是win7 (64)为的系统,所以我选择的是windows-x86-64这个版本,当然可以根据个人的电脑系统选择对应的版本

 

打开一个版本你会看到如下:

 

我一般都是将nexus安装成windows服务,所以点击install-nexus.bat这个,访问http://localhost:8081/nexus/ 启动后如下页面,在右上角有个Log in 的超链接,点击登录

默认的用户名是 admin 密码是 admin123

 登录后你可以在左侧修改登录信息:

 

4.接下来,我们配置一下maven的代理服务器(前提是你的电脑不能连接外网,如果可以上外网,这里也没有意思,只是介绍一下)

   在左侧菜单找到如图:

   

  点击查看右边有很多选项,找到这里

添加你的代理服务器就可以了。

 5:接下来,好像这里都不需要怎么配置的,反正我没有用到很多的功能,可能是技术学得不好,不会用,呵呵....

     对了,这里还有一个可能需要注意一下的,就是3rd party、Snapshots、Releases这三个,分别用来保存第三方jar(典型的oracle数据库的j驱动包),项目组内部的快照、项目组内部的发布版.

     我目前只是用3rd party这个第三方的功能,将maven仓库中没有构件的jar包上传到服务器。如何将第三方的jar上传到nexus上面呢?如下:举例上传oracle的驱动包

    

 

 

 

那么现在nexus已经安装好,怎么使用上传的jar包吧,很简单的,前提是你已经建立了一个maven的项目。含有pom.xml这个文件,在这个文件中添加如下:

先去服务器看看jar的maven构件

然后在pom.xml中添加如下的本地仓库地址:

复制代码
复制代码
 1     <repositories>
 2         <repository>  3 <id>nexus</id>  4 <name>Team Nexus Repository</name>  5 <url>http://localhost:8081/nexus/content/groups/public</url>  6 </repository>  7 </repositories>  8 <pluginRepositories>  9 <pluginRepository> 10 <id>nexus</id> 11 <name>Team Nexus Repository</name> 12 <url>http://localhost:8081/nexus/content/groups/public</url> 13 </pluginRepository> 14 </pluginRepositories> 15 <dependencies> 16 <dependency> 17 <groupId>com.oracle</groupId> 18 <artifactId>ojdbc</artifactId> 19 <version>10.1.0.2.0</version> 20 </dependency> 21 </dependencies>
复制代码
复制代码

 

到此,完成了nexus私服的搭建,项目组开发人员开发时,只要在项目的pom.xml文件中,添加如下pom.xml信息即可获取私服的jar.

如果添加其他的构件时,会先在nexus私服下载好,以后才会下载到本地。以后,如果发现私服已经存在某一jar包,则会直接从私服下载,如果没有再去网络上下载。这就是搭建私服的好处。哈哈........

所以很有必要搭建maven私服。



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值