简体   繁体   English

Maven私有远程存储库设置

[英]Maven private remote repository setup

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". 当发出' mvn compile '时,它会搜索“〜/ .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). 当我们需要一个工件(例如 - spring-mvc)时,我们需要将它与该工件的所有依赖项(spring-mvc)一起放在远程私有存储库中。 In this case "spring-mvc" is downloaded from our remote private repository. 在这种情况下,从我们的远程私有存储库下载“spring-mvc”。 But the dependencies of "spring-mvc" are not downloaded from our remote private repository. 但是,“spring-mvc”的依赖关系不会从我们的远程私有存储库下载。 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. 以这样一种方式配置专用远程存储库“R”,即我不必手动将所有依赖项放入其中。 When a "mvn compile" issued then first of all; 当首先发出“mvn编译”时; dependencies are searched in "~/.m2" if not found then it goes to the private remote repository 'R'. 在“〜/ .m2”中搜索依赖项,如果没有找到,则它将转到私有远程存储库“R”。 If the dependencies are found in 'R' then "~/.m2" will get it from 'R'. 如果在'R'中找到依赖关系,那么“〜/ .m2”将从'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. 如果在'R'中找不到依赖关系,则从中央存储库下载这些依赖关系,并将依赖关系保存在'R'中以供进一步使用。 After that '~/.m2' will get them from 'R'. 之后'〜/ .m2'将从'R'获得它们。 Here I do not need to put the dependencies in our remote private repository 'R' by hand. 在这里,我不需要手动将依赖项放在我们的远程私有存储库“R”中。

  • '~/.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. '〜/ .m2'将从远程私有存储库'R'获取“spring-mvc”(这是我项目的pom.xml中提到的主要依赖项)的所有依赖项,并且'R'将从相应的中央存储库获取它们。

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. 编辑:我尝试过Nathaniel Waisbrot的回答和nexus-2.7.0-06-bundle。 After hours of endeavors, I am able to set it up while I am using jre-7. 经过几个小时的努力,我可以在使用jre-7时进行设置。 With jre-6, nexus cannot be started. 使用jre-6,无法启动nexus。 But our project is deployed with JDK-6. 但我们的项目是使用JDK-6部署的。 We do not want to the change the current jre version. 我们不想改变当前的jre版本。 With jre-6 apache-archiva is OK. 用jre-6 apache-archiva就行了。 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? 是否有任何archiva用户/专家可以告诉我我是否可以在上一节中实现这些目标?

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. 我已经设置了nexus用于镜像我的maven本地存储库(在~/.m2/repository. )由于这篇文章仍然被很多人访问过,我认为如果我分享如何将nexus配置为存储库,这将对其他人有所帮助经理。 This procedure works perfectly for me in Ubuntu 12.04 . 这个程序在Ubuntu 12.04 非常适合我。 Here it is - 这里是 -

1. Download nexus-2.11.1-01-bundle.tar.gz or latest version of nexus oss. 1.下载nexus-2.11.1-01-bundle.tar.gz或最新版本的nexus oss。

2. Extract the tar file in you home directory- 2.解压缩您主目录中的tar文件 -

$ 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. 现在,您将在主目录中获得两个目录 - nexus-2.11.1-01sonatype-work

3. Copy these two directories to /usr/local/ directory (they can be copied to other place) - 3.将这两个目录复制到/usr/local/目录(可以将它们复制到其他地方) -

$ 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. 与nexus相关的可执行文件/配置文件存储在nexus-2.11.1-01目录中, pom.xml中提到的jar文件存储在sonatype-work目录中。
These jar files are mirror of your ~/.m2/repository . 这些jar文件是~/.m2/repository镜像。 First time you issue a mvn package command then all the jar s are stored here. 第一次发出mvn package命令时,所有jar都存储在这里。 After then when you issue mvn package again then all jars are downloaded from the nexus repository instead of downloading from the central repository. 然后,当您再次发出mvn package时,所有jar都从nexus存储库下载,而不是从中央存储库下载。

4. Go to the /usr/local/ directory - 4.转到/usr/local/目录 -

$ cd /usr/local/  

5. Create a link to nexus-2.11.1-01 - 5.创建一个指向nexus-2.11.1-01的链接 -

$ sudo ln -s nexus-2.7.0-06 nexus

6. Now to run nexus type the following in terminal - 6.现在在终端中运行nexus类型 -

$ bash nexus/bin/nexus console  

Here nexus is attached with your console. 这里nexus与您的控制台相连。 If you close your console then the nexus server will be terminated. 如果关闭控制台,则nexus服务器将被终止。 When you are trying to run nexus for a Ubuntu server machine then you may use screen. 当您尝试为Ubuntu服务器计算机运行nexus时,您可以使用屏幕。

Note: While trying to run nexus by using the command above there may occur 2 problems. 注意:尝试使用上面的命令运行nexus时可能会出现2个问题。 If you do not found any problem then skip next 2 steps (step - 7 and 8) 如果您没有发现任何问题,请跳过下面的2个步骤(步骤7和8)

7. First problem may occur due to insufficient permission. 7.由于许可不足,可能会出现第一个问题。 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 - 8.如果您使用的任何jdk版本低于java 7,则可能会显示以下错误消息 -

wrapper | 包装| Launching a JVM... wrapper | 启动JVM ...包装器| JVM exited while loading the application. 加载应用程序时JVM退出。 jvm 1 | jvm 1 | Exception in thread "main" java.lang.UnsupportedClassVersionError: org/sonatype/nexus/bootstrap/jsw/JswLauncher : Unsupported major.minor version 51.0 线程“main”中的异常java.lang.UnsupportedClassVersionError:org / sonatype / nexus / bootstrap / jsw / JswLauncher:不支持的major.minor版本51.0

In this case use jdk7 to run the command mentioned in step 6. In ubuntu its pretty easy. 在这种情况下,使用jdk7来运行步骤6中提到的命令。在ubuntu中它非常简单。 Assuming you have two jdk - jdk6 and jdk7. 假设你有两个jdk - jdk6和jdk7。 Your project runs on jdk6. 您的项目在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) - 然后只有运行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. Now in browser type the address - http://localhost:8081/nexus/ . 9.现在在浏览器中键入地址 - 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. 如果步骤1到6(如果发生错误,然后是步骤1到8)完美地完成,您可以成功找到登录屏幕。 The default login user name is - admin and password is - admin123 默认登录用户名是 - admin ,密码是 - admin123

10. Stop nexus. 10.停止连接。 Just close the terminal or press Ctrl+C at step 6's terminal. 只需关闭终端或在步骤6的终端按Ctrl + C. In you ~/.m2 directory create an empty file named - settings.xml . 在你~/.m2目录下创建一个名为 - settings.xml的空文件。 Copy the following content into this settings.xml file - 将以下内容复制到此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. And add these following lines in your project's pom.xml file - 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>

Sonatype Nexus does everything you want, needs very little configuration for what you're asking, and has a nice interface. Sonatype Nexus可以满足您的所有需求,只需要很少的配置就可以满足您的要求,而且界面也很棒。 There's some details on the basic setup under the question " Maven Internal Repository, Is it Really This Hard? " 在“ Maven内部存储库,问题真的很难吗? ”下的基本设置有一些细节。

Basically, you can set up Nexus as a caching repository: your Maven client will only visit the internal repository. 基本上,您可以将Nexus设置为缓存存储库:您的Maven客户端将只访问内部存储库。 When the internal repository can't find an artifact, it goes to Central (or any other external repositories you define), fetches it for you, and stores it for later. 当内部存储库找不到工件时,它会转到Central(或您定义的任何其他外部存储库),为您提取它,并将其存储以供日后使用。

This is cool because if you want to transition from a grab-whatever-dependencies-you-want free-for-all to a locked-down environment, Nexus makes it easy. 这很酷,因为如果你想从一个依赖于抓取的依赖关系 - 你想要的免费转换到一个锁定的环境,Nexus让它变得简单。 You set it up to pull from Central, run your build from a clean machine (forcing Nexus to ingest all the current Central dependencies) and then turn off the proxy-repo feature (so any new/changed dependencies will be managed by devops). 您将其设置为从中央提取,从干净的计算机运行您的构建(强制Nexus摄取所有当前的Central依赖项),然后关闭代理 - repo功能(因此任何新的/更改的依赖项将由devops管理)。

What you've described is pretty much the default configuration of most repository managers, including Archiva. 您所描述的几乎是大多数存储库管理器的默认配置,包括Archiva。 Does the following help in getting it set up to use from Maven? 以下是否有助于将其设置为使用Maven?

http://archiva.apache.org/docs/1.3.6/userguide/using-repository.html http://archiva.apache.org/docs/1.3.6/userguide/using-repository.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM