动态更新常春藤额外属性

时间:2012-04-29 12:25:13

标签: ant ivy

我计划在ivy.xml文件中使用额外的属性,以便能够访问修订号部分,我需要这样做,以便我可以将这些值用于ivysettings中的解析器模式.xml文件:

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:e="http://ant.apache.org/ivy/extra" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info organisation="MyCompany" module="MyModule" revision="1.2.3.4" e:shortrev="1.2.3" e:buildnum="4" publication="20120207140052" />
    ...
</ivy-module>

我想在构建期间动态更新 shortrev buildnum 额外属性,以便在发布此模块时,其值将与其一起保存,以便我可以在ivysettings.xml文件中使用这些额外的属性作为解析器模式。

<resolvers>
    <filesystem name="fs.resolver">
        <ivy pattern="${my.dir}/[organisation]/[module]/[shortrev]/[buildnum]/ivy.xml" />
        <artifact pattern="${my.dir}/[organisation]/[module]/[shortrev]/[buildnum]/[artifact].[ext]" />
    </filesystem>
</resolvers>

我可以尝试使用常春藤文件中的属性动态设置它们的值suggested,但我不清楚如何去做。

1 个答案:

答案 0 :(得分:4)

此示例使用ANT中的标准buildnumber任务。遗憾的是,对于在解析器定义中具有额外属性的模块,ivy buildnumber任务无法正确解析: - (

实施例

$ tree
.
|-- build.xml
|-- ivysettings.xml
|-- ivy.xml
`-- src
    `-- main
        `-- HelloWorld.java

的ivy.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
    <info organisation="MyCompany" module="MyModule" e:shortrev="${publish.target.revision}" e:buildnum="${publish.buildnumber}"/>

    <configurations defaultconfmapping="compile->default">
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Shared library needed at runtime" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <publications>
        <artifact name="MyModule" type="jar" e:shortrev="${publish.target.revision}" e:buildnum="${publish.buildnumber}"/>
    </publications>

    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.6"/>
        <dependency org="junit" name="junit" rev="4.8.2" conf="test->default"/>
    </dependencies>

</ivy-module>

注意:

  • 额外属性与两者信息和工件定义相关联。

的build.xml

<project xmlns:ivy="antlib:org.apache.ivy.ant" name="publish-example" default="build">

    <!--
    ==========
    Properties
    ==========
    -->
    <property name="src.dir"     location="src/main"/>
    <property name="build.dir"   location="build"/>
    <property name="classes.dir" location="${build.dir}/classes"/>
    <property name="reports.dir" location="${build.dir}/reports"/>

    <property name="publish.target.revision" value="1.2.3"/>
    <property name="publish.status"   value="release"/>
    <property name="publish.resolver" value="custom.repository"/>

    <!--
    =======
    Targets
    =======
    -->
    <target name='init' description='Resolve project dependencies and set classpaths'>
        <ivy:resolve/>
        <ivy:report todir='${reports.dir}' graph='false' xml='false'/>

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

        <mkdir dir="${classes.dir}"/>
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="compile.path"/>
    </target>

    <target name="build" depends="compile">
        <ivy:info/>
        <jar destfile="${build.dir}/${ivy.module}.jar" basedir="${classes.dir}"/>
    </target>

    <target name="publish-revision" description="Determine the new published revision">
        <buildnumber/>

        <property name="publish.revision" value="${publish.target.revision}.${build.number}"/>
        <property name="publish.buildnumber" value="${build.number}"/>
    </target>

    <target name="publish" depends="build,publish-revision" description="Publish artifacts into repository">
        <ivy:deliver deliverpattern="${build.dir}/ivy.xml" pubrevision="${publish.revision}" status="${publish.status}"/>

        <ivy:publish resolver="${publish.resolver}" pubrevision="${publish.revision}" overwrite="true">
            <artifacts pattern="${build.dir}/[artifact].[ext]"/>
        </ivy:publish>
    </target>

    <target name="clean" description="--> clean project files">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean" description="--> clean ivy cache">
        <ivy:cleancache />
    </target>

</project>

注意:

  • 常春藤deliver任务可确保已正确填充已发布的常春藤文件。

ivysettings.xml

<ivysettings>
    <settings defaultResolver="central"/>
    <resolvers>
        <ibiblio name="central" m2compatible="true"/>
        <filesystem name="custom.repository">
            <ivy pattern="${ivy.settings.dir}/repository/[organisation]/[module]/[shortrev]/[buildnum]/ivy.xml" />
            <artifact pattern="${ivy.settings.dir}/repository/[organisation]/[module]/[shortrev]/[buildnum]/[artifact].[ext]" />
        </filesystem>
    </resolvers>
</ivysettings>

注意:

  • 我总是将Maven Central设置为我的默认解析器,以便检索第三方依赖项,如commons-lang和junit。
  • custom.repository 解析程序在构建文件中配置为属性“publish.resolver”