Maven私有远程存储库设置 [英] Maven private remote repository setup

查看:82
本文介绍了Maven私有远程存储库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的团队建立一个私有的远程存储库.现在,到目前为止,我已经尝试过-

  • 当发出" mvn编译"时,它将在〜/.m2"中搜索依赖项.如果没有找到,则转到远程专用存储库,而当此处仍然不存在依赖项时,它将转到中央存储库.

  • 在编译之前,我必须将所有依赖项以及校验和和元数据放入我们的私有(和远程)存储库中.

  • 当我们需要一个工件(例如spring-mvc)时,我们需要将其与该工件的所有依赖项(spring-mvc)一起放入远程私有存储库中.在这种情况下,"spring-mvc"是从我们的远程专用存储库下载的.但是,"spring-mvc"的依赖项不是从我们的远程专用存储库下载的.它们是从中央存储库下载的.

现在我想做的是-

  • 以这样一种方式配置私有远程存储库"R",而我不必手工放置所有依赖项.当发出"mvn编译"时,首先;如果找不到依赖项,则在〜/.m2"中搜索依赖项,然后将其转到专用远程存储库"R".如果在"R"中找到依赖项,则〜/.m2"将从"R"中获取它.如果在"R"中找不到依赖项,那么将从中央存储库下载这些依赖项,并将这些依赖项保留在"R"中以备将来使用.之后,〜/.m2"将从"R"获得它们.在这里,我不需要手动将依赖项放在我们的远程私有存储库"R"中.

  • '〜/.m2'将从远程私有存储库'R'中获取"spring-mvc"的所有依赖项(这是我项目的pom.xml中提到的主要依赖项)它们来自各自的中央存储库.

有人可以提出一些方法/或教程作为参考,以实现上述两个目标吗?预先感谢.

我尝试使用 Nathaniel Waisbrot 的答案和联系- 2.7.0-06捆绑包.经过数小时的努力,我可以在使用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都存储在这里.之后,当您再次发布mvn package时,所有jar都会从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时,可以使用屏幕.

注意:在尝试使用上述命令运行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,则可能会显示以下错误消息-

包装器|启动JVM ...包装器| JVM加载时退出 应用. jvm 1 |线程主"中的异常 java.lang.UnsupportedClassVersionError: org/sonatype/nexus/bootstrap/jsw/JswLauncher:不支持major.minor 版本51.0

在这种情况下,请使用jdk7运行步骤6中提到的命令.在ubuntu中,它非常简单.假设您有两个jdk-jdk6和jdk7.您的项目在jdk6上运行.然后,仅对于运行nexus,您可以在终端上执行此操作(假设/usr/lib/jvm/jdk1.7.0_45目录中的jdk7)-

$ 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步)完成得很好,您可能会成功找到登录屏幕.默认的登录用户名是- admin ,密码是- admin123

10..停止联系.只需关闭终端或在步骤6的终端上按 Ctrl + C .在您的~/.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>

I'm trying to set up a private remote repository for our team. Now the repository serves the following with so far I have tried -

  • When a 'mvn compile' is issued, then it searches dependencies in "~/.m2". If it is not found there then it goes to the remote private repository and when the dependencies still absent here then it goes to the central repository.

  • Before compilation, I have to put all dependencies in our private (and remote) repository along with checksum and metadata.

  • When we need an artifact(eg - spring-mvc), we need to put it in the remote private repository along with all dependencies of that artifact (spring-mvc). In this case "spring-mvc" is downloaded from our remote private repository. But the dependencies of "spring-mvc" are not downloaded from our remote private repository. They are downloaded from the central repository.

Now what I am trying to do are -

  • Configuring a private remote repository 'R' in such a way that I do not have to put all the dependencies to it by hand. When a "mvn compile" issued then first of all; dependencies are searched in "~/.m2" if not found then it goes to the private remote repository 'R'. If the dependencies are found in 'R' then "~/.m2" will get it from 'R'. If the dependencies are not found in 'R' then these dependencies are downloaded from the central repositories and keep the dependencies in 'R' for further uses. After that '~/.m2' will get them from 'R'. Here I do not need to put the dependencies in our remote private repository 'R' by hand.

  • '~/.m2' will get all dependencies of "spring-mvc" (which is the main dependency mentioned in my project's pom.xml) from the remote private repository 'R' and 'R' will get them from the respective central repository.

Can anyone suggest some way/or tutorial for reference to meet these two above targets? Thanks in advance.

EDIT : I have tried with Nathaniel Waisbrot's answer and nexus-2.7.0-06-bundle. After hours of endeavors, I am able to set it up while I am using jre-7. With jre-6, nexus cannot be started. But our project is deployed with JDK-6. We do not want to the change the current jre version. With jre-6 apache-archiva is OK. But I am not sure whether I can achieve all the goals in previous section. Is there any archiva user/expert who can tell me whether I can meet these goals in the previous section?

解决方案

I have setup nexus for mirroring my maven local repository (at ~/.m2/repository.) Since this post is still visited by a lot of people, I think it would be helpful for others if I share how I configured nexus as a repository manager. This procedure works perfectly for me in Ubuntu 12.04. Here it is -

1. Download nexus-2.11.1-01-bundle.tar.gz or latest version of nexus oss.

2. Extract the tar file in you home directory-

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

Now you will get two directories - nexus-2.11.1-01 and sonatype-work in your home directory.

3. Copy these two directories to /usr/local/ directory (they can be copied to other place) -

$ cp -r nexus-2.11.1-01 /usr/local/
$ cp -r sonatype-work /usr/local/

The executable/configuration files related to nexus are stored in nexus-2.11.1-01 directory and the jar file mentioned in pom.xml are stored in sonatype-work directory.
These jar files are mirror of your ~/.m2/repository. First time you issue a mvn package command then all the jars are stored here. After then when you issue mvn package again then all jars are downloaded from the nexus repository instead of downloading from the central repository.

4. Go to the /usr/local/ directory -

$ cd /usr/local/  

5. Create a link to nexus-2.11.1-01 -

$ sudo ln -s nexus-2.7.0-06 nexus

6. Now to run nexus type the following in terminal -

$ bash nexus/bin/nexus console  

Here nexus is attached with your console. If you close your console then the nexus server will be terminated. When you are trying to run nexus for a Ubuntu server machine then you may use screen.

Note: While trying to run nexus by using the command above there may occur 2 problems. If you do not found any problem then skip next 2 steps (step - 7 and 8)

7. First problem may occur due to insufficient permission. Read the error message and take necessary steps. But as a quick solution you may do this -

$ sudo chmod -R 777 nexus-2.11.1-01/
$ sudo chmod -R 777 sonatype-work/

8. If you are using any jdk version lower than java 7 than the following error message may be shown -

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

In this case use jdk7 to run the command mentioned in step 6. In ubuntu its pretty easy. Assuming you have two jdk - jdk6 and jdk7. Your project runs on jdk6. Then only for running nexus you may do this from your terminal (assuming your jdk7 in /usr/lib/jvm/jdk1.7.0_45 directory) -

$ export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_45
$ export PATH=$JAVA_HOME/bin:$PATH  

9. Now in browser type the address - http://localhost:8081/nexus/. If step 1 to 6 (if errors occurred then step 1 to 8) are done perfectly you may successfully find the login screen. The default login user name is - admin and password is - admin123

10. Stop nexus. Just close the terminal or press Ctrl+C at step 6's terminal. In you ~/.m2 directory create an empty file named - settings.xml. Copy the following content into this settings.xml file -

<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. And add these following lines in your project's pom.xml file -

<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私有远程存储库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆