ivy:retrieve select which ivy.xml file to use

和自甴很熟 提交于 2020-01-15 05:23:08

问题


Is there a way to select which ivy.xml file to use when I invoke ivy:retrieve ?

Looking at the documentation it appears that I can use the settingsRef property to select which IVY settings file to use but it's not the ivysettings.xml file I wish to modify, rather it's ivy.xml. My use case is the following:

  • I have a main ivy.xml file that I use to fetch my compile-time and run-time dependencies
  • I also have a number of build tool-chain dependencies, i.e. jars used by certain Ant tasks themselves (e.g. stuff like findbugs, cobertura, pmd, code style compliance checking etc.). That's stuff that's neither a compile time nor a run-time dependency of my code. Moreover, it's the same in all the projects I am building with this tool chain so I'd like to be able to identify these dependencies in a single, separate ivy.xml file that I can simply copy across my projects.

回答1:


Short anwer is to use ivy configurations:

http://ant.apache.org/ivy/history/latest-milestone/tutorial/conf.html

Ivy configurations can be used to group dependencies for different purposes. You don't need multiple ivy files.

Example

Here's my resolve target:

<target name="resolve" description="Download dependencies and setup classpaths">
    <ivy:resolve/>
    <ivy:report todir='${reports.dir}/ivy' graph='false' xml='false'/>

    <ivy:cachepath pathid="compile.path" conf="compile"/>
    <ivy:cachepath pathid="test.path"    conf="test"/>
    <ivy:cachepath pathid="build.path"   conf="build"/>
</target>

Which can then be used directly in my various tasks that require a classpath

<!-- Compiling code -->
<javac srcdir="${src.dir}"... classpathref="compile.path"/>

<!-- Testing code --> 
<junit haltonfailure="yes" fork="true">
  <classpath>
    <path refid="test.path"/>
    <pathelement path="${classes.dir}"/>
    <pathelement path="${test.classes.dir}"/>
  </classpath>
  ..
</junit>

<!-- 3rd party ANT tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpathref="build.path"/>

<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="build.path"/>

..

Personally I only use the retrieve task to build archives. Here again I use configurations in order to control which jars I want:

<ivy:retrieve pattern="${build.dir}/libs/[artifact].[ext]" conf="runtime"/>

<war destfile="${war.file}" webxml="${resources.dir}/web.xml">
  <fileset dir="${resources.dir}" excludes="web.xml"/>
  <classes dir="${build.dir}/classes"/>
  <lib dir="${build.dir}/libs"/>
</war>

IVY file

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

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
        <conf name="build"   description="ANT task dependencies"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>

        <!-- Build dependencies --> 
        <dependency org="org.codehaus.sonar-plugins" name="sonar-ant-task" rev="2.1" conf="build->default"/>
       <dependency org="org.jacoco" name="org.jacoco.ant" rev="0.6.3.201306030806" conf="build->default"/>

    </dependencies>

</ivy-module>

The desired configurations are declared at the top. Note how some are set operations. For example The "compile" dependencies are automatically part of "runtime" and "test".

Secondly the configuration mappings are critical:

  • myconf->default : Includes transitive dependencies
  • myconf->master : Just the module jar without dependencies



回答2:


Yes, but you have to specify the file in ivy:resolve, parameter is called file. The reason for his is that retrieve is a post resolve task.




回答3:


So this is how I did it in the end:

<target name="fetch-buildsystem-deps" depends="configure-ivy-settings">
    <ivy:resolve file="ivy-buildsystem.xml"/>
    <ivy:retrieve conf="build-system"
                  pattern="${lib-ivy-buildsystem.dir}/[artifact]-[revision](-[classifier]).[ext]"
                  sync="true"
                  type="jar, bundle"/>
</target>

… where the file ivy-buildsystem.xml identifies only the dependencies of my Ivy tasks. E.g. contains stuff like:

<configurations>
    <conf name="build-system"  description="artifacts needed by the build-system itself"/>
</configurations>
<dependencies>
    <!--dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" conf="build-system->master"/-->
    <dependency org="com.puppycrawl.tools"     name="checkstyle"   rev="5.9"   conf="build-system->default"/>
    <dependency org="com.google.code.findbugs" name="findbugs-ant" rev="3.0.0" conf="build-system->default"/>
    <dependency org="net.sourceforge.pmd"      name="pmd-java"     rev="5.5.3" conf="build-system->default"/>

</dependencies>


来源:https://stackoverflow.com/questions/42166680/ivyretrieve-select-which-ivy-xml-file-to-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!