Gangmax Blog

Check Files Exist or Not in Ant

Here’s ANT code how to determine if there’s Java files in “src” directory or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<resourcecount property="src.java.file.count">
<fileset dir="src" erroronmissingdir="false">
<include name="**/*.java"/>
</fileset>
</resourcecount>
<echo message="src.java.file.count = ${src.java.file.count}"/>
<script language="javascript">
<![CDATA[
var srcJavaFileCount = parseInt(project.getProperty('src.java.file.count'));
project.setProperty('has.java.file', srcJavaFileCount > 0);
]]>
</script>
<echo message="has.java.file = ${has.java.file}"/>
<condition property="has-src">
<and>
<available file="src"/>
<equals arg1="${has.java.file}" arg2="true"/>
</and>
</condition>
<echo message="has-src = ${has-src}"/>

Refer here, here, and here.

Add@20120523:

For the “script” task, more details can be described.

From the “script“ task description we can know that after Ant 1.7 the “script” task can be used with either “BSF” or “JSR223“. “JSR223” was embedded in JDK after JDK6. So After JDK6, ant 1.7 or later version can use the “script” task without any other dependencies.

And also, if you want to know what scripting languages are supported by your current JDK, please run the following Java class:

TestScript.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public class TestScript {
public static void main(String[] args) {
ScriptEngineManager sem = new ScriptEngineManager();
for(ScriptEngineFactory factory : sem.getEngineFactories()) {
System.out.println("Engine name is: " + factory.getEngineName());
System.out.println("Engine version is: " + factory.getEngineVersion());
System.out.println("Engine extensions are: " + factory.getExtensions());
System.out.println("Language name is: " + factory.getLanguageName());
System.out.println("Language version is: " + factory.getLanguageVersion());
System.out.println("Mime Types are: " + factory.getMimeTypes());
System.out.println("Name are: " + factory.getNames());
}
}
}

Compile this Java source file to Java class file, then run:

1
2
javac ./TestScript.java
java TestScript

Here’s some kinds of JDK output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1. For "IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux x86-32 jvmxi3260sr9-20110624_85526 (JIT enabled, AOT enabled)":

Engine name is: Mozilla Rhino
Engine version is: 1.6 release 2
Engine extensions are: [js]
Language name is: ECMAScript
Language version is: 1.6
Mime Types are: [application/javascript, application/ecmascript, text/javascript, text/ecmascript]
Name are: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]


2. For "Java(TM) SE Runtime Environment (build 1.6.0_30-b12)":

Engine name is: Mozilla Rhino
Engine version is: 1.6 release 2
Engine extensions are: [js]
Language name is: ECMAScript
Language version is: 1.6
Mime Types are: [application/javascript, application/ecmascript, text/javascript, text/ecmascript]
Name are: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]


3. For "Java(TM) SE Runtime Environment (build 1.7.0_01-b08)":

Engine name is: Mozilla Rhino
Engine version is: 1.7 release 3 PRERELEASE
Engine extensions are: [js]
Language name is: ECMAScript
Language version is: 1.8
Mime Types are: [application/javascript, application/ecmascript, text/javascript, text/ecmascript]
Name are: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]

So it seems “Mozilla Rihno” is the only script language supported by JDK scripting by default. Any other scripting language wanted to be used using JDK scripting support should have some more configuration.

Comments