Skip to content

Instantly share code, notes, and snippets.

@nicholashagen
Created February 8, 2012 16:07
Show Gist options
  • Save nicholashagen/1770746 to your computer and use it in GitHub Desktop.
Save nicholashagen/1770746 to your computer and use it in GitHub Desktop.
Trimming Spaces in Ant
<!-- task to trim spaces on properties in Ant via inline javascript (requires 1.7+) -->
<!-- NOTE that properties in Ant are immutable so you must define the variable with a different name -->
<target name="trim-properties">
<script language="javascript">
<![CDATA[
var myproperty= project.getProperty("myproperty");
if (myproperty!= null) {
project.setProperty("myproperty.trimmed", myproperty.trim())
}
]]>
</script>
<echo>myproperty now set to -${myproperty.trimmed}-</echo>
</target>
@cobexer
Copy link

cobexer commented Dec 12, 2013

Awesome!
however i just tried it out and it failed for me on openSUSE (Factory) with ant 1.9.2 and java version "1.7.0_45" (OpenJDK)... adding manager="javax" as described here: http://ant.apache.org/manual/Tasks/script.html fixed the issue for me.

build.xml:5: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.bsf.BSFManager.<init>(BSFManager.java:102)
        at org.apache.tools.ant.util.optional.ScriptRunner.createManager(ScriptRunner.java:170)
        at org.apache.tools.ant.util.optional.ScriptRunner.executeScript(ScriptRunner.java:96)
        at org.apache.tools.ant.taskdefs.optional.Script.execute(Script.java:52)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:435)
        at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:179)
        at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:93)
        at org.apache.tools.ant.Main.runBuild(Main.java:826)
        at org.apache.tools.ant.Main.startAnt(Main.java:235)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 18 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment