steros
steros

Reputation: 1924

ant run target if file has specifc suffix

I pass a filename to ant script via ant -Dfilepath=/foo/bar/foobar.suffix I want to copy it to a destination and if it is a .js file generate a compiled version of it. This works but currently the compile task runs on all files not just .js file. How do I exclude non .js files in the "runjscompile" task? In a fileset I would do this (but I don't get how to apply this on the task):

<fileset dir="${foo}" casesensitive="yes">
    <exclude name="**/*.min.js" />
    <include name="**/*.js" />
</fileset>

My build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." default="build">
    <taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask" classpath="/home/bar/bin/compiler.jar" />
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="/usr/share/java/ant-contrib.jar" />
        </classpath>
    </taskdef>

    <property name="serverRoot" value="/home/bar/server/public_html" />
    <property name="foo" value="${serverRoot}/foo/" />

    <property name="workspaceRoot"
        value="/home/bar/Zend/workspaces/DefaultWorkspace/" />
    <property name="foo_service" value="${workspaceRoot}/foo_service/" />
    <property name="filepath" value="${filepath}" />

    <target name="build" depends="transferFile, runjscompile" />

    <target name="transferFile" description="overwrite old file">
        <basename property="filename" file="${filepath}" />
        <dirname property="path" file="${filepath}" />
        <pathconvert property="path.fragment" pathsep="${line.separator}">
            <propertyresource name="path" />
            <mapper type="regexp" from="^/[^/]+/(.*)" to="\1" />
        </pathconvert>
        <echo message="copy ${workspaceRoot}${filepath} to ${foo}${path.fragment}${filename}" />
        <copy file="${workspaceRoot}${filepath}" tofile="${foo}${path.fragment}${filename}"
            overwrite="true" force="true" />
        <property name="destFile" value="${foo}${path.fragment}${filename}" />
    </target>

    <target name="runjscompile">
        <echo message="compile ${destFile}" />
        <basename property="file" file="${destFile}" />
        <basename property="prefix" file="${destFile}" suffix=".js" />
        <dirname property="directory" file="${destFile}" />

        <echo message="Compressing file ${file} to ${directory}/${prefix}.min.js" />
        <jscomp compilationLevel="simple" debug="false" output="${directory}/${prefix}.min.js" forceRecompile="true">
            <sources dir="${directory}">
                <file name="${file}" />
            </sources>
        </jscomp>
    </target>
</project>

Upvotes: 2

Views: 320

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

Add another target which checks the file suffix with a <condition> and sets a property if it matches, then make the jscompile target conditional on that. Following your fileset example you probably want something like:

<target name="check.js">
  <condition property="do.jscompile">
    <!-- check for filepath that ends .js but not .min.js -->
    <matches string="${filepath}" pattern=".*(?&lt;!\.min)\.js$$" />
  </condition>
</target>

<target name="build" depends="check.js, transferFile, runjscompile" />

<target name="runjscompile" if="do.jscompile">

Upvotes: 1

Related Questions