zoukankan      html  css  js  c++  java
  • (转)搭建Maven私服(使用Nexus)

    搭建私服可以做什么? 
    1、如果公司开发组的开发环境全部内网,这时如何连接到在互联网上的Maven中央仓库呢? 
    2、如果公司经常开发一些公共的组件,如何共享给各个开发组,使用拷贝方式吗?如果这样,公共库升级了怎么办?

    当然可以解决的问题可能不止上面两点,下面来介绍在Linux中搭建自己的Maven私服,使用Nexus。

    一、下载和安装

    下载

    网址:http://www.sonatype.org/nexus/go/ 
    这里写图片描述

    下载包:nexus-2.12.0-01-bundle.tar.gz 
    解压包:tar -zxvf nexus-2.12.0-01-bundle.tar.gz 
    默认端口为8081,如需修改请查看配置文件 conf/nexus.properties 
    它本身不建议在root用户下使用,如果我们需要在root用户下启动服务,要先配置 bin/nexus 文件中的 RUN_AS_USER=root

    我下载的是zip包,解压后进入 exus-2.1.2-bundle exus-2.1.2injsw,根据操作系统类型选择文件夹,我选的是windows-x86-32文件夹,进入后可看到如下所示bat文件。

     

    安装

     将下载Bundle文件解压,会得到如下两个子目录:

    nexus-webapp-1.7.2/:该目录包含了Nexus运行所需要的文件,如启动脚本,依赖jar包等。

    sonatype-work/:该目录包含Nexus生成的配置文件、日志文件、仓库文件等。

     其中第一个目录是运行Nexus所必需的。

    第二个目录不是必须的,Nexus会在运行的时候动态创建改目录,不过它的内容对于各个Nexus实例是不一样的,因为不同用户在不同机器上使用的Nexus会有不同的配置和仓库内容。

    当用户需要备份Nexus的时候,默认备份sonatype-work/目录,因为该目录包含了用户特定的内容,而nexus-webapp-1.7.2目录下的内容是可以从安装包直接获得的。

    ps:最简单的方式就是将两个文件全部拷贝,然后直接安装使用。

    二、私服的启动和配置

    启动

    [root@localhost nexus-maven]# cd nexus-2.12.0-01/bin/
    [root@localhost bin]# ./nexus start
    ****************************************
    WARNING - NOT RECOMMENDED TO RUN AS ROOT
    ****************************************
    Starting Nexus OSS...
    Started Nexus OSS.
    [root@localhost bin]# ./nexus status
    ****************************************
    WARNING - NOT RECOMMENDED TO RUN AS ROOT
    ****************************************
    Nexus OSS is running (34504).
    [root@localhost bin]#

    启动后访问首页: http://192.168.19.130:8081/nexus/index.html

    登录默认账号/密码 admin/admin123 
    这里写图片描述

    打开 Repositories 将列表中所有Type为proxy 的项目的 Configuration 中的 Download Remote Indexes 设置为True 
    这里写图片描述

    将Releases仓库的Deployment Policy设置为*Allow ReDeploy 
    这里写图片描述

    设置 deployment 账户密码 
    这里写图片描述 
    这里写图片描述

    然后在Central 仓库上右键然后点击 Repair Index 下载中心仓库的索引文件,若干时间后,可以点击下边的 Browse Index 即可看见下载的索引文件。


    当然我们也避免不了会使用到一些第三方的 jar ,而这些jar包也不存在于互联网上的maven中央仓库中,这时我们可以手工添加jar 到我们的私服中。 
    添加第三方 jar 如下: 
    这里写图片描述

    如果需要删除,如下: 
    这里写图片描述

    三、本地项目配置引用私服

    在项目的 pom.xml 中配置私库地址,pom.xml 的下面添加:

     <!-- 私有仓库 -->
        <repositories>  
            <repository>  
                <id>public</id>  <!--这个ID需要与你的组group ID一致--> 
                <name>Public Repository</name>
                <url>http://192.168.19.130:8081/nexus/content/groups/public</url>   
            </repository>  
        </repositories> 
    
        <!-- 打包发布 -->
        <distributionManagement>
            <repository>
                <id>releases</id><!--这个ID需要与你的release仓库的Repository ID一致-->
                <url>http://192.168.19.130:8081/nexus/content/repositories/releases</url>
            </repository>
    
            <snapshotRepository>
                <id>snapshots</id><!--这个ID需要与你的snapshots仓库的Repository ID一致-->
                <url>http://192.168.19.130:8081/nexus/content/repositories/snapshots</url>
            </snapshotRepository>
        </distributionManagement>

    ps:仓库也可以通过节点的方式,配置在setting.xml文件中。

    在settings.xml 中配置 server 账户信息:

    <servers>
         <server>
            <id>releases</id>
            <username>deployment</username>
            <password>lixuwu123</password><!--这个密码就是你设置的密码-->
        </server>
        <server>
            <id>snapshots</id>
            <username>deployment</username>
            <password>lixuwu123</password><!--这个密码就是你设置的密码-->
        </server>
      </servers>

    需要说明一点: 
    当pom.xml中同时配置了releases仓库和snapshots仓库时。 
    pom.xml文件开头的版本配置1.0.0-SNAPSHOT为build到snapshots库, 
    pom.xml文件开头的版本配置1.0.0 (不带-SNAPSHOT) 的会build到releases库, 
    如果只配置了releases库而版本号写的是带-SNAPSHOT的,build到最后一步会报400错误,因为它找不到对应的库。

    四、测试

    1、新建一个简单的maven项目,随便写个类。 
    在pom.xml 文件按上面 三、本地项目配置引用私服 方法添加 私有仓库和打包发布配置 
    然后使用命令 mvn deploy 发布成功后,此时我们在我们的私服中就可以看到发布后的结果,如下: 
    这里写图片描述

    2、再新建一个项目,或者使用已有的maven项目(最好使用别人的环境不同的电脑)。 
    在pom.xml 中和第1步一样先配置私库地址,然后添加第1步发布后的 dependency 后,就可以看到jar 被正常加载到工程中了。 
    这里写图片描述

    自己测试实例:

    setting.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <settings xmlns="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.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
      <localRepository>D:/.m2/repository</localRepository>
    
    
      <pluginGroups></pluginGroups>
    
    
      <proxies></proxies>
    
      <!-- 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>
                <!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
                <id>nexus-releases</id>
                <username>admin</username>
                <password>admin123</password>
            </server>
            <server>
                <!-- 这个ID要和项目pom.xml中distributionManagement下的ID一致 -->
                <id>nexus-snapshots</id>
                <username>admin</username>
                <password>admin123</password>
            </server>
      </servers>
    
      
      <mirrors>
         <mirror>
              <id>nexus</id>
              <mirrorOf>*</mirrorOf>
              <url>http://localhost:8081/nexus/content/groups/public/</url>
        </mirror>
      </mirrors>
    
     
      <profiles>
            <profile>
                <id>nexus</id>
                 <!-- 配置仓库 -->
                <repositories>
                    <repository>
                        <id>central</id>
                        <url>http://central</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                </repositories>
    
                <!-- 配置插件仓库 -->
                <pluginRepositories>
                    <pluginRepository>
                        <id>central</id>
                        <url>http://central</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </pluginRepository>
                </pluginRepositories>
            </profile>
        </profiles>
    
        <!-- 激活节点nexus -->
        <activeProfiles>
            <activeProfile>nexus</activeProfile>
        </activeProfiles>
    </settings>

    pom.xml文件:

    使用eclipse新建一个quickstart的maven项目

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.li</groupId>
        <artifactId>test-archetype</artifactId>
        <packaging>jar</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>test-archetype Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
    
    <distributionManagement>
            <repository>
                <id>nexus-releases</id>
                <name>Nexus Release Repository</name>
                <url>http://localhost:8081/nexus/content/repositories/releases/</url>
            </repository>
            <snapshotRepository>
                <id>nexus-snapshots</id>
                <name>Nexus Snapshot Repository</name>
                <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
            </snapshotRepository>
        </distributionManagement>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    执行命令

    clean deploy

  • 相关阅读:
    KVM(Keyboard、Video、Mouse)
    javascript valueof
    javascript的typeof的返回值
    javascript的===和==
    WGestures全局鼠标手势工具
    向win+x快捷目录添加功能
    好用的影子系统软件
    备份任务栏
    修改多个调用提示最小值_改变多选右键菜单
    执行Windows Update更新
  • 原文地址:https://www.cnblogs.com/lixuwu/p/8111778.html
Copyright © 2011-2022 走看看