Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant javascript failonerror

I have an ant task that includes embedded javascript. I'd like to have the target fail or succeed based on some logic I run in the javascript:

<target name="analyze">
    <script language="javascript">
    <![CDATA[
            importClass(java.io.File);
            importClass(java.io.FileReader)
            importClass(java.io.BufferedReader)

            String.prototype.startsWith = function(str) {
                return (this.indexOf(str) === 0);
            }

            String.prototype.endsWith = function(str) {
                var lastIndex = this.lastIndexOf(str);
                return (lastIndex != -1) && (lastIndex + str.length == this.length);
            }

            //setup the source directory
            srcDir = project.getProperty("MY_HOME") + "/foo/src";

            if(srcDir.startsWith("/foo") { 
            //TARGET SHOULD PASS
            } else { 
            //TARGET SHOULD FAIL
            }

    ]]>
    </script>
</target>
like image 571
Amir Afghani Avatar asked Jun 30 '11 23:06

Amir Afghani


3 Answers

You could make Ant exit via the Exit API, but that throws a build exception which will lead to a messy stack trace. The cleanest method would be to set a property in the javascript then test it using the fail task:

Javascript:

project.setProperty( "javascript.fail.message", "There was a problem" );

Ant, immediately after the script task:

<fail if="javascript.fail.message" message="${javascript.fail.message}" />
like image 75
martin clayton Avatar answered Oct 17 '22 14:10

martin clayton


Another target:

<target name="failme"><fail/></target>

Script:

`project.executeTarget("failme");`

Not tested. Documentation

like image 33
Thresh Avatar answered Oct 17 '22 13:10

Thresh


You can do it completely in one JavaScript block.

    <script language="javascript">
        <![CDATA[
                failTask = project.createTask("fail");
                failTask.setMessage("I have failed");
                failTask.perform();
    ]]>
    </script>
like image 39
Portree Kid Avatar answered Oct 17 '22 15:10

Portree Kid