how to excludes and include task in ant script - ant

I want to copy only specific jar file using ant script. It is very easy task but i am not able to do this. I have written the below ant script for this task. I want to excludes all jar file except aopalliance-.jar file. But when i run this script it excludes all the jar file. can someone correct this script .
<?xml version="1.0" encoding="UTF-8"?>
<project default="mvcexample" name="MVCExample" basedir=".">
<property name="web.dir" value="WebContent" />
<property name="webinf.dir" value="WebContent/WEB-INF" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="lib2.dir" value="WebContent/WEB-INF/lib2" />
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}" excludes="**/*.jar">
<include name="${lib.dir}/aopalliance-.jar"></include>
</fileset>
</copy>
</target>
</project>

Here's what you need to do:
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}">
<!-- <include name="${lib.dir}/aopalliance-.jar"/> -->
<include name="aopalliance-.jar"/> <!-- Don't put the dir name! -->
</fileset>
</copy>
</target>
When you specify a directory, you merely specify the name pattern under that directory. What you were asking for was the jar ${lib.dir}/${lib.dir}/aopalliance-.jar. By the way, is there suppose to be a version number in there somewhere? Like aopalliance-3.4.jar? If so, you need <include name="aopalliance-*.jar"/>.
Also notice that if I used <include .../> instead of <include...></include>.

Related

Ant fileset matching only files but excluding all sub directories

How do I copy only files immediately under the directory and not its subdirectories? I don't know a priori the names of the files or the subdirectories. I've tried the following to no avail:
<include name="*"> # includes all files and subdirs
===
<include name="*">
<exclude name="*/**> # or "*/" or "**"
Any help would be appreciated.
UPDATE:
I ended up just adding a delete task to delete the copied subdirectories:
<delete includeemptydirs="true">
<fileset dir="${targetdir}">
<type type="dir"/>
</fileset>
</delete>
<?xml version="1.0" encoding="UTF-8"?>
<project default="copy-top-most-files-only" name="My Project">
<property name="to.dir" value="/path/to/dest/folder/" />
<property name="from.dir" value="/path/to/source/folder/" />
<target name="copy-top-most-files-only">
<copy todir="${to.dir}" includeEmptyDirs="false" >
<fileset dir="${from.dir}">
<exclude name="*/**/*" />
</fileset>
</copy>
</target>
</project>
One way to do this is to specify a <depth> selector, for example:
<copy todir="dest">
<fileset dir="src">
<depth max="0" />
</fileset>
</copy>
That will prevent subdirectories from being copied.
You can combine selectors with include/exclude rules if needed.

Execute task before directory is loaded

I have a build.xml with several <taskdef>s and <target>s. Furthermore, a <property> and a <path> are set:
<property name="foo" location="/some/path/elsewhere" />
<path id="foo.path">
<fileset dir="${foo}">
<include name="*.jar" />
<include name="**/*.jar" />
</fileset>
</path>
One <target> trys to <move> (rename) directories which are under foo.path. This doesn't work because Ant already loaded that stuff, which blocks <move> on system level.
Is there a way to execute some sort of task before the directory in <property> and/or <path> is loaded?
EDIT: <move> task looks as follows:
<target name="moveDirs" if="some.condition">
<move todir="/some/path_old">
<fileset dir="/some/path"/>
</move>
<move todir="/some/path">
<fileset dir="/some/path_new"/>
</move>
</target>
Basically, I'm trying to rename /some/path to /some/path_old and /some/path_new to /some/path. As mentioned before, foo.path points to a folder under /some/path.

ant build script don't run on windows using command line

i have written an ant script, which runs ok and generate the .jar file when i use it with eclipse.
But when i use it on command prompt on windows xp, it's shows successfull, but nothing happens. ant is properly configured and also i can run other ant scripts.
here is my build.xml file
<?xml version="1.0"?>
<project name="TaskNodeBundle" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="bundlename" value="task-node-bundle" />
<property name="src.dir" location="../src" />
<property name="lib.dir" location="../lib" />
<property name="build.dir" location="/buildoutput" />
<property name="build.dest" location="build/dest" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<!-- Deletes the existing build directory-->
<target name="mkdir" depends="clean">
<mkdir dir="${build.dest}"/>
</target>
<!-- Compiles the java code -->
<target name="compile" depends="mkdir">
<javac srcdir="${src.dir}" destdir="${build.dest}" classpathref="classpath" />
</target>
<target name="package-bundle" depends="compile" description="Generates the bundle">
<jar destfile="${dist.dir}/${bundlename}.jar">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
</target>
</project>
When you execute an ant script from the command line it will execute the first target defined in the build.xml file (in your case clean).
You can specify the target(s) to be executed on the command line
$ ant target1 target2
or define a default target in your build.xml file with the default attribute of the <project> tag:
<project name="TaskNodeBundle" basedir="." default="package-bundle">

Ant: simplest way to copy over a relative list of files

Using Ant, I want to copy a list of files from one project to another, where each project has the same directory structure. Is there a way to get the following to work?
<project name="WordSlug" default="pull" basedir=".">
<description>
WordSlug: pull needed files
</description>
<property name="prontiso_home" location="../../prontiso/trunk"/>
<!-- I know this doesn't work, what's the missing piece? -->
<target name="pull" description="Pull needed files">
<copy todir="." overwrite="true">
<resources>
<file file="${prontiso_home}/application/views/scripts/error/error.phtml"/>
<file file="${prontiso_home}/application/controllers/CacheController.php"/>
<!-- etc. -->
</resources>
</copy>
</target>
</project>
Success is deriving the paths automatically:
${prontiso_home}/application/views/scripts/error/error.phtml copied to ./application/views/scripts/error/error.phtml
${prontiso_home}/application/controllers/CacheController.php copied to ./application/controllers/CacheController.php
Thanks!
Try creating a fileset:
<copy todir="." overwrite="true">
<fileset dir="${prontiso_home}/application/">
<include name="views/scripts/error/error.phtml,controllers/CacheController.php"/>
</fileset>
</copy>
Typically, however, you use fileset to define patterns of files to copy, such as "copy all PHP files in my application directory:
<fileset dir="${prontiso_home}/application/">
<include name="**/*.phtml,**/*.php"/>
</fileset>
OP here. I have a working solution:
<project name="WordSlug" default="pull" basedir=".">
<description>
WordSlug: pull needed files
</description>
<property name="prontiso_home" location="../../prontiso/trunk"/>
<target name="pull" description="Pull needed files">
<copy todir="." overwrite="true" verbose="true">
<fileset dir="${prontiso_home}">
<or>
<filename name="/application/views/scripts/error/error.phtml"/>
<filename name="/application/controllers/CacheController.php"/>
</or>
</fileset>
</copy>
</target>
</project>

Common jar task in ant build script

I have a bunch of ant projects that build plug-ins that all conform to the same API. The plug-ins incorporate the third-party libraries that they depend on so that they are self-contained. As much of the behaviour in the individual build scripts was similar, I decided to pull out the common parts to a common build script.
The original build scripts looked something like this:
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib1.jar" />
<zipfileset src="externlib2.jar" />
</jar>
</target>
...
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib2.jar" />
<zipfileset src="externlib3.jar" />
</jar>
</target>
...
</project>
Here is what my build scripts look like after refactoring:
Common.xml:
<project name="Common" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="common-manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset refid="extern.libs" />
</jar>
</target>
...
</project>
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib1.jar" />
<file file="externlib2.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib2.jar" />
<file file="externlib3.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
However, the refactored build does not work--I believe that the problem is that I cannot declare a zipfileset that has multiple files.
I cannot figure out a way that I can declare a fileset such that the behaviour with the common jar task is the same as the behaviour where the jar tasks are declared in each project's build script. Has anyone solved this problem before? Is there a different way I can accomplish the same thing?
It's a bit fiddly, but the zipgroupfileset of the jar task may help.
Something like this for ProjectA should work. I've guessed the directory, adjust as appropriate.
<fileset dir="${project.build.lib.dir}" id="extern.libs">
<include name="externlib1.jar" />
<include name="externlib2.jar" />
</fileset>
Then in the common.xml file, I've renamed your common-manifest.mf.
<target name="jar">
<jar destfile="${project.target}" duplicate="preserve" manifest="common-manifest.mf">
<zipgroupfileset refid="extern.libs"/>
</jar>
</target>

Resources