4

我正忙着用常春藤弄湿我的脚。我有一个在本地 PC 上运行的现有 nexus 存储库,以及一个现有的 ant 构建脚本。
两者都工作正常。

部分构建脚本有一些文件可以从网络共享中检索我们的 3rdparty jar 文件(log4j、xmlbeans、junit、pdf 等)——这充其量是 klunky。

我想使用 ivy 的依赖机制从 nexus 存储库中检索这些文件并在构建中使用它。每个 3rdparty 库都有一个名称和一组任意文件(jar、dll、license.dat、xml 等)。

由于我们有大量这些 3rdparty 库并且每个库都有多个文件 - 手动上传到 nexus 不是一种选择 - 我需要一些可以用来获取一组文件的东西,给它们一个库名称,一个版本号和将结果上传到nexus。那么我需要能够从常春藤中检索它。

我设法让上传部分工作,但检索过程不起作用。使用我们的 xmlbeans lib 作为起点,我创建了以下 ivy.xml 文件

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

然后一些 ant 脚本将其发布到 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。

当我尝试在我的构建中使用它时,问题就来了。我为我的构建创建了以下 ivy.xml 文件:

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

然后当我运行我的构建时 - 它找不到任何东西:

::::::::::::::::::::::::::::::::::::::::::::::  
::          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  
:::::::::::::::::::::::::::::::::::::::::::::: 

问题似乎与这种模式有关:

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  

(网址被混淆以防止发生事故)。

所以不知何故,我需要以不同的方式发布,或以不同的方式检索。非常感谢您的想法和建议。

4

1 回答 1

14

Nexus 主要是一个 Maven 存储库,这意味着必须适应 Maven 构建工件的方式。

由于您专注于批量加载 Nexus,我建议您查看以下问题的答案:

将工件上传到 Nexus,无需 Maven

如果您想坚持使用常春藤,请继续阅读......

背景

需要一个 Maven POM

您的第一个问题是您的 Maven 模块将需要一个 POM 文件。该文件描述了 maven 模块,可以很容易地从您的ivy.xml文件的内容中生成(参见下面的解决方案)。

其次,Maven 假设正在构建一个主要工件。例如:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <packaging>txt</packaging>
</project>

Maven 客户端会将此信息转换为以下 URL:

http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1.txt

这演示了 Nexus 如何存储任何类型的二进制依赖项。包装参数默认为“jar” 。

maven 如何处理额外的模块工件

虽然 Maven 专注于单个构建工件,但可以通过将它们发布到同一目录中来添加其他补充工件(正如您所做的那样)。

这些不在Maven POM 中列出。相反,Maven 使用特殊的“分类器”属性。下面是一个可能的依赖声明。

<dependency>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <classifier>metadata</classifier>
  <type>n3</type>
</dependency>

Maven 客户端会将其转换为以下 URL:

http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1-metadata.n3

开源项目通常以这种方式发布其源代码。

常春藤解决方案

那么最后如何使用 ivy 将文件发布到 Nexus 中?

首先确定哪个工件是“主要”构建工件,并为您的 POM 文件添加一个附加条目:

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

其他文件也可以列出,但每个文件都必须有一个唯一的分类器属性......在这里,您将面临将 ANT 项目转换为 Maven 的经典问题之一......您发布的每个 jar 文件可能需要有一个单独的 POM。它们并不是真正的“补充”工件......

假装您不需要发布多个模块......使用以下构建目标来发布您的模块:

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

更新

下载工件

要检索所有已发布的工件(不仅仅是主要工件),您需要按如下方式列出它们:

<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 片段相同:

<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>
于 2011-02-25T09:13:21.700 回答