问题描述
我正忙着用常春藤弄湿我的脚.我有一个在本地 PC 上运行的现有 nexus 存储库,以及一个现有的 ant 构建脚本.
两者都工作正常.
I'm busily getting my feet wet with ivy. I have an existing nexus repository running on my local PC, and an existing ant build script.
Both work fine.
部分构建脚本有一些文件可以从网络共享中检索我们的 3rdparty jar 文件(log4j、xmlbeans、junit、pdf 等)——这充其量是笨拙的.
Part of the build scripts have some files to retrieve our 3rdparty jar files (log4j, xmlbeans, junit, pdf, etc..) from a network share - which is klunky at best.
我想使用 ivy 的依赖机制从 nexus 存储库中检索这些文件并在构建中使用它.每个 3rdparty 库都有一个名称和一组任意文件(jar、dll、license.dat、xml 等).
I want to use ivy's dependency mechanisms to retrieve these files from a nexus repository and use that in the build. Each 3rdparty lib has a name and an arbitrary set of files (jar, dll, license.dat, xml, etc).
因为我们有大量的这些 3rdparty 库,并且每个库都有多个文件 - 手动上传到 nexus 不是一种选择 - 我需要一些可以用来获取一组文件的东西,给它们一个库名称,一个版本号并将结果上传到nexus.然后我需要能够从 ivy 中检索它.
Since we have a large number of these 3rdparty libs and each lib has multiple files - manual uploading to nexus is not an option -- i need something i can use to take a set of files, give them a lib name, a version number and upload the result to nexus. then I need to be able to retrieve this from ivy.
我设法使上传部分工作,但检索过程不起作用.使用我们的 xmlbeans 库作为起点,我创建了以下 ivy.xml 文件
I managed to get the upload part to work, but the retreival process does not work. Using our xmlbeans lib as a starting point I created the following ivy.xml file
<ivy-module version="1.0">
<info organisation="thirdparty_tools" module="xmlbeans" status="integration">
<publications>
<artifact name="jsr173_api" type="jar" ext="jar"/>
<artifact name="saxon-dom" type="jar" ext="jar"/>
<artifact name="saxon-xpath" type="jar" ext="jar"/>
<artifact name="saxon" type="jar" ext="jar"/>
<artifact name="xbean" type="jar" ext="jar"/>
<artifact name="xbean_xpath" type="jar" ext="jar"/>
<artifact name="xmlpublic" type="jar" ext="jar"/>
</publications>
</ivy-module>
然后一些蚂蚁脚本将其发布到nexus:
and then some ant script to publish it to nexus:
<ivy:resolve/>
<ivy:publish <ivy:publish resolver="thirdparty" forcedeliver="true" update="true" revision="${version}" overwrite="true">
<artifacts pattern="[artifact].[ext]"/>
<ivy:publish/>
这一切正常.它将所有 jar 文件发布到预期目录中的 nexus.
And this all works fine. It publishes all the jar files to nexus in the expected directory.
当我尝试在我的构建中使用它时,问题就来了.我为我的构建创建了以下 ivy.xml 文件:
The trouble comes when I try to use it in my build.I created the following ivy.xml file for my build:
<ivy-module version="1.0">
<info organisation="myCompany" module="GLB_Data"/>
<dependencies>
<dependency org="thirdparty_tools" name="xmlbeans" rev="2.2.0"/>
</dependencies>
</ivy-module>
然后当我运行我的构建时 - 它找不到任何东西:
Then when I run my build - it fails to find anything:
::::::::::::::::::::::::::::::::::::::::::::::
:: UNRESOLVED DEPENDENCIES ::
::::::::::::::::::::::::::::::::::::::::::::::
:: thirdparty_tools#jsr173_api;2.2.0: not found
:: thirdparty_tools#saxon-dom;2.2.0: not found
:: thirdparty_tools#saxon-xpath;2.2.0: not found
:: thirdparty_tools#saxon;2.2.0: not found
:: thirdparty_tools#xbean;2.2.0: not found
:: thirdparty_tools#xbean_xpath;2.2.0: not found
:: thirdparty_tools#xmlpublic;2.2.0: not found
::::::::::::::::::::::::::::::::::::::::::::::
问题似乎出在这种模式上:
The problem seems to be with this pattern:
WARN: ==== public: tried
WARN: http //localhost:8081/nexus/content/groups/public/thirdparty_tools/jsr173_api/2.2.0/jsr173_api-2.2.0.pom
WARN: -- artifact thirdparty_tools#jsr173_api;2.2.0!jsr173_api.jar:
WARN: http //localhost:8081/nexus/content/groups/public/thirdparty_tools/jsr173_api/2.2.0/jsr173_api-2.2.0.jar
ivy seems to be looking for the jsr173_api artifact under its own name, rather than under the xmlbeans folder where it was published to:
[ivy:publish] published jsr173_api to http //localhost:8081/nexus/content/repositories/thirdparty/thirdparty_tools/xmlbeans/2.2.0/jsr173_api-2.2.0.jar
(混淆网址以防止意外).
(urls obfuscated to prevent accidents).
所以不知何故我需要以不同的方式发布,或以不同的方式检索.非常感谢您的想法和建议.
so somehow I need to publish differently, or retrieve differently. Ideas and suggestions are much appreciated.
推荐答案
Nexus 主要是一个 Maven 存储库,这意味着必须适应 Maven 构建工件的方式.
Nexus is primarily a Maven repository, this means one must adapt to the way Maven structures artifacts.
由于您专注于批量加载 Nexus,我建议您查看以下问题的答案:
Since you're focused on bulk loading Nexus I suggest looking at the answer to the following question:
如果你想坚持使用常春藤读.....
If you wish to stick with ivy read on.....
您的第一个问题是您的 Maven 模块需要一个 POM 文件.此文件描述了 Maven 模块,可以从 ivy.xml 文件的内容轻松生成(请参阅下面的解决方案).
Your first issue is that your Maven module(s) will need a POM file. This file describes the maven module and can be easily generated from the contents of your ivy.xml file (See solution below).
其次,Maven 假定正在构建一个 主要工件.例如:
Secondly, Maven assumes that there is one primary artifact being built. For example:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb</groupId>
<artifactId>donaldduck</artifactId>
<version>1.0.1</version>
<packaging>txt</packaging>
</project>
Maven 客户端会将此信息转换为以下 URL:
A Maven client would translate this information into the following URL:
http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1.txt
这演示了 Nexus 如何存储任何类型的二进制依赖项.packaging 参数默认为jar".
This demonstrates how Nexus can store any type of binary dependency. The packaging parameter defaults to "jar".
虽然 Maven 专注于单个构建工件,但可以通过将它们发布到同一目录中来添加额外的补充工件(正如您所做的那样).
While Maven focuses on a single build artifact, it is possible to add additional supplementary artifacts by posting them into the same directory (as you've done).
这些未列在 Maven POM 中.相反,Maven 使用特殊的分类器"属性.以下是可能的依赖声明.
These are not listed in the Maven POM. Instead a Maven uses a special "classifier" attribute. The following is a possible dependency declaration.
<dependency>
<groupId>com.myspotontheweb</groupId>
<artifactId>donaldduck</artifactId>
<version>1.0.1</version>
<classifier>metadata</classifier>
<type>n3</type>
</dependency>
Maven 客户端会将其转换为以下 URL:
A Maven client would translate this into the following URL:
http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1-metadata.n3
开源项目通常以这种方式发布其源代码.
Open source projects typically release their source code in this manner.
那么最后如何使用 ivy 将文件发布到 Nexus 中?
So finally how does one publish files into Nexus using ivy?
首先决定哪个工件是主要"构建工件并为您的 POM 文件添加一个附加条目:
First of all decide which artifact is the "main" build artifact and add an additional entry for your POM file:
<ivy-module version='2.0' xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="com.myspotonontheweb" module="donaldduck" revision="1.0.1"/>
<publications>
<artifact name="donaldduck" type="txt"/>
<artifact name="donaldduck" type="pom"/>
<artifact name="donaldduck" type="n3" e:classifier="metadata"/>
<artifact name="donaldduck" type="zip" e:classifier="disto"/>
</publications>
</ivy-module>
也可以列出其他文件,但每个文件都必须具有唯一的classifier 属性..... 在这里,您将面临将 ANT 项目转换为 Maven 的经典问题之一.... 您发布的每个 jar 文件,可能都需要有一个单独的 POM.它们并不是真正的补充"人工制品.....
The other files can also be listed but each must have a unique classifier attribute..... Here you will be faced with one of the classic problems translating an ANT project into Maven.... Each jar file you publish, will probably need to have a separate POM. They not really "supplementary" artifacts.....
假装您不需要发布多个模块....使用以下构建目标来发布您的模块:
Pretending that you don't need to publish multiple modules.... Use the following build targets to publish your module:
<target name="prepare" description="Generate POM">
<!-- Optional: Intermediate file containing resolved version numbers -->
<ivy:deliver deliverpattern="${build.dir}/ivy.xml" pubrevision="${publish.revision}" status="release"/>
<!-- Generate the Maven POM -->
<ivy:makepom ivyfile="${build.dir}/ivy.xml" pomfile="${build.dir}/donaldduck.pom"/>
</target>
<target name="publish" depends="init,prepare" description="Upload to Nexus">
<ivy:publish resolver="nexus-deploy" pubrevision="${publish.revision}" overwrite="true" publishivy="false" >
<artifacts pattern="${build.dir}/[artifact](-[classifier]).[ext]"/>
</ivy:publish>
</target>
Nexus 凭据
为了完整起见,这里是包含 Nexus 存储库位置和凭据的 ivysettings.xml 文件:
<ivysettings>
<settings defaultResolver="nexus-central"/>
<credentials host="somehost" realm="Sonatype Nexus Repository Manager" username="????" passwd="????"/>
<resolvers>
<ibiblio name="nexus-central" root="http://somehost/nexus/content/repositories/central/" m2compatible="true"/>
<ibiblio name="nexus-deploy" root="http://somehost/nexus/content/repositories/repo" m2compatible="true"/>
</resolvers>
</ivysettings>
更新
下载工件
要检索所有已发布的工件(不仅仅是主要工件),您需要按如下方式列出它们:
Update
Downloading artifacts
To retrieve all the published artifacts (not just the main one), you need to list them as follows:
<dependency org="com.myspotontheweb" name="donaldduck" rev="1.0.1">
<artifact name="donaldduck" type="txt"/>
<artifact name="donaldduck" type="n3" e:classifier="metadata"/>
<artifact name="donaldduck" type="zip" e:classifier="distro"/>
</dependency>
功能上与下面的Maven片段相同:
Functionally the same as the following Maven fragment:
<dependency>
<groupId>com.myspotontheweb</groupId>
<artifactId>donaldduck</artifactId>
<version>1.0.1</version>
<type>txt</type>
</dependency>
<dependency>
<groupId>com.myspotontheweb</groupId>
<artifactId>donaldduck</artifactId>
<version>1.0.1</version>
<classifier>metadata</classifier>
<type>n3</type>
</dependency>
<dependency>
<groupId>com.myspotontheweb</groupId>
<artifactId>donaldduck</artifactId>
<version>1.0.1</version>
<classifier>distro</classifier>
<type>zip</type>
</dependency>
这篇关于如何使用ivy和nexus发布3rdparty工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!