Ant script to choose between multiple version of classpaths

你离开我真会死。 提交于 2019-11-29 18:29:17

I'd suggest using Apache ivy for managing complex classpaths. It externalizes your build dependencies into a separate ivy.xml file.

Secondly, ivy can automatically download such dependencies, reducing the size of your project under source control.

Finally, this solution at first glance might appear horribly complex. It's advantage is that it's compatible with other build technologies such as Maven.

Example

ivy.xml

Ivy uses "configurations" to manage logical groupings of jars.

In this example the code compiles against the SLF4J api jars, but at run-time use different logging implementations:

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

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime.simple"  description="Runtime environment with minimal logging" extends="compile"/>
        <conf name="runtime.complex" description="Runtime environment with logback enabled" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime.simple"/>
        <conf name="build"   description="ANT tasks used by build"/>
    </configurations>

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

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

        <!-- complex runtime dependencies -->
        <dependency org="ch.qos.logback" name="logback-classic" rev="1.0.3" conf="runtime.complex->default"/>

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

        <!-- Build dependencies -->
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.6" conf="build->default"/>
    </dependencies>

</ivy-module>

Notes:

  • The extends attribute enables the creation of jar union sets
  • By default ivy will download from Maven Central (an open repository that now hosts approx 90% of Java open source software).
  • Using the conf attribute you can associated a depedency against one or more of your locally defined configurations.
  • Ivy can also be used to manage 3rd party ANT task dependencies

build.xml

The ivy ANT tasks are imported as an antlib. The ivy cachepath task is used to turn an ivy managed configuration into normal ANT paths and the ivy report task produces a dependency report.

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

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

        <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

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

The ivy retrieve task is used to populate a directory during your application's packaging phase:

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

    <war destfile="myapp.war" webxml="src/metadata/myapp.xml">
        <fileset dir="${src.dir}/html/myapp"/>
        <fileset dir="${src.dir}/jsp/myapp"/>
        <lib dir="${build.dir}/libs"/>
        <classes dir="${build.dir}/classes"/>
    </war>
</target>

IDE configuration files

An Eclipse plugin for ivy is available.

It's also possible to generate IDE configuration files using an embedded groovy task. Following is an Eclipse example:

<target name="eclipse">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <ivy:cachefileset setid="libfiles" conf="compile"/>

    <groovy>
    <arg value="${src.dir}"/>
    <arg value="${build.dir}/classes"/>

    import groovy.xml.MarkupBuilder

    //
    // Generate the project file
    //
    project.log("Creating .project")

    new File(".project").withWriter { writer ->
        def xml = new MarkupBuilder(writer)

        xml.projectDescription() {
            name(project.name)
            comment()
            projects()
            buildSpec() {
                buildCommand() {
                    name("org.eclipse.jdt.core.javabuilder")
                    arguments()
                }
            }
            natures() {
                nature("org.eclipse.jdt.core.javanature")
            }
        }
    }

    //
    // Generate the classpath file
    //
    // The "lib" classpathentry fields are populated using the ivy artifact report
    //
    project.log("Creating .classpath")

    new File(".classpath").withWriter { writer ->
        def xml = new MarkupBuilder(writer)

        xml.classpath() {
            classpathentry(kind:"src",    path:args[0])
            classpathentry(kind:"output", path:args[1])
            classpathentry(kind:"con",    path:"org.eclipse.jdt.launching.JRE_CONTAINER")

            project.references.libfiles.each {
                classpathentry(kind:"lib", path:it)
            }
        }
    }
    </groovy>        
</target>

I would like to share the approach that I finally implemented.

There were classpath, settings and some project config xmls that were dependent on runtime.

In each project we created a runtime_classpah & runtime_settings and configxml_runtime version of each file.

Created a target in ant that takes in runtime as param ,itrates over each project & copies contents of classpath_runtime to classpath ,setting_runtime to settings.

And a target that overrites configxml with contents of configxml_runtime

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