Ant-javac doesn't see dependencies from Gradle - ant

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

Related

Get directories with modified files with Ant

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>

No output from Checkstyle in ANT

I am not using an automated build tool. Just Checkstyle 5.5 and ANT 1.8.
I am trying to have Checkstyle run in my ANT script. The ANT script executes without error, but doesn't seem to call Checkstyle. I get no output except ANT reports BUILD SUCCESSFUL.
Here is my ant script:
<project name="ccu" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
<target name="checkstyle" description="Generates a report of code convention violations.">
<cs:checkstyle config="custom_check.xml">
<fileset dir="src" casesensitive="yes">
<include name="**/*.java"/>
</fileset>
<!--
<fileset dir="src" includes="**\*.java"/>
-->
</cs:checkstyle>
</target>
</project>
what am i missing?
It was a classpath problem. For some reason I needed to direct the ANT classpath to the class files not the jar.
My final script looks like this:
<project name="ccu" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
<taskdef resource="checkstyletask.properties">
<classpath>
<pathelement location="C:\myClasses\bin"/>
<pathelement location="C:\checkstyle-5.5\checkstyle-5.5-all.jar"/>
</classpath>
</taskdef>
<checkstyle config="custom_check.xml">
<fileset dir="src" includes="**/*.java"/>
</checkstyle>
</project>

How to compile a .drl file through an ant build script

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.

How to get archive name while unzipping

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

groovy inside ant: how to access refids from grooy that are defined by ant tags

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>

Resources