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
Related
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 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>
I tried to implement the awstasks Class for uploading files via an ant script to an S3 bucket.
Downloaded all dependencies and used the following code:
<taskdef name="S3Upload" classname="dak.ant.taskdefs.S3Upload">
<classpath refid="classpath.compile"/>
</taskdef>
<target name="final2S3">
<basename property="customer.id" file="${basedir}"/>
<basename property="customer.name" file="${basedir.parent.parent}"/>
<basename property="customer.campaign" file="${basedir.parent}"/>
<basename property="customer.final" file="${basedir.parent}\_final\${customer.id}\"/>
<S3Upload verbose="true"
accessId="${aws.accessId}"
secretKey="${aws.secretKey}"
bucket="${aws.bucket}\${customer.id}"
publicRead="true">
<fileset dir="${customer.final}">
<include name="**/*.json"/>
<include name="**/*.swf"/>
</fileset>
</S3Upload>
</target>
I get the following error:
Reference classpath.compile not found.
I tried to set the property classpath.compile to the lib folder in the Ant home but nothing changes.
I am new to Drools. I want to know if it is possible to compile a .drl file using some kind of a command that can be entered in the windows command line (shell/cmd). I looked through the binaries that come with the drools distribution but I am unable to figure out a way to compile a .drl file.
The reason I am interested in such a command is that I want to write an ant build file which will compile my java classes and rules and create a jar. This jar should be self sufficient, i.e running the jar from the command line should run the main program, which passes facts in the session causing the rules that operate on these facts to automatically be executed.
The DroolsCompilerAntTask used to be the way to do this. It would take all your various rule files and compile them into a serialized file. It appears to have some bugs in 5.3 though which I am currently trying to work out. In the meantime, here is an illustrative build file that can be used for creating an executable JAR based on Drools. The build will fail if the rules cannot be compiled.
<project name="DroolsProto" default="dist" basedir=".">
<property name="build.src" location="src"/>
<property name="build.target" location="target"/>
<property name="build.dist" location="dist"/>
<property name="build.artifact" value="droolsproto"/>
<property name="one-jar.dist.dir" value="~/Work/Data/Misc/OneJar"/>
<property name="one-jar.version" value="0.97"/>
<property name="one-jar.ant.jar" value="${one-jar.dist.dir}/one-jar-ant-task-${one-jar.version}.jar"/>
<path id="build.lib.path">
<fileset dir="${build.src}/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask"
classpath="${one-jar.ant.jar}" onerror="report"/>
<taskdef name="droolscompiler" classname="org.drools.contrib.DroolsCompilerAntTask">
<classpath refid="build.lib.path"/>
</taskdef>
<target name="clean">
<delete dir="${build.target}"/>
<delete dir="${build.dist}"/>
</target>
<target name="init">
<tstamp/>
<mkdir dir="${build.target}"/>
<mkdir dir="${build.dist}"/>
</target>
<target name="compile" depends="init">
<mkdir dir="${build.target}/classes"/>
<javac srcdir="${build.src}/main/java" destdir="${build.target}/classes">
<classpath refid="build.lib.path"/>
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</javac>
</target>
<target name="verify-rules">
<droolscompiler srcDir="${build.src}/main/resources" toFile="${build.target}/classes/foo.foo">
<classpath refid="build.lib.path"/>
</droolscompiler>
</target>
<target name="verify-resources" depends="verify-rules"/>
<target name="bundle-resources" depends="verify-resources">
<copy todir="${build.target}/classes">
<fileset dir="${build.src}/main/resources"/>
</copy>
</target>
<target name="dist" depends="compile, bundle-resources">
<one-jar destfile="${build.dist}/${build.artifact}.jar">
<manifest>
<attribute name="One-Jar-Main-Class" value="org.drools.examples.HelloWorldExample"/>
</manifest>
<main>
<fileset dir="${build.target}/classes"/>
</main>
<lib>
<fileset dir="${build.src}/lib">
<include name="**/*.jar"/>
</fileset>
</lib>
</one-jar>
</target>
</project>
Note that the build uses One-Jar in order to create the self-contained executable, you may wish to substitute this with your 'Super Jar™' tool of choice. There is also a DroolsVerifierAntTask which allegedly can check logical errors in your rules (as opposed to syntactical ones), but I have no hands on experience with it.
You can use something like this:
private static void compile(final String srcFile, final String destFile) throws IOException {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
URL src = FormChecker.class.getResource(srcFile);
Resource r = ResourceFactory.newInputStreamResource(src.openStream());
kbuilder.add(r, ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new IllegalStateException("Can not initialize Drools: " + kbuilder.getErrors().toString());
}
Collection<KnowledgePackage> kpackages = kbuilder.getKnowledgePackages();
File dest = new File(destFile);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
out.writeObject(kpackages);
out.close();
}
There's a drools-ant jar in the droolsjbpm-tools zip.