properties file for Closure-Compiler-ant-task - ant

I am using closure compiler to minify javascript via the ant task. My build file is getting cluttered. Also, I am not minifying the javascript files in dev environment. Currently I am declaring the javascript files in two places. Once inside build.xml in Closure Compiler Ant Task and the other inside FreeMarker template page for un-minified version. I want to move the declaration of javascript files into a comma separated values in a .properties file. How can I configure Closure Compiler Ant Task so that it reads from a properties file ?
<target name="minimize-javascript" description="Create a minified version of the various JS scripts using Closure Compiler">
<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask">
<classpath refid="jars.classpath"/>
</taskdef>
<jscomp compilationLevel="simple"
debug="false" output="${web.dir}/resources/js/minimized.js">
<sources dir="${web.dir}/resources/js/src/deps/jquery/1.8.3/">
<file name="jquery-1.8.3.js"/>
</sources>
<sources dir="${web.dir}/resources/js/src/deps/jquery/ui/1.8.24">
<file name="jquery-ui.js"/>
<file name="jquery.ui.core.js"/>
<file name="jquery.ui.widget.js"/>
<file name="jquery.ui.tabs.js"/>
<file name="jquery.ui.sortable.js"/>
<file name="jquery.ui.selectable.js"/>
<file name="jquery.ui.resizable.js"/>
<file name="jquery.ui.position.js"/>
<file name="jquery.ui.mouse.js"/>
<file name="jquery.ui.droppable.js"/>
<file name="jquery.ui.draggable.js"/>
<file name="jquery.ui.dialog.js"/>
<file name="jquery.ui.button.js"/>
<file name="jquery.effects.core.js"/>
<file name="jquery.effects.drop.js"/>
</sources>
</jscomp>
</target>

Don't know if you solved your problem - you can start with a fileset and it can get all the .js files rather than declaring them one by one.
<fileset dir="${web.dir}/resources/js/src/deps/jquery/ui/1.8.24">
<include name="**/*.js"/>
</fileset>
This means you don't need a properties file.

Related

How to include an optional manifest in an ant jar taks?

<jar destfile="somefile" manifest="META-INF/MANIFEST.MF">
This script runs for multiple projects. Some of the projects have a manifest file, others don't. Build fails on a project that does not have a manifest file.
Is there a way to configure this jar task, so that ant uses project manifest file if it exists and generates a manifest file for a jarred project that doesn't have its own manifest?
Here's a sample I created. This has two jar tasks: jar and jar1. One uses the if check and the other the unless check. The ant target 'all' calls both, but only 1 should run. It checks if the manifest file exsts and if so uses that file, otherwise you can create a local in the jar1 task.
If you want to have only the 1 jar target, then you may need to use the Ant-Contrib tasks and use the tag. Here is a good link to review for this too.
<project name="StackOverflow" basedir="." default="all">
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<target name="all" depends="compile,jar,jar1"/>
<target name="check-manifest">
<available file="META-INF/MANIFEST.MF" property="manifest.present"/>
</target>
<target name="jar" depends="check-manifest" if="manifest.present">
<echo>Using existing manifest.mf</echo>
<jar destfile="hello.jar" manifest="META-INF/MANIFEST"/>
</target>
<target name="jar1" depends="check-manifest" unless="manifest.present">
<echo>creating local manifest</echo>
<jar destfile="hello.jar">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Permissions" value="all-permissions"/>
<attribute name="Codebase" value="*"/>
</manifest>
<fileset dir="${classes.dir}">
<include name="**/*.*"/>
</fileset>
</jar>
</target>
</project>

Deploy JavaFX application with Ant script

I'm trying to generate a executable jar for my JavaFX application with Ant, and the difference between my jar and the one generated by the JavaFX Packager is that the latter include classes from com.javafx.main package.
How can I tell in my Ant script to include these classes in the jar as well ?
The ant file you're using must have the special fx-tasks to deploy the jar, and not the ant built-in jar tasks. Here's a sample ant target for generating a jar with JavaFX:
<target name="jar" depends="compile">
<echo>Creating the main jar file</echo>
<mkdir dir="${distro.dir}" />
<fx:jar destfile="${distro.dir}/main.jar" verbose="true">
<fx:platform javafx="2.1+" j2se="7.0"/>
<fx:application mainClass="${main.class}"/>
<!-- What to include into result jar file?
Everything in the build tree-->
<fileset dir="${classes.dir}"/>
<!-- Define what auxilary resources are needed
These files will go into the manifest file,
where the classpath is defined -->
<fx:resources>
<fx:fileset dir="${distro.dir}" includes="main.jar"/>
<fx:fileset dir="." includes="${lib.dir}/**" type="jar"/>
<fx:fileset dir="." includes="."/>
</fx:resources>
<!-- Make some updates to the Manifest file -->
<manifest>
<attribute name="Implementation-Vendor" value="${app.vendor}"/>
<attribute name="Implementation-Title" value="${app.name}"/>
<attribute name="Implementation-Version" value="1.0"/>
</manifest>
</fx:jar>
</target>
Note, that you must have a taskdef defined somewhere in the script:
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath="${javafx.sdk.path}/lib/ant-javafx.jar"/>
and the project tag must have the fx xmlns reference:
<project name = "MyProject" default ="compile" xmlns:fx="javafx:com.sun.javafx.tools.ant">
The generated jar file should now include the classes from javafx.main and the manifest will include them as an entry point into the application. More info:
http://docs.oracle.com/javafx/2/deployment/packaging.htm

Use wildcards in a Google Closure Compiler Ant build

Is it possible to use wildcards to select multiple files instead of specifying each one with a <file> tag?
Currently I am doing this:
<jscomp compilationLevel="simple" debug="false" output="build.js">
<sources dir="${basedir}/source">
<file name="foo.js" />
<file name="bar.js" />
</sources>
</jscomp>
I tried using <fileset> but <sources> doesn't support it:
<jscomp compilationLevel="simple" debug="false" output="build.js">
<sources dir="${basedir}">
<fileset dir="source">
<include name="**/*.js" />
</fileset>
</sources>
</jscomp>
Looking at the source for the compiler I believe (not tested) that it supports an Ant path as an alternative to the filelist it originally supported. A patch was made to add this. There have been attempts to change from a filelist to a fileset, but the problem there is that filesets are not ordered.
Based on the patch submission, you would do something like this. This example shows a mix of a filelist and a fileset being used:
<path id="ui.js.fileset">
<fileset dir="source">
<include name="**/*.js" />
</fileset>
</path>
<path id="app.js.fileset">
<filelist dir="${js.dir}/app/">
<file name="app.core.js"/>
<file name="app.foo.js"/>
</filelist>
<path refid="ui.js.fileset" />
</path>
<jscomp compilationLevel="simple" debug="false" output="build.js">
<path refid="app.js.fileset" />
</jscomp>
It appears the documentation hasn't been updated to reflect the availability of this option, but it does say there "New features are added occasionally, the source for the task may be the best documentation."

How to use filelist as fileset in uptodate Ant command?

I have a target of build.xml that creates a Zip file. To avoid creating the Zip if no file has been updated, I'd like to check for updates beforehand. AFAIK, uptodate is the task to use.
Here is the relevant (simplified) script sections:
<filelist id="zip-files">
<file name="C:/main.exe" />
<file name="D:/other.dll" />
</filelist>
<target name="zip" depends="zip-check" unless="zip-uptodate">
<zip destfile="${zip-file}" >
<filelist refid="zip-files" />
</zip>
</target>
<target name="zip-check">
<uptodate property="zip-uptodate"
targetfile="${zip-file}">
<srcfiles refid="zip-files" />
</uptodate>
</target>
However, uptodate fails because srcfiles must reference a fileset, not a filelist. Still, I can't use a fileset because it would require a dir attribute, which I can't set because source files do not share a base directory.
Of course, I could just copy all files to a common directory before zipping them, thus being able to use fileset, but I was wondering whether there was an alternative solution.
I'm using Ant 1.8.1
Instead of using <srcfiles>, try using <srcresources>. <srcfiles> must be a fileset, but <srcresource> can be a union of any collection of resources, and that should include a filelist.
I can't do any test right now, but it should look something like this:
<filelist id="zip-files">
<file name="C:/main.exe" />
<file name="D:/other.dll" />
</filelist>
<target name="zip" depends="zip-check" unless="zip-uptodate">
<zip destfile="${zip-file}" >
<filelist refid="zip-files" />
</zip>
</target>
<target name="zip-check">
<union id="zip-union">
<filelist refid="zip-files"/>
</union>
<uptodate property="zip-uptodate"
targetfile="${zip-file}">
<srcresources refid="zip-union" />
</uptodate>
</target>
Hope it works for you.

how do I specify a list of folders for subant task

I need to be able to call a subant task on a prespecified list of project folders which may or may not be siblings.
I know I can call subant task like this:
<subant antfile="custom-build.xml" target="custom-target"
inheritall="false" failonerror="true">
<filelist dir="${parent.dir}">
<file name="project1"/>
<file name="project2"/>
...
<file name="projectn"/>
</filelist>
</subant>
This is fine, if all "projects" are siblings in the filesystem, but it might not be the case, so I need a way to tell subant the explicit list of folders by absolute pathnames to look for the custom-build.xml files to execute.
Adding more filelist, one for each path, like this:
<subant antfile="custom-build.xml" target="custom-target"
inheritall="false" failonerror="true">
<filelist dir="${parent.dir}">
<file name="project1"/>
<file name="project2"/>
...
<file name="projectn"/>
</filelist>
<filelist dir="${another.parent.dir}">
<file name="projectA"/>
<file name="projectB"/>
...
<file name="projectZ"/>
</filelist>
</subant>

Resources