如何创建Maven“项目存储库" [英] How to create a Maven "Project Repo"

查看:60
本文介绍了如何创建Maven“项目存储库"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人要求我查看一个需要 Maven 2.1 的旧项目,以及一些未(也不会)存储在Nexus中的JAR.

I've been asked to look at an old project that requires Maven 2.1 and a couple JARs that are not (and will not) stored in our Nexus.

我正在尝试按照@Nikita Volkov的建议在

I'm trying to follow the advice from @Nikita Volkov in this post about creating a project repo to hold the JARs as artifacts. The idea being I can check this repo into source control. I can then check it out on any machine and have it build without any special configuration.

首先,我创建了一个存储库,并通过运行以下命令将我的第一个Jar添加到其中:

To start with I've created a repo and added my first Jar to it by running:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=lib/myJar.jar -DgroupId=my.group -DartifactId=myArtifact -Dversion=0.0.1

然后创建如下所示的POM:

I then create POM that looks like:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group</groupId>
    <artifactId>MyApp</artifactId>
    <packaging>ear</packaging>
    <version>1.0-SNAPSHOT</version>

    <repositories>
      <repository>
        <id>my-repo</id>
        <url>file://${project.basedir}/repo</url>
      </repository>
    </repositories>

    <dependencies>
      <dependency>
        <groupId>my.group</groupId>
        <artifactId>myArtifact</artifactId>
        <version>0.0.1</version>
      </dependency>
    </dependencies>

    <build>
    </build>
</project>

当我在Eclipse中查看时,如果用错误标记依赖项
Missing artifact my.group:myArtifact:jar:0.0.1.

When I view this in Eclipse if flags the dependency with error
Missing artifact my.group:myArtifact:jar:0.0.1.

当我从命令行运行它时,我得到了错误
Unable to find resource 'my.group:myArtifact:pom:0.0.1' in repository my-repo

When I run it from the command line Iget the error
Unable to find resource 'my.group:myArtifact:pom:0.0.1' in repository my-repo

很明显,我对原始帖子一无所知,但看不到

Clearly I've not understood something in the original post, but I don't see what

因此,任何人都可以提供有关如何创建项目内Maven Repo的有效示例吗?

So, can anybody provide a working example of how to create a in-project Maven Repo?

更新 存储在我的本地存储库中的文件是:

Update The files stored in my local repo are:

  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar
  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar.md5
  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.jar.sha1
  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom
  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom.md5
  • my/group/myArtifact/0.0.1/myArtifact-0.0.1.pom.sha1
  • my/group/myArtifact/maven-metadata-local.xml
  • my/group/myArtifact/maven-metadata-local.xml.md5
  • my/group/myArtifact/maven-metadata-local.xml.sha1

更新 我的.m2/settings文件的内容是:

Update The contents of my .m2/settings file is:

<?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">

  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://192.168.152.78:5000/nexus/content/groups/public/</url> 
    </mirror>
  </mirrors>
</settings>

推荐答案

对原始问题进行多次更新后的有效解决方案

您的settings.xml文件声明了一个全包镜像.这将对pom文件中的本地存储库声明生效.要么删除全部镜像,要么尝试从镜像中排除项目存储库的存储库ID:

Your settings.xml file declares a catch-all mirror. This takes effect over the local repository declaration in your pom file. Either remove the catch-all mirror, or try excluding the repository ID of your project repository from the mirroring:

<mirror>*,!my-repo</mirror>

原始答案

好像您将库安装到默认的本地Maven存储库位置(〜/.m2/repository),但是随后尝试从项目中的某个位置将其提取.

Looks like you install the library to the default local Maven repository location (~/.m2/repository), but then you try to pick it up from a location within your project.

在运行"mvn install:install-file"目标之前,请尝试更改Maven的存储库位置.您可以通过在 settings.xml 中添加"localRepository"设置来实现.

Try changing the repository location for Maven before you run the "mvn install:install-file" goal. You can do this by adding a "localRepository" setting in your settings.xml.

您还可以为项目创建一个新的settings.xml,并告诉Maven在您处理项目时使用它(命令行上的-s参数).诸如Eclipse或IntelliJ之类的IDE也支持使用替代的settings.xml文件.

You could also create a new settings.xml specifically for your project and tell Maven to use that whenever you work on your project (-s parameter on the command line). IDEs like Eclipse or IntelliJ also support using an alternative settings.xml file.

这篇关于如何创建Maven“项目存储库"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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