Mac下使用Nexus搭建Maven私服

  • 安装nexus

    1.下载

      咱们能够在nexus的官网上找到它的相关介绍,下载地址是http://www.sonatype.org/nexus/go,在这里能够找到最新的版本,若是须要之前的版本,在这里也能够找到下载地址。Nexus提供了两种安装方式,一种是内嵌Jetty的bundle,只要你有JRE就能直接运行。第二种方式是WAR,你只须简单的将其发布到web容器中便可使用。为了方便就直接选用bundle版本。我下载的是:nexus-2.2-bundle.tar.gzlinux

    2.安装

    在指定的目录解压下载的文件。解压后会看到两个文件夹,分别是nexus-2.2-01和sonatype-work,前者包含了nexus的运行环境和应用程序,后者包含了你本身的配置和存储构件的地方。nexus-2.2-01/conf/nexus.properties中能够修改端口信息以及工做区的路径。web

    3.启动nexus

    进入nexus-2.2-01/bin/jsw/目录,而后根据OS的版本进入相应的目录,在linux下,运行./nexus start即启动了服务,直接运行./nexus会看到提示信息,运行./nexus console 能够以控制台方式启动nexus,方便咱们观察启动时的相关工做。neuxs默认监听端口是8081,此时在浏览器中运行访问http://127.0.0.1:8081/nexus或者http://localhost:8081/nexus或者http://0.0.0.0:8081/nexus应该能够看到neuxs的界面。浏览器

  • 配置nexus

    新搭建的neuxs环境只是一个空的仓库,须要手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载。登录nexus系统,默认用户名密码为admin/admin123。 点击左边Views/Repositories菜单下面的Repositories,找到右边仓库列表中的三个仓库Apache SnapshotsCodehaus SnapshotsCentral,而后再没有仓库的Configuration下把Download Remote Indexes修改成true。而后在这三个仓库上分别右键,选择Repari Index,这样Nexus就会去下载远程的索引文件。缓存

    新建公司的内部仓库,步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name便可,好比分别填入myrepo和 my repository,另外把Deployment Policy设置为Allow Redeploy,点击save就建立完成了。maven

    修改nexus仓库组url

    Nexus中仓库组的概念是Maven没有的,在Maven看来,无论你是hosted也好,proxy也好,或者group也好,对我都是同样的,我只管根据groupId,artifactId,version等信息向你要构件。为了方便Maven的配置,Nexus可以将多个仓库,hosted或者proxy合并成一个group,这样,Maven只须要依赖于一个group,便能使用全部该group包含的仓库的内容。spa

    neuxs-2.2中默认自带了一个名为“Public Repositories”组,点击该组能够对他保护的仓库进行调整,把刚才创建的公司内部仓库加入其中,这样就不须要再在maven中明确指定内部仓库的地址了。同时建立一个Group ID为public-snapshots、Group Name为Public Snapshots Repositories的组,把Apache Snapshots、Codehaus Snapshots和Snapshots加入其中。插件

    到这里neuxs的安装配置就完成了,下面介绍如何在mavne中使用本身的私服。code

    maven安装好默认是没有配置仓库信息,此时mavne都默认去远程的中心仓库下载所依赖的构件。既然使用了私服,就须要明确告诉maven去哪里下载构件,能够在三个地方进行配置,分别是是mavne的安装目录下的conf下的settings.xml文件,这个全局配置,再一个是在userprofile/.m2目录下的settings.xml,若是不存在该文件,能够复制conf下settings.xml,这个是针对当前用户的,还有一个是在pom.xml中指定,其只对该项目有效,三个文件的优先级是pom.xml大于.m2,.m2大于conf下。xml

    settings.xml的配置以下:

    在profiles段增长一个profile

 <profile>
      <id>nexus</id>
      <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
       </pluginRepositories>
    </profile>


    上述的id,name能够根据本身的喜爱来定义,保证多个repository的id不重复便可,主要是url的配置,能够在nexus的repositories列表中找到对应的url,就是repositories的Repository Path,刚才咱们已经在neuxs中定于了仓库组,这里就取对应组的Repository Path信息便可。

    另外,仓库是两种主要构件的家。第一种构件被用做其它构件的依赖。这是中央仓库中存储的大部分构件类型。另一种构件类型是插件。若是不配置pluginRepositories,那么执行maven动做时,仍是会看到去远程中心库下载须要的插件构件,因此这里必定要加上这个。

    在settings.xml中指使配置了profile还不行,须要激活它,在activeProfiles段,增长以下配置:

<activeProfile>nexus</activeProfile>

    此处activeProfile中的内容就上面定义profile的id。
    pom.xml配置以下:

<repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>local private nexus</name>
            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    其中id,name都无所谓,关键不能把url弄错。

    有了上述配置,当在项目执行maven操做时,若是本地库中没有依赖的构件,maven会去私服下载,若是私服也没有,私服会去远程中心库下载,同时会在私服的本地缓存,这样若是第二我的再要用到这个构件,私服就直接提供下载。

相关文章
相关标签/搜索