Maven 私有(private)远程仓库设置

标签 maven repository settings nexus archiva

我正在尝试为我们的团队建立一个私有(private)远程存储库。现在存储库提供以下服务,到目前为止我已经尝试过 -

  • 当一个' mvn 编译 ' 发出,然后它在“~/.m2”中搜索依赖项。如果在那里找不到它,那么它会转到远程私有(private)存储库,当这里仍然不存在依赖项时,它会转到中央存储库。
  • 在编译之前,我必须将所有依赖项连同校验和和元数据一起放在我们的私有(private)(和远程)存储库中。
  • 当我们需要一个 Artifact (例如 - spring-mvc)时,我们需要将它与该 Artifact 的所有依赖项(spring-mvc)一起放在远程私有(private)存储库中。在这种情况下,“spring-mvc”是从我们的远程私有(private)存储库下载的。但是“spring-mvc”的依赖不是从我们的远程私有(private)仓库下载的。它们是从中央存储库下载的。

  • 现在我想做的是——
  • 配置一个私有(private)远程存储库“R”,这样我就不必手动将所有依赖项放入它。当发出“mvn compile”时,首先;如果找不到依赖项,则在“~/.m2”中搜索依赖项,然后转到私有(private)远程存储库“R”。如果在“R”中找到依赖项,则“~/.m2”将从“R”中获取它。如果在“R”中未找到依赖项,则从中央存储库下载这些依赖项并将依赖项保留在“R”中以供进一步使用。之后,“~/.m2”将从“R”中获取它们。在这里,我不需要手动将依赖项放在我们的远程私有(private)存储库“R”中。
  • '~/.m2' 将从远程私有(private)存储库 'R' 和 'R' 中获取“spring-mvc” 的所有依赖项(这是我项目的 pom.xml 中提到的主要依赖项),而 'R' 将从各自的中央存储库获取它们.

  • 任何人都可以提出一些引用方法/或教程来满足上述两个目标吗?提前致谢。

    编辑:我试过 Nathaniel Waisbrot的答案和 nexus-2.7.0-06-bundle。经过数小时的努力,我能够在使用 jre-7 时进行设置。使用 jre-6,无法启动 nexus。但是我们的项目是使用 JDK-6 部署的。我们不想更改当前 jre版本。使用 jre-6 apache-archiva没关系。但我不确定我是否可以实现上一节中的所有目标。有没有archiva用户/专家谁能告诉我我是否可以达到上一节中的这些目标?

    最佳答案

    我已设置 nexus 用于镜像我的 maven 本地存储库(位于 ~/.m2/repository. )由于这篇文章仍然有很多人访问,我认为如果我分享我的配置方式对其他人会有所帮助 nexus作为存储库管理员。这个程序完美运行对我来说 Ubuntu 12.04 .这里是 -

    1. 下载 nexus-2.11.1-01-bundle.tar.gz或最新版本的nexus oss。

    2. 提取主目录中的 tar 文件-

    $ tar -xvf nexus-2.11.1-01-bundle.tar.gz
    

    现在你会得到两个目录 - nexus-2.11.1-01sonatype-work在您的主目录中。

    3. 将这两个目录复制到 /usr/local/ 目录(可以复制到其他地方) -
    $ cp -r nexus-2.11.1-01 /usr/local/
    $ cp -r sonatype-work /usr/local/
    

    与nexus相关的可执行文件/配置文件存储在nexus-2.11.1-01 pom.xml中提到的目录和jar文件存储在 sonatype-work目录。
    这些 jar 文件是您的 ~/.m2/repository 的镜像。 .第一次发出 mvn package然后命令所有 jar s 存储在这里。之后当你发出 mvn package然后所有 jars 都是从 nexus 存储库下载的,而不是从中央存储库下载的。

    4. 转至/usr/local/目录 -
    $ cd /usr/local/  
    

    5. 创建指向 nexus-2.11.1-01 的链接-
    $ sudo ln -s nexus-2.7.0-06 nexus
    

    6. 现在运行 nexus 在终端中键入以下内容 -
    $ bash nexus/bin/nexus console  
    

    这里 nexus 与您的控制台相连。如果您关闭控制台,则连接服务器将被终止。当您尝试为 Ubuntu 服务器机器运行 nexus 时,您可以使用 screen.

    注:在尝试使用上述命令运行 nexus 时,可能会出现 2 个问题。如果您没有发现任何问题,请跳过接下来的 2 个步骤(步骤 - 7 和 8)

    7. 由于权限不足,可能会出现第一个问题。阅读错误消息并采取必要的步骤。但作为 快速解决方案你可以这样做 -
    $ sudo chmod -R 777 nexus-2.11.1-01/
    $ sudo chmod -R 777 sonatype-work/
    

    8. 如果您使用任何 jdk低于 java 7 的版本可能会显示以下错误消息 -

    wrapper | Launching a JVM... wrapper | JVM exited while loading the application. jvm 1 | Exception in thread "main" java.lang.UnsupportedClassVersionError: org/sonatype/nexus/bootstrap/jsw/JswLauncher : Unsupported major.minor version 51.0



    在这种情况下,使用 jdk7 运行步骤 6 中提到的命令。在 ubuntu 中它非常简单。假设您有两个 jdk - jdk6 和 jdk7。您的项目在 jdk6 上运行。然后只有在运行 nexus 时,您才可以从终端执行此操作(假设您的 jdk7 在 /usr/lib/jvm/jdk1.7.0_45 目录中) -
    $ export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_45
    $ export PATH=$JAVA_HOME/bin:$PATH  
    

    9. 现在在浏览器中输入地址 - http://localhost:8081/nexus/ .如果步骤 1 到 6(如果发生错误,则步骤 1 到 8)完美完成,您可能会成功找到登录屏幕。默认登录用户名是 - 管理员 密码是 - 管理员123

    10. 停止联系。只需关闭终端或按 Ctrl+C 在第 6 步的终端。在你~/.m2目录创建一个名为 - settings.xml 的空文件.将以下内容复制到此settings.xml文件 -
    <settings>
        <mirrors>
            <mirror>
            <!--This sends everything else to /public -->
                <id>nexus</id>
                <mirrorOf>*</mirrorOf>
                <url>http://localhost:8081/nexus/content/groups/public</url>
            </mirror>
        </mirrors>
    
        <profiles>
            <profile>
                <id>nexus</id>
                <!--Enable snapshots for the built in central repo to direct -->
                <!--all requests to nexus via the mirror -->
                <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>
    
        <activeProfiles>
        <!--make the profile active all the time -->
            <activeProfile>nexus</activeProfile>
        </activeProfiles>
    </settings>
    

    11. 并将以下几行添加到项目的 pom.xml 中文件 -
    <distributionManagement>
        <snapshotRepository>
            <id>my-snapshots</id>
            <name>My internal repository</name>
            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    
        <repository>
            <id>my-releases</id>
            <name>My internal repository</name>
            <url>http://localhost:8081/nexus/content/repositories/releases</url>
        </repository>
    </distributionManagement>
    

    关于Maven 私有(private)远程仓库设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882622/

    相关文章:

    java - Git - 将新项目提交到错误的存储库,无法删除它

    git - Visual Studio 代码和 git : Permission denied to X

    c# - 我可以通过配置文件设置 SoapHttpClientProtocol Timeout 属性吗?

    java - 无法运行 Sonar 扫描

    mysql - 带有多个定界符的 sql-maven-plugin

    java - 测试|从可运行 jar 内的 main 方法调用 testng.xml 文件

    github - IntelliJ,无法在 GitHub 上共享项目(权限被拒绝)

    每个用户的 Gitosis 存储库目录?

    maven:在pom中指定settings.xml位置

    使用相对路径的 Java Export JAR