简体   繁体   English

有条件地排除常春藤依赖

[英]Excluding an Ivy dependency conditionally

I need to build a custom Ant script that builds a project based on CI output. 我需要构建一个自定义的Ant脚本,该脚本可以基于CI输出构建一个项目。 We use Atlassian Bamboo as CI server. 我们使用Atlassian Bamboo作为CI服务器。

Normally our projects have a dependency to our platform module, managed via Ivy/Artifactory. 通常,我们的项目依赖于我们的平台模块,并通过Ivy / Artifactory管理。

Our typical dependencies.xml file contains a dependency to that module, transitively. 我们典型的dependencies.xml文件可传递地包含对该模块的依赖。 And other potential dependencies. 其他潜在的依赖关系。 As an example, our core module depends on lots of Spring packages, but not on Spring Boot. 例如,我们的核心模块依赖于许多Spring软件包,而不依赖于Spring Boot。 If a project needs Spring Boot too, it will define its dependency in its dependencies.xml file along with <depencency org="com.acme" name="core-platform"... 如果一个项目也需要Spring Boot,它将在其dependencies.xml文件中定义其依赖关系,以及<depencency org="com.acme" name="core-platform"...

My goal now is to exclude com.acme#core-platform from resolution, because I am making a different task that uses Bamboo output artifact to take the latest build of the core module and its dependencies without going through Artifactory. 我现在的目标是将com.acme#core-platform排除在分辨率之外,因为我正在执行另一项任务,该任务使用Bamboo输出工件来获取核心模块及其依赖项的最新版本,而无需经过Artifactory。

This is very important because during a build I may like to change the version of a dependent package (eg upgrade Spring 4.3.1 to 4.3.3) and test with the proper Spring. 这一点非常重要,因为在构建过程中,我可能希望更改相关软件包的版本(例如,将Spring 4.3.1升级到4.3.3)并使用正确的Spring进行测试。 If I simply resolve dependencies to com.acme#core-platform#latest.release , which is released on Artifactory, I won't take 4.3.3 of Spring which was committed to Git and available in core-platform 's currently-building dependencies.xml . 如果仅将依赖关系解析为在Artifactory上发布的com.acme#core-platform#latest.release ,则不会采用Spring的4.3.3版本,该版本致力于Git,并且可以在core-platform当前构建的版本中使用dependencies.xml I hope my explanation is easy to understand. 我希望我的解释容易理解。

So let's say I have this dependency list as an example 所以说我有这个依赖列表作为例子

    com.acme#core-platform#${version}
    org.hibernate#hibernate-java8#5.1.0.Final
    org.springframework.boot#spring-boot-starter-web#1.3.1.RELEASE
    commons-collections#commons-collections#3.2.2
    .... others

Full dependency is 完全依赖为

<dependencies>  
    <dependency org="com.acme"              name="core-platform"          rev="${version}"         transitive="true"      conf="runtime->runtime" changing="true"/>
    <dependency org="com.acme"              name="core-platform"          rev="${version}"         transitive="true"      conf="compile->compile" changing="true"/>
    <dependency org="com.acme"              name="core-platform"          rev="${version}"         transitive="true"      conf="provided->provided" changing="true"/>
    <dependency org="com.acme"              name="core-platform"          rev="${version}"         transitive="true"      conf="junit->junit" changing="true"/>
    <dependency org="com.acme"              name="core-platform"          rev="${version}"         transitive="true"      conf="test->test" changing="true"/>



    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
    <dependency org="org.hibernate" name="hibernate-java8" rev="5.1.0.Final" transitive="false" />


    <dependency org="org.springframework.boot"  name="spring-boot-starter-web"          rev="1.3.1.RELEASE"                 transitive="false" />
    <dependency org="org.springframework.boot"  name="spring-boot-starter-tomcat"       rev="1.3.1.RELEASE"                 transitive="false" />
    <dependency org="org.springframework.boot"  name="spring-boot-starter-validation"   rev="1.3.1.RELEASE"                 transitive="false" />


    <dependency org="commons-collections"               name="commons-collections"              rev="3.2.2"             transitive="false" />


    <!-- jackson2 libs -->
    <dependency org="com.fasterxml.jackson.datatype"    name="jackson-datatype-jdk8"            rev="2.8.1"             transitive="false"          conf="runtime->*"/>
    <dependency org="com.fasterxml.jackson.datatype"    name="jackson-datatype-jsr310"          rev="2.8.1"             transitive="false"          conf="runtime->*"/>


    <exclude module="joda-time" />
    <exclude module="jackson-datatype-joda" />
</dependencies>

I simply want to take Hibernates' Java8, commons-collections, etc. 我只是想使用Hibernate的Java8,commons-collections等。

  1. Creating a duplicate dependencies.xml is not an option 创建重复的dependencies.xml 不是一种选择
  2. I was considering manipulating the dependencies.xml via Ant and have it exclude the acme modules by regex. 我正在考虑通过Ant来操作dependencies.xml,并使其通过正则表达式排除acme模块。 Feasible but tricky 可行但棘手
  3. Unfortunately I can't combine Ant task's ivy:retrieve with attributes file and element exclude , because that would have helped a looooooot 不幸的是,我无法将Ant任务的ivy:retrieve与属性file和元素exclude ,因为那样可以帮助looooooot

Any ideas? 有任何想法吗?

It is hard to understand your requirement. 很难理解您的要求。 I suspect that your problem could be solved by creating an additional configuration and use configuration mappings to control the downloads. 我怀疑可以通过创建其他配置并使用配置映射来控制下载来解决您的问题。

Example

This build creates two directories. 此生成将创建两个目录。 The first contains the log4j dependency without transitive dependencies, the second includes the remote module's optional dependencies. 第一个包含没有传递依赖的log4j依赖,第二个包含远程模块的可选依赖。 If you look at the remote POM you'll see they have a different scope. 如果查看远程POM,您会发现它们具有不同的范围。

├── build.xml
├── ivy.xml
├── lib1
│   └── log4j-1.2.17.jar
└── lib2
    ├── activation-1.1.jar
    ├── geronimo-jms_1.1_spec-1.0.jar
    ├── log4j-1.2.17.jar
    └── mail-1.4.3.jar

build.xml build.xml

<project name="demo" default="resolve" xmlns:ivy="antlib:org.apache.ivy.ant">

  <target name="resolve">
    <ivy:resolve/>

    <ivy:retrieve pattern="lib1/[artifact]-[revision](-[classifier]).[ext]" conf="noDependencies"/>
    <ivy:retrieve pattern="lib2/[artifact]-[revision](-[classifier]).[ext]" conf="withDependencies"/>
  </target>

</project>

Notes: 笔记:

  • Each "retrieve" task creates a directory containing the files that make up the configuration. 每个“检索”任务都会创建一个目录,其中包含构成配置的文件。

ivy.xml 常春藤

<ivy-module version="2.0">
  <info organisation="com.myspotontheweb" module="demo"/>

  <configurations>
    <conf name="noDependencies" description="File grouping that has no transitive dependencies"/>
    <conf name="withDependencies" description="File grouping that contains dependencies"/>
  </configurations>

  <dependencies>
    <dependency org="log4j" name="log4j" rev="1.2.17" conf="noDependencies->master; withDependencies->master,optional"/>
  </dependencies>

</ivy-module>

Notes: 笔记:

  • Note how the configuration is declared at the top of the ivy file and the dependency contains two configuration mappings 请注意,如何在ivy文件顶部声明配置,并且依赖项包含两个配置映射

Additional 额外

The following answer explains how ivy interprets Maven modules. 以下答案说明了ivy如何解释Maven模块。 It creates configurations that can be used to decide which files should be downloaded: 它创建可用于决定应下载哪些文件的配置:

Ok, looks like the replace trick is very easy too. 好的,看起来替换技巧也很容易。

  1. Add the following markers <!-- DEPS START --> and <!-- DEPS END --> (or any of choice) between the parts of the dependencies.xml file to ignore <!-- DEPS START -->文件的各部分之间添加以下标记<!-- DEPS START --><!-- DEPS END --> (或选择任意一种)
  2. Hack via Ant 通过蚂蚁入侵

     <copy file="dependencies.xml" tofile="ci/hacked-dependencies.xml" overwrite="true"> <filterchain> <replacestring from="&lt;!-- DEPS START --&gt;" to="&lt;!--" /> <replacestring from="&lt;!-- DEPS END --&gt;" to="--&gt;" /> </filterchain> </copy> 

Example

    <!-- DEPS START --> 
    <dependency org="com.acme" name="core-platfrom" rev="${version}" transitive="true" conf="runtime->runtime"/>
    <!-- DEPS END -->

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

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