Ant script to choose between multiple version of c

2020-02-15 08:13发布

I am new to Ant scripts.

below is description of requirement

in my workspace, there are various projects and I have to have my project work on RAD and eclipse IDE as well as Websphere , tomcat and jboss environment.. i have made project specific settings to make the project work on RAD and websphere and eclipse and tomcat n jboss..

but there are changes in several files like classpath n few config files.

this leaves me with three versions of workspace.

but my idea is to have one workspace with multiple version of classpath for eg. classpath_eclipse, classpath_rad etc.. and have an ant script that will choose between the right files during build depending on which ide.

So guys please suggest some approach how can i implement this approach. totally new to ant. .:/

标签: eclipse ant rad
2条回答
该账号已被封号
2楼-- · 2020-02-15 08:40

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

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-15 08:53

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>
查看更多
登录 后发表回答