【问题标题】:Retrieve specific jar from ant ivy从 ant ivy 中检索特定的 jar
【发布时间】:2015-06-18 13:55:20
【问题描述】:

我在一个项目中使用 ant + ivy。因此,假设我需要为 ant 运行 <sql 任务,因此我需要获取 jdbc 驱动程序 1st。此外,在编译项目期间需要驱动程序。所以我想要2个配置:

  • 默认:检索jdbc驱动和其他项目依赖
  • jdbc:仅检索 jdbc 驱动程序。

然后使用不同的配置运行检索任务:

<!--Fetch all project dependencies, including jdbc driver-->
<ivy:retrieve pattern="${build.lib.home}/[artifact].[ext]" conf="default" />


<!-- Fetch only jdbc driver-->
<ivy:retrieve pattern="${build.lib.home}/[artifact].[ext]" conf="jdbc" />

ivy.xml

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info organisation="" module="notebook-ivy"/>

    <configurations>
        <conf name="default" visibility="public" extend="jdbc"/>
        <conf name="jdbc" visibility="public"/>
    </configurations>

    <dependencies>
        <dependency org="mysql" name="mysql-connector-java" rev="5.1.6" conf="jdbc->default"/>
        <dependency org="org.apache.camel" name="camel-core" rev="2.15.1"/>

    </dependencies>
</ivy-module>

我正在使用公共 mavencentral,因此无法更改服务器上的依赖项配置: ivysettings.xml

<ivysettings>
  <settings defaultResolver="chain"/>
  <resolvers>
    <chain name="chain">
      <ibiblio name="central" m2compatible="true" root="http://central.maven.org/maven2/"/>
    </chain>
  </resolvers>
</ivysettings>

上述配置有效。但是,当 default extends jdbcjdbc extends default 同时出现时,它看起来很混乱。我是 ivy 新手,所以我的问题是:这是否是为 ivy 使用配置的正确方法。

【问题讨论】:

    标签: ant ivy


    【解决方案1】:

    “扩展”操作使您能够在 ivy 配置中对 jar 执行联合集操作,因此可以正常工作。

    我的偏好是根据预期的类路径要求对配置进行建模:

    <configurations>
        <conf name="compile" description="Dependencies required to build project"/>
        <conf name="compile" description="Dependencies required to run project" extends="compile"/>
        <conf name="test" description="Dependencies required to test project" extends="runtime"/>
        <conf name="build" description="ANT build tasks"/>
    </configurations>
    

    然后可以使用 ivy cachepath 任务在构建文件中创建这些路径:

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

    这种方法意味着类似 jdbc jar 的东西将被映射到“编译”配置,使其可用于 javac 任务:

      <target name="compile" depends="resolve">
        ..
        <javac ... classpathref="compile.path"/>
      </target>
    

    但也包含在构建 jar 包时作为依赖项保存到磁盘的“运行时”配置中:

      <target name="build" depends="compile">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact].[ext]" conf="runtime"/>
    
        <manifestclasspath property="jar.classpath" jarfile="${dist.jar}">
          <classpath>
            <fileset dir="${dist.dir}/lib" includes="*.jar"/>
          </classpath>
        </manifestclasspath>
    
        <jar destfile="${dist.jar}" basedir="${build.dir}/classes">
          <manifest>
            <attribute name="Main-Class" value="${dist.main.class}"/>
            <attribute name="Class-Path" value="${jar.classpath}"/>
          </manifest>
        </jar>
      </target>
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2018-02-24
      • 2017-11-15
      • 1970-01-01
      • 2017-07-06
      • 2015-01-11
      • 2013-11-25
      • 2013-12-31
      • 2018-03-25
      相关资源
      最近更新 更多