HTML 5 Boiler plate ant script with absolute paths - ant

I am using the html5boiler plate ant build scripts and i am trying to integrate this into my existing platform where i all my css and js files are included using absolute paths. For example:
<!-- scripts concatenated and minified via build script -->
<script type="text/javascript" src="/static/js/common.js"></script>
<script type="text/javascript" src="/static/js/overlay.js"></script>
<script type="text/javascript" src="/static/js/init.js"></script>
<script type="text/javascript" src="/static/js/base.js"></script>
<!-- end scripts -->
However, when i run the build script it tells me the directory does not exist. That is because its not using a relative path to find the files. If i take off the leading / then i can get it to work becuase its relative to my project source folder. Any idea how i can get this working without taking that leading slash off? The idea is to be able to take an existing project and make it work without any changes besides adding the comments in.
UPDATE
The following ANT code is from the html5boiler plate ant build.xml. Basically, my paths in the file.root.page are absolute like i static above. Therefore the scripts.ordered property is outputting file names with an absolute path. So when it gets to the concat command it take an input of script.toconcat with are absolute paths starting with /static. I need to prepend a path to those absolute paths so i now have /content/test.war/static/js/common.js
So basically, im trying to prepend a path such as /content/test.war to the variable scripts.toconcat which under the covers is a list of absolute paths defined above.
<filelist id="file.root" dir="${dir.source}/../${dir.cssjsfileloc}" files="${file.root.page}"/>
<echo message="Concatenating Main JS scripts based on ${file.root.page}..."/>
<apply executable="java" parallel="false"
outputproperty="scripts.ordered">
<arg value="-cp"/>
<arg value="${dir.build.tools}"/>
<arg value="ScriptsToConcat"/>
<first>
<filelist refid="file.root"/>
</first>
</apply>
<filelist id="scripts.toconcat" dir="./${dir.intermediateroot}/" files="${scripts.ordered}">
</filelist>
<!-- overwrite=no here means not to overwrite if the target is newer than the sources -->
<concat destfile="./${dir.intermediate}/${dir.js}/scripts-concat.min.js" overwrite="no">
<filelist refid="scripts.toconcat" />
</concat>
Thanks

This is Ant. Why not have an Ant task that runs after that one do the substitution?
You could call replaceregexp which is a directory paced task for file search and replace.

Related

Ant failing with Content is not allowed in prolog

I am using subant and it is resulting into Content is not allowed in prolog problem.
<subant target="main" genericantfile="build.xml">
<fileset dir = "." />
</subant>
Error:
The following error occurred while executing this line:
pattern.py:1: Content is not allowed in prolog.
Please note I have different files in those folders for example, python files.
When I use explicit listing using the filelist, all works fine.
<subant target="main" genericantfile="build.xml">
<filelist dir="."
files = "A/build.xml,
B/build.xml"
/>
</subant>
With subant you either specify genericantfile along with a dirset (to run the same build file many times, each time with a different basedir) or you omit genericantfile but supply a fileset or other resource collection of build files to run. You are mixing the two styles, and it looks like when you provide a fileset ant is ignoring the genericantfile attribute and treating each element of that fileset as a build file, attempting to parse each of the files as XML, and failing for those that are not XML (i.e. the python files).
<subant target="main">
<fileset dir = "." includes="**/build.xml" />
</subant>
would include only the real build files in the fileset.

Ant absolute path to single file in fileset

I'm using Jenkins with Email-Ext plugin to automate PHP Unit Testing. If the tests failed I want to send a notification email to myself including the zipped test reports.
The Email-Ext plugin requires an Ant Fileset definition in the attachment field.
The zipped test report can be found here:
D:\Test_Reports\test-report-failed.zip
I can't find a working example of the use of fileset with absolute path to a single file.
I tried the following but didn't work:
<fileset file="D:\Test_Reports\test-report-failed.zip" />
Could find any example of using absolute paths but relative paths only.
This is the official help from the Email-ext plugin about the attachment field:
This is the set of attachments that will be used for the email. The format is a comma separated list of Ant include file syntax. The base directory is the workspace.
You can use <include/> tag to do this. It will look like this.
<fileset dir="D:\Test_Reports">
<include name="test-report-failed.zip" />
</fileset>
If you want to get file path in property then you can do this in that way (I've tested this and it works):
<path id="absolute.path.id">
<fileset dir="D:\Test_Reports">
<include name="test-report-failed.zip" />
</fileset>
</path>
<property name="absolute.path" value="${toString:absolute.path.id}" />
<echo>file absolute path: ${absolute.path}</echo>
Just specify the absolute path of your file in the attachment field (not the XML element).
I solved it by changing the Jenkins Job's workspace to the directory where are the test reports and other resources located. Then I was using relative addressing to the zip file like: test-report-failed.zip

can you do this seemingly simple Ant copy task without using ant-contrib's foreach?

I have a source directory with a bunch of plugins. Each plugin has its own lib directory. I want the contents of each of those lib directories to be merged into a single lib directory within my build. In theory you'd do something like this:
<copy todir="build/web/lib">
<fileset dir="web/plugins/*/lib/" includes="**/*" />
</copy>
However, Ant chokes when the dir attribute includes a wildcard. Is ant-contrib the only alternative, or can you make this work with vanilla ant?
Choke message is build.xml:28: [...]/web/plugins/*/lib does not exist.
The dir= attribute of a fileset doesn't take a wildcard - hence the error you see. You need to specify a single directory, in this case web/plugins, and use a slightly different wildcard for the includes:
<copy todir="build/web/lib">
<fileset dir="web/plugins" includes="*/lib/**/*" />
</copy>
If you need to alter the paths as you copy, you can use a mapper, for example the flattenmapper will give you file names with all leading directory information stripped off.

How do I convert an Ant Path into a FileSet?

I'm writing an Ant script to package a project into a WAR file. The software consists of several projects with their own source directories, libraries, etc.
The WAR task has a nested element lib which I'm currently working on. I currently have a reference of the required libs as a Path (containing several FileSets, which I use in a classpath reference. The lib, however, wants the input to be a FileSet, and it refuses a Path.
I tried converting my Path into a FileSet, but then I didn't get it to work as a classpath elsewhere.
Is there a way to convert a Path into a FileSet? I would hate to copy-paste the directories.
<path id="compile.libs">
<fileset dir="${common.path}/lib" includes="*.jar"/>
<fileset dir="${data.path}/lib" includes="*.jar"/>
<fileset dir="${gui.path}/lib" includes="*.jar"/>
<fileset dir="${gui.path}/WebContent/WEB-INF/lib" includes="*.jar"/>
</path>
...when used with <war ..><../> <lib refid="compile.libs"/> </war> leads to:
BUILD FAILED
build.xml:173: compile.libs doesn't denote a zipfileset or a fileset
Assuming the paths are absolute, you can first convert the Path to a comma-delimited list using <pathconvert>, and then convert the list back into a Fileset:
<!-- create path -->
<path id="foo.path">
<pathelement location="/foo/bar/baz.txt"/>
<pathelement location="/qux/quux/quuux.txt"/>
</path>
<!-- convert foo.path to foo.list -->
<pathconvert
refid="foo.path"
property="foo.list"
pathsep=","
dirsep="/"
>
<!--
<fileset> will want relative paths, so we need to strip
the leading /. result: "foo/bar/baz.txt,qux/quux/quuux.txt"
-->
<map from="/" to=""/>
</pathconvert>
<!-- convert foo.list to fileset -->
<fileset id="foo.fileset" dir="/" includes="${foo.list}"/>
(Note the above assumes Unix; you may need to fiddle a bit with separators and whatnot if you're on Windows or you want to make it platform-independent.)
You may have several choices.
You may provide more than one
<lib> nested element to <war>
task. Maybe this would be enough.
You may preassemble all of your
lib files in one temporary
directory and then just reference that
directory as a fileset.
There is an ant-contrib
PathToFileSet task, but it
requires a central root directory,
and this may not be a case with your
compile.libs layout.
Since Ant 1.8.0 you can use a mappedresources. Source: Ant script: Prevent duplication of JAR in javac-classpath war-lib
I think I would try option 1.
I solved this by staging the libs like this :
<copy todir="stage/libs" flatten="true">
<path refid="classpath" />
</copy>
and then using a in the WAR task.simple.
The jars in the classpath used to compile are not the same that needs to be packaged inside the war. For example: I'm sure you need servlet-api.jar to compile your project but you don't need it inside the war because the container provides it. And some jars aren't needed at compile time but at runtime.
I know I'm not answering your question, just want you to think what you are doing.

How to get Flex Builder 3 to generate two builds: one "-use-network=true", another "-use-network=false"?

I'm building a Flex application that will need run under two different deployment scenarios:
First, the application will be hosted on the web. The SWF loads some external resources (images, text) so it requires network access, which is the Flex Builder 3 default build flag "-use-network=true". I don't need to do anything special; it just works.
Second, the application will be written to CD with autorun enabled to launch the index.html hosting the SWF. The SWF still needs to be able to load those same external resources, which reside on the CD in a subfolder. Since those files are on the CD, they are considered local, so Flash security requires the SWF to be built using a flag of "-use-network=false". I add that to the "Additional compiler arguments" text box found under "Flex Compiler" in the Flex project's Properties dialog.
That all works as expected, but it's tedious to have to manually modify the Flex Builder project settings to add or remove that flag as the case may be.
Ideally, I would like to just build the project once and have multiple output folders: one for the network deployment scenario, and another for the local deployment scenario.
What's the best way to do that? Is moving to an Ant build the way to go, or is there a simpler way? If an Ant build configuration is the correct way, do you have an example to share of such multiple build configurations?
Thanks for your help!
Once you get your head around the Ant build, it will make your life a lot easier. Building a multiple build file is no different from a single build file, you will just add an additional task inside of your build with the appropriate settings (you could also use a loop in ant, but that adds complexity)
So, expanding on the Flex Ant Tasks example from the docs, something like this should work (not-tested):
<?xml version="1.0" encoding="utf-8"?>
<!-- myMXMLCBuild.xml -->
<project name="My App Builder" basedir="." default="main">
<taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar" />
<property name="FLEX_HOME" value="C:/flex/sdk"/>
<property name="APP_ROOT" value="apps"/>
<property name="DEPLOY_DIR" value="c:/jrun4/servers/default/default-war"/>
<target name="main" depends="clean, compile1, compile2">
</target>
<target name="compile1">
<mxmlc
file="${APP_ROOT}/Main.mxml"
output="${DEPLOY_DIR}/Main.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="true"
incremental="true"
use-network="true"
>
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript
class hierarchies. -->
<source-path path-element="${FLEX_HOME}/frameworks"/>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- Set size of output SWF file. -->
<default-size width="500" height="600" />
</mxmlc>
</target>
<target name="compile2">
<mxmlc
file="${APP_ROOT}/Main.mxml"
output="${CD_DEPLOY_DIR}/Main.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="true"
incremental="true"
use-network="false"
>
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript
class hierarchies. -->
<source-path path-element="${FLEX_HOME}/frameworks"/>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- Set size of output SWF file. -->
<default-size width="500" height="600" />
</mxmlc>
</target>
<target name="clean">
<delete dir="${APP_ROOT}/generated"/>
<delete>
<fileset dir="${DEPLOY_DIR}" includes="Main.swf"/>
</delete>
</target>
</project>
As a side note, if you are going to be running the Ant build in eclipse/Flash Builder you might as well increase the memory now.

Resources