executable jar from groovy sources - ant

I could not find a proper answer to the following question : while creating an executable jar from groovy compiled sources, I cannot simply include groovy-all-version.jar in a lib directory (as I would do in java) but I need to extract all the classes from the jar and package them with my own classes as otherwise I get an exception. This actually is the case with all libraries I may want to use.
Not clear ? Let's see an example.
toto.groovy belonging to package titi is compiled in a build directory which looks like this :
build
lib
groovy-all-1.8.4.jar
titi
toto.class
and a MANIFEST.MF that will be included in the jar contains the lines:
Main-Class: titi.toto
Class-Path: lib/groovy-all-1.8.4.jar
Executable jar creation is done with the following ant target in build.xml:
<jar destfile="${build.exe.dir}/${exe.name}"
basedir="${build.dir}"
manifest="${src.conf.dir}/MANIFEST.MF"
/>
Now if I try to execute the jar, I get
$ java -jar target/toto.jar
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script
(I've checked that the groovy.lang.Script class exists.)
On the other hand, if I unjar groovy-all-version.jar in build by adding an unjar task before the jar, execution goes fine. OK, it works, but it slows down the compilation process and I'd like to understand...
Any feedback is highly appreciated.

Related

#Grab annotation fails under Ant

I'm using a #Grab annotation to grab the definition of an Html parser I can give to the XMLSlurper (I think it's the tagsoup parser) and all is good when I run my script from the cmd line. If I invoke the same script from Ant I get an Ivy NoClassDefFound error. I think it may berelated to having Ivy in Antlib. Is there another way to parse Html without customizing the slurper via #Grab?
This:
#Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2' )
doc = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser()).parse(confluenceWebPageInputStream)
Works just fine from the command line but when I run it from an Ant build target:
<target name="update-wiki-chart">
<echo message="Will update chart for version ${version}"/>
<java dir="${basedir}" classname="groovy.lang.GroovyShell">
<arg value="ParseWikiPage.groovy"/>
<classpath refid="groovylib"/>
</java>
</target>
where groovyLib is a path ref pointing to the Groovy-1.8.6 jar downloaded from our internal Nexus repo, I get the NoClassDefFound error. I'm thinking this is probably due to having Ivy installed in Antlib causing the class loader to find it in two places. I just thought of something while writing this post. I can probably run java in forked mode or do something to cause it to not see/share Ant's classpath.It's been a few years since I've wrestled w/ Ant and class loader issues. My project is a little delinquent due to the bug and I'm looking for a quick/easy fix.
I just tried running my groovy on the cmd line via the "java" cmd and loading groovy-all jar in the class path and I realized that I get the NoClassDefFound error there as well. It has nothing to do with collisions with Ivy under Antlib. Rather, I am missing Ivy altogether. I had assumed it was included in Groovy-all.jar. I just need a clever way of passing Ivy from AntLib into my java task to get this all up and running.
Sounds like you're missing one or more jars from the classpath. I'd suggest digging around the classpathref you've labelled "groovylib".
A less error prone way to launch groovy from within ANT using the groovy ANT task.
Here's a similar example to your use-case:
Parse HTML using with an Ant Script
My example uses ivy directly to manage all build dependencies. The Grab annotations are still supported but obviously these would only manage the dependencies of the groovy script.

How to get all dependency jars from build.xml

I have several very big ant build.xml files.
Someone know tool or program for get all dependency jar files from build.xml ?
Please check How To Check Dependencies Between Jar Files?.
http://www.jboss.org/tattletale or jdepend could help in getting dependencies between jars.
If you want to extract dependencies from a given build.xml , one option is to write XML parser and process ant file. Not an easy task. Another option will be write a listener for each build task. If the task is javac then query its classpath.

compile jdk via ant

I want to compile jdk files in order to include debug infromation.
I'd like to use ant, because it's included in my NetBeans environement, so i've done the following:
unzipped /src.zip in a tmp directory
created a very simple build.xml file (one default target, one taks) in my tmp directory:
<?xml version="1.0" encoding="UTF-8"?>
<project name="CompileJDK" default="default" basedir=".">
<target name="default">
<javac srcdir="."
destdir="jdkwd"
debug="on"
/>
</target>
</project>
created a jdkwd directory
launched ant without parameters (just >log.txt)
This leads to 100 compilation errors such as:
[javac] C:\jdkdebug\java\awt\Window.java:196: cannot find symbol
[javac] symbol : class IdentityArrayList
[javac] location: class java.awt.Window
[javac] private static final IdentityArrayList<Window> allWindows = new IdentityArrayList<Window>();
I have just one JDK installed on my machine, so i don't know why it does not resolve all this references.
UPDATE:
The majority of these unresolved references belongs to the package:
sun.awt.util
The question now is corrected to: where are the missing jdk files?
Building the JDK itself is a complex process and is not achievable by a simple javac call wrapped inside an ant project.
You should look at the OpenJDK Build README to get instructions on how to build for your platform.
http://www.oracle.com/technetwork/java/faq-141681.html
A14. Where can I get the Java programming language source code?
Java Software has two separate bundles of source code that you can obtain at no charge:
The Java 2 SDK, Standard Edition itself contains a file called src.zip that contains the source code for the public classes in the java package. Because this does not contain sun.* classes, you cannot do a complete build of the Java technology from these source files. These source files are for your information, to supplement the documentation, so you can see how Java technology works.
The full source code release is available from us by going to the Community Source Code Licensing web site..
The community source code link is incorrect: it's now http://download.java.net/openjdk/jdk7/
Try adding a classpath to your javac call.
<classpath path="/PATH/to/missing_class/" />
Also, try running ant with the -d and -v options. It's a lot of output but will show you where its searching for classes.
According to this post (from 2007), you should include rt.jar and tools.jar on your classpath to compile the JRE sources.
However, I tried that, and it doesn't work for me (100 errors).
There are more elaborate and older (2004) instructions in christhielen's post in the Java bug requesting debug symbols.
If you use Gentoo compiling OpenJDK would be as simple as running emerge dev-java/icedtea.
There is a debug use flag that would switch off all optimizations, I haven't tried it myself but chances are that this is what you want. If it's not - then it should not be a big deal to change build scripts, but would require you to learn a little bit of portage.

ANT is failing to find org.dbunit.ant.DbUnitTask

So I'm writing a ant build file to run some tests, and I use the following line without any issue for MXUnit:
<taskdef name="mxunittask" classname="org.mxunit.ant.MXUnitAntTask" classpath="../mxunit/ant/lib/mxunit-ant.jar"/>
But when I download dbunit-2.4.8.jar from http://sourceforge.net/projects/dbunit/files/ and put it in the same directory as mxunit-ant.jar, then added the following line to the ANT build file:
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpath="../mxunit/ant/lib/dbunit-2.4.8.jar"/>
For which I see a warning in Eclipse that says:
taskdef A class needed by class
org.dbunit.ant.DbUnitTask cannot be
found: org/slf4j/LoggerFactory
When I extract the jar file it does have the org.dbunit.ant.DbUnitTask class.. so I'm very confused as to what ANT is complaining about.. any idea what the issue is?
It looks like you need to make sure all the dependencies of DbUnit are satisfied, as well as installing the dbunit-2.4.8.jar.
The specific error you quote with class org/slf4j/LoggerFactory not being found suggests that you don't have the SLF4J classes needed. (Probably the latest version of slf4j-api.jar is needed there.)

Problem with dependencies using Ant from the shell

I'm having problems building my project, using an Ant script, from the command prompt using Ant itself. It can't find a certain import for a particular Java file in my project (which has nearly 5,000 source files as it is). The import is included in a .jar package whose location I have set in the Ant file itself. (As a pathelement, along with other needed JARs that either are fine, or haven't tried to been used when the crash occurs). The crash happens with javac, with the simple message of "import etc.ect.* cannot be found at line etc" Oddly enough, I can build the project just fine from the Ant file using an IDE like Eclipse. Any ideas what could be wrong? Thanks!
Wow, the solution was completely unrelated. It was a dumb fault in the java code where the class was trying to import .* from a directory that only had folders in it. For some reason, Eclipse didn't seem to mind, but javac did!
Eclipse's root classloader contains a lot of classes, when you run ant from console there's much less.
Just tell javac task to use the required .jar, and you'll be fine.
You should post the stack trace, does it say "import required by ..."? ( I forget the exact text). Likely there's a jar that's available in your eclipse environment that is not included in your ant script. Look in the stack trace for the missing class to identify the jar that's not being included in your build.

Resources