I have fromfolder=xxx it has one.txt and
tofolder=yyy same file is there one.txt
While performing copy operation by using ant if it found same name of file is present then it will show alert message like files already present one.txt in log and should not overwrite the file.
<target name="copyPublicHtml" description="Copy Public_html to output directory" >
<touch>
<fileset dir="../html"/>
</touch>
<copy todir="../html" failonerror="on" verbose="on" overwrite="false">
<fileset dir="../src">
</copy>
</target>
You can use the groovy task to iterate thru the files:
<target name="copyPublicHtml" depends="init" description="Copy Public_html to output directory">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<fileset id="srcFiles" dir="src"/>
<groovy>
project.references.srcFiles.each {
def src = new File(it.toString())
def trg = new File("html", src.name)
if (trg.exists()) {
project.log "File already exists: ${trg}"
}
ant.copy(file:it, todir:"html", verbose:"true", overwrite:"false")
}
</groovy>
</target>
Related
I'm trying to create a ant script to compile my jasper files, but I have many "srcdir" and "destdir":
<target name="all">
<jrc
srcdir="many..."
destdir="many..."
tempdir="any"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="**/*.jrxml"/>
</jrc>
</target>
...and I would like it to compile each file to it's own dir. For every ".jrxml" file.
Is there a way?
You can use ant-contrib foreach task to loop over each jrxml file and call the jrc task for each of those. If you don't have it, you'll need to install ant-contrib by copying its JAR file to the lib directory of your Ant installation (if you're using Eclipse, you can add it by going to "Window > Preferences > Ant > Runtime" and adding the JAR into "Ant Home Entries").
The following defines a target "all" that will select all the jrxml files under the current directory. For each of those file, the "jrc" target will be called and the corresponding file will be referenced by the property jrxml.file.
Inside this task, the directory where the jrxml file is located is retrieved with the dirname task and the name of the jrxml file is retrieved with the basename task. The built .jasper file will be created under a folder having the same name as the jrxml file. (It needs to be created first with the mkdir task).
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<target name="all">
<foreach target="jrc" param="jrxml.file">
<path>
<fileset dir=".">
<include name="**/*.jrxml"/>
</fileset>
</path>
</foreach>
</target>
<target name="jrc">
<dirname property="jrxml.dir" file="${jrxml.file}"/>
<basename property="jrxml.filename" file="${jrxml.file}" suffix="jrxml"/>
<mkdir dir="${jrxml.dir}/${jrxml.filename}"/>
<jrc srcdir="${jrxml.dir}"
destdir="${jrxml.dir}/${jrxml.filename}"
tempdir="${jrxml.dir}/${jrxml.filename}"
xmlvalidation="true">
<classpath refid="classpath"/>
<include name="${jrxml.filename}.jrxml"/>
</jrc>
</target>
As an example, if you have a structure:
+folder
+--jrxml
+----Example1.jrxml
+----Example2.jrxml
the result will be
+folder
+--jrxml
+----Example1.jrxml
+----Example1
+------Example1.jasper
+----Example2.jrxml
+----Example2
+------Example2.jasper
I'm trying to migrate from ant to gradle. First phase of this is to move all dependecies to gradle.build and still build war via ant.
In ant building task looks like that:
<fileset id="project-libraries" dir="${project.libs.path}">
<include name="*jar"/>
</fileset>
<path id="master-classpath">
<fileset refid="project-libraries"/>
<fileset refid="tomcat"/>
<fileset refid="hibernate-tools"/>
<fileset refid="findbug"/>
<pathelement path="${build.dir}"/>
</path>
<target name="build" description="Build the application">
<javac destdir="${build.dir}" target="${javac.version}" source="${javac.version}" nowarn="true" deprecation="false" optimize="false" failonerror="true" encoding="utf-8" debug="on">
<src refid="src.dir.set"/>
<classpath refid="master-classpath${master-classpath-version}"/>
<compilerarg value="-Xlint:-unchecked"/>
</javac>
</target>
In Gradle I'm importing build.xml with this code:
ant.importBuild('build.xml') { antTargetName ->
'ant_' + antTargetName
}
The problem is that ant task (./gradlew ant_build) doesn't have dependencies from Gradle (dependencies { ... }). How can I put them into classpath (without modifying ant build)?
You can do the following to add the dependencies to the project's AntBuilder instance:
task antClasspathSetter {
doLast {
def antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.compile.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
}
}
ant_build.dependsOn antClasspathSetter
However, this is a 'hacky' solution.
Using taskdef is a better solution, if the ant build script can be moved to a separate ant task file. In that case, you can do the following:
ant.taskdef(name: 'myAntTask',
classname: 'my.ant.Task',
classpath: configurations.compile.asPath)
I used a copy task to put all of my gradle dependencies into a {libs} folder that I declared on my ant master-classpath.
//add property
<property name="lib.dir" value="${basedir}/lib" /></pre>
//tell ANT to put all jars in folder on classpath
<path id="master-classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
....
</path>
// copy task goes in your build.gradle file
task copyGradleDependenciesInAntFolder(type: Copy) {
from configurations.compile
into 'lib'
}
// make sure to run it before your {ant_build} target
{ant_build}.dependsOn copyGradleDependenciesInAntFolder
I need to process a directory if it has at least one modified file in it. I wrote a block that reduces a fileset to a unique list of the directories that contain those files, but I think this would be easier if there was a way to do this without the script.
Is there a way?
Tricky to do this with core ANT.
Here's an example using an embedded groovy script:
<project name="demo" default="process-modified-dirs">
<path id="build.path">
<pathelement location="/path/to/groovy-all/jar/groovy-all-2.1.1.jar"/>
</path>
<fileset id="modifiedfiles" dir="src">
<modified/>
</fileset>
<target name="process-modified-dirs">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
dirs = project.references.modifiedfiles.collect {
new File(it.toString()).parent
}
dirs.unique().each {
ant.echo("Do something with this dir: ${it}")
}
</groovy>
</target>
</project>
I am using ant unzip task to get contents of a archive file.
Is there a possibility to also save the name of that archive somehow.
Below is the code I am using to unzip an archive.
<unzip dest="${import.dir}">
<fileset dir="${tmp.dir}">
<include name="**/*.zip"/>
</fileset>
</unzip>
Regards,
Satya
You could use an embedded groovy script
<target name="unzip">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<fileset id="zips" dir="${tmp.dir}" includes="**/*.zip"/>
<groovy>
project.references.zips.each { file ->
ant.echo(message:"message goes here", file:"build.log", append:true)
ant.unzip(src:file, dest:properties["import.dir"])
}
</groovy>
</target>
Groovy task is documented here
I'm using a groovy code snippet in an ant build file. Inside the groovy code I'm trying to reference a fileset that has been defined outside of the groovy part, like this:
<target name="listSourceFiles" >
<fileset id="myfileset" dir="${my.dir}">
<patternset refid="mypatterns"/>
</fileset>
<groovy>
def ant = new AntBuilder()
scanner = ant.fileScanner {
fileset(refid:"myfileset")
}
...
</groovy>
</target>
When I execute this I get the following error message:
Buildfile: build.xml
listSourceFiles:
[groovy]
BUILD FAILED
d:\workspace\Project\ant\build.xml:13:
Reference myfileset not found.
What am I missing?
According to the Groovy Ant Task documentation, one of the bindings for the groovy task is the current AntBuilder, ant.
So modifying your script to drop the clashing 'ant' def I got it to run with no errors:
<project name="groovy-build" default="listSourceFiles">
<taskdef name="groovy"
classname="org.codehaus.groovy.ant.Groovy"/>
<patternset id="mypatterns">
<include name="../*.groovy"/>
</patternset>
<target name="listSourceFiles" >
<fileset id="myfileset" dir="${my.dir}">
<patternset refid="mypatterns"/>
</fileset>
<groovy>
scanner = ant.fileScanner {
fileset(refid:"myfileset")
}
</groovy>
</target>
</project>