如果文件具有特定后缀,则运行目标

时间:2014-04-11 08:09:47

标签: file ant target

我将文件名传递给ant脚本 ant -Dfilepath=/foo/bar/foobar.suffix 我想将它复制到目的地,如果是.js文件,则生成它的编译版本。 这有效,但目前编译任务在所有文件上运行,而不仅仅是.js文件。 如何在“runjscompile”任务中排除非.js文件? 在文件集中我会这样做(但我不知道如何在任务上应用它):

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

我的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>

1 个答案:

答案 0 :(得分:1)

添加另一个用<condition>检查文件后缀的目标,如果匹配则设置属性,然后使jscompile目标成为条件。按照fileset示例,您可能需要以下内容:

<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">