org/apache/commons/codec/DecoderException when integrating Ant-Contrib into Eclipse - eclipse

I've tried to integrate Ant-Contrib 1.0b3 into Eclipse by adding the ant-contrib-1.0b3.jar and the appendant jar-files from the libs-directory to the "Global Entries"-section of the Ant-Runtime-preferences page in Eclipse.
For tasks that don't need the libs, such as propertyregex, this works fine.
But when I try to use the postMethod task I get the following error:
java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
Any suggestions on what to do to resolve this?

It looks like your rig doesn't include the Apache Commons Codec which must be a dependency of ant-contrib.
According to the dependencies here you might also need commons-logging, but you've probably already got that.

It's much cleaner to specify classpath when you define 3rd-party tasks in your build file.
Provide either individual jars or add a <fileset> of dependent jars to classpath, e.g.
<taskdef
resource="net/sf/antcontrib/antlib.xml"
uri="http://ant-contrib.sourceforge.net"
>
<classpath>
<pathelement location="${ant-contrib.jar}"/>
<fileset dir="${ant-contrib-dependency.lib}">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
You can define default ant-contrib.jar and ant-contrib-dependency.lib properties in this build file, and provide project specific overrides in ant build tool VM arguments. This means you don't have to copy these files into various directories on your system.

Related

Unable to deploy JavaFX applicaiton with external libraries in a JAR

I'm developing a JavaFX application in Eclipse. The app has external libraries, like log4j and others and runs perfectly from Eclipse. I've tried deploying it from Eclipse as a Runnable jar through the Eclipse built-in feature, and it runs well on some computers, while on others it would give me an obscure Undefined Link error. After digging around it seems JavaFX needs to be specially packaged either through an ANT script or through the javafxpackager application.
For the life of me I can't seem to deploy a runnable jar through either of those methods. Trying either of those methods, I get an Exception. Things I've tried:
Build the JavaFX application through an ANT script, below is a snippet of the relevant parts of the build script
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${javafx.sdk.path}/jre/lib" includes="jfxrt.jar"/>
</path>
<target name="compile" depends="clean">
<echo>Compiling the source</echo>
<mkdir dir="${classes.dir}"/>
<!-- Copy over the misc files into the classes dir -->
<copy todir="${classes.dir}/bundles">
<fileset dir="${src.dir}/bundles"/>
</copy>
<copy todir="${classes.dir}/css">
<fileset dir="${src.dir}/css"/>
</copy>
<copy todir="${classes.dir}/img">
<fileset dir="${src.dir}/img"/>
</copy>
<copy todir="${classes.dir}/views">
<fileset dir="${src.dir}/views"/>
</copy>
<copy todir="${classes.dir}/bundles">
<fileset dir="${src.dir}/bundles"/>
</copy>
<copy file="${src.dir}/log4j2.xml" todir="${classes.dir}"/>
<javac target="1.7" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" debug="on">
</javac>
</target>
<target name="jar" depends="compile">
<echo>Creating the main jar file</echo>
<mkdir dir="${distro.dir}" />
<fx:jar destfile="${distro.dir}/main.jar" verbose="true">
<fx:platform javafx="2.1+" j2se="7.0"/>
<fx:application mainClass="${main.class}"/>
<!-- What to include into result jar file?
Everything in the build tree-->
<fileset dir="${classes.dir}"/>
<!-- Define what auxilary resources are needed
These files will go into the manifest file,
where the classpath is defined -->
<fx:resources>
<fx:fileset dir="${distro.dir}" includes="main.jar"/>
<fx:fileset dir="." includes="${lib.dir}/**" type="jar"/>
<fx:fileset dir="." includes="."/>
</fx:resources>
<!-- Make some updates to the Manifest file -->
<manifest>
<attribute name="Implementation-Vendor" value="${app.vendor}"/>
<attribute name="Implementation-Title" value="${app.name}"/>
<attribute name="Class-Path" value="${lib.dir}"/>
<attribute name="Implementation-Version" value="1.0"/>
</manifest>
</fx:jar>
</target>
Compiling/generating the jar through the ant script, I get a window pop up for half a second and "Exception in Application start method" in the console, with no stack trace. I've tried runnning java with the -XX:-OmitStackTraceInFastThrow flag, but it still won't give me a stack trace.
Running through javafxpackager gives the following error:
RenderJob.run: internal exception
java.lang.UnsatisfiedLinkError: com.sun.prism.d3d.D3DContext.nSetBlendEnabled(JZ
Z)I
at com.sun.prism.d3d.D3DContext.nSetBlendEnabled(Native Method)
at com.sun.prism.d3d.D3DContext.initState(D3DContext.java:84)
at com.sun.prism.d3d.D3DResourceFactory.(D3DResourceFactory.java:5
7)
at com.sun.prism.d3d.D3DPipeline.createResourceFactory(D3DPipeline.java:
147)
at com.sun.prism.d3d.D3DPipeline.getD3DResourceFactory(D3DPipeline.java:
153)
at com.sun.prism.d3d.D3DPipeline.findDefaultResourceFactory(D3DPipeline.
java:179)
at com.sun.prism.d3d.D3DPipeline.getDefaultResourceFactory(D3DPipeline.j
ava:201)
at com.sun.prism.GraphicsPipeline.getDefaultResourceFactory(GraphicsPipe
line.java:97)
at com.sun.javafx.tk.quantum.QuantumRenderer$3.run(QuantumRenderer.java:
143)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:47
1)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at com.sun.prism.render.RenderJob.run(RenderJob.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(Quantu
mRenderer.java:98)
at java.lang.Thread.run(Thread.java:724)
You can deploy your application using javafxpackager. If you deploy a self contained application, the Java Runtime Environment will be bundled with your application and it will run on any machine whether the right version of Java is installed or not.
(You should have javafxpackager in your jdk directory under the bin folder.)
Look for your compiled files (.class) in your eclipse workspace folder. They should be in the bin directory.
Create a createjar folder and inside it create a classes and a out folder. Copy all of your .class files in the classes folder. Also include in the classes folder all jar needed by your application.
Now on the command line go inside the createjar directory and run this command :
"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -createjar -appclass package.MainClass -srcdir classes -outdir out -outfile NameOfYourJar -classpath "" -v
This should create a runnable jar in the out directory. You will need it for the next step.
Now create a deploy folder alongside the createjar folder.
Inside the deploy folder make to other directories dist and packages.
In the dist folder copy your freshly created jar from above plus all the dependencies/ressources it will need.
Go back on the command line (if you ever left it) and go in the deploy directory. Run the following command from there :
"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -deploy -native -outdir packages -outfile NameOfYourApp -srcdir dist -srcfiles NameOfYourJar.jar -appclass package.MainClass -name "Name of You Application" -title "Title of your application"
Once it is done it should have created all you need to deploy your app including native .exe file to run on Windows platform. This is the self contained application ! To be able to run it you need to go into bundles\NameOfYourApp\app and paste here all dependencies/ressource your app needs.
At last, double click on YourApp.exe and it should run even on machines without Java installed.
With ant files you have two main options: <fx:jar> and <fx:deploy>. The former just produces a jar that allows you to refer to your dependencies if they are in the same file as the jar. You can unpack the jar and look at the MANIFEST file produced to see how they are added to the javafx classpath.
<fx:deploy> allows you to pull in dependencies, but requires that you build a platform-specific installer. It can't be done using a jar. Apparently NetBeans allows you to create a stand-alone jar but I have not tested with that.
you do not package javafxrt.jar with your app
e(fx)clipse would have produced the ant-script for your if you use it

Getting an error "Could not load definitions from resource net/sf/antcontrib/antcontrib.properties. It could not be found."

I am getting an error Could not load definitions from resource net/sf/antcontrib/antcontrib.properties. It could not be found. when I am trying to ant build on eclipse. So I downloaded ant-contrib-0.6.jar and kept it in my /lib location of apache ant, but it still does not resolve my issue. I have also tried by specifying the /lib location in my CLASSPATH system variable. How can I get around this error?
You can provide full path to the ant-contrib JAR explicitly using "classpath" element:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${path-to-ant-contrib}/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
EDIT: Link contributed by Djacomo:
http://ant-contrib.sourceforge.net/
One important thing missing from this StackOverflow page is that setting the correct ANT_HOME env var is absolutely vital and important, without this setting ant keeps telling the same error, regardless of where you copy the ant-contrib-1.0b3.jar on your file systems. This missing thing has costed me a few hours. =)
However I receive this error without eclipse, in the pure ant.
I fixed that this way:
Add the JAR to the Ant runtime classpath entries.
Window>Preferences>Ant>Runtime>Classpath
Add the JAR to either Ant Home Entries or Global Entries.
It would appear that you haven't installed the ant contrib jar into the correct lib directory. This can be difficult to do if you have several installations of ANT.
My suggestion is to install your ANT plugins into the "$HOME/.ant/lib" directory. You could go one step further and automate the process as follows:
<project name="ant-contrib-tasks" default="all">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ant-contrib.jar" src="http://search.maven.org/remotecontent?filepath=ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
</target>
<target name="all">
<for param="file">
<fileset dir="." includes="*.txt"/>
<sequential>
<echo message="Found file #{file}"/>
</sequential>
</for>
</target>
</project>
Use the below mentioned code in your build xml:
<path id="ant.classpath">
<pathelement location="${ant.jarPath}/ant.jar" />
<pathelement location="${ant.jarPath}/ant-contrib-0.3.jar" />
</path>
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath refid="ant.classpath" />
</taskdef>
And in your build property file:
ant.jarPath=D:/antjars
And place ant.jar and ant-contrib-0.3.jar in directory:D:/antjars
Check you have read permissions for the ant-contrib jar file.
In our case after copying the file with another user it did not, giving the same error message.

Modify project build path with Apache Ant in Eclipse

I have an ant script that does the tipically: clean, compile, jar, javadocs, etc. I also have 3 projects in Eclipse: a library and 2 projects to test it.
In the build path of this 2 test projects I've defined as external jar the library jar located in the library project.
The library jar has its version in the jar name, i.e. library-0.1.jar. In the ant script I have a property with the version of the library:
<property name="project_version" value="0.1"/>
So to change the version I modify this property and run the script again. As you may deduce this generates a dependency error in the 2 other projects because they will still be pointing to an old file library-0.1.jar.
How can I change automatically the build path of that 2 other projects in Eclipse? Apache ant can do this with a specific tag?
Thanks.
Refer to the version with a variable in all your build files, e.g.
<include name="my-${version}.jar"/>
Now when you execute your builds, you can execute with explict version to match what you require, e.g.
ant -Dversion=1.3
Alternatively, you could load the same properties file in each of your build scripts to load the version property
<property file="version.properties">
Note that if you go with the latter you should remove the property declaration (from you post above) which sets the value explicitly. Either that or load the properties file first.
....
Use sed, e.g. (not tested):
version=2
sed s/value="\d+"/value="$version"/g build.xml
EDIT: Using the method below you will be able to compile using Ant, but eclipse will show you a dependency error in the project explorer because you don't have defined any external jar in the build path panel. To solve this you have to edit the .classpath file that you will see in the project root and add the following line:
<classpathentry kind="lib" path="../Library/bin"/>
Where Library is the folder for Library project and bin the folder for classes.
SOLVED:
I have to write an ant script for the 2 other projects and set the classpath with the script, not with eclipse IDE:
<path id="build-classpath">
<fileset dir="${dist}">
<include name="${project_name}-${project_version}.jar"/>
</fileset>
</path>
${dist} is the folder where is the jar library it's something like: "../Library/dist", where Library is the name of the project.
${project_name} and ${project_version} are loaded from a version.properties file that, again, is stored in "../Library":
<property file="..Library/version.properties"/>
The file version.properties just contains:
project_name=LibraryName
project_version=0.1
Then, to add the classpath when compiling...
<target name="compile" depends="clean, makedir">
<javac srcdir="${src}" destdir="${bin}">
<classpath refid="build-classpath"/>
</javac>
</target>
The refid value is the path id defined previously.

Using Guava with GWT

Could someone tell me what I need to do to enable Guava support in GWT.
I have downloaded Guava R07 and in there there are the following two files:
guava-r07.jar
guava-r07-gwt.jar
I have a few questions regarding this:
Where do these files go? I am guessing that the standard Jar is made available to my IDE for coding, and that both are made available to the GWT compiler for building the JavaScript?
Do I need to add all the .gwt.xml files from the -gwt.jar into my project's main gwt.xml file or only the portions I need?
There are other Jars on the trunk of the Guava&GWT project (ie not in the download, such as one for jsr305) which I think I may need, but I'm not sure.
Sorry, normally I don't have trouble with this kind of thing, but I can't quite work out what goes where.
FYI I'm using GWT 1.6 for the time being, but am hoping to move to 2 soon. If Guava isn't compatible with 1.6 that is not a problem.
Update
I have the following files in a folder called gwtlib:
guava-r07-gwt.jar
guava-r07.jar
jsr305-1.3.9.jar
And my Ant script does the following:
<path id="project.class.path">
<fileset dir="gwtlibs" includes="guava-r07.jar"/>
<fileset dir="gwtlibs" includes="guava-r07-gwt.jar"/>
<fileset dir="gwtlibs" includes="jsr305-1.3.9.jar"/>
<pathelement location="${gwt.sdk}/gwt-user.jar"/>
<fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>
</path>
<target name="gwtc">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="app"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-localWorkers"/>
<arg value="2"/>
<arg value="-war"/>
<arg value="gwt-public"/>
<arg value="Main"/>
</java>
</target>
Running the above command, I get the following errors:
Errors in 'jar:file:///project/gwtlibs/guava-r07-gwt.jar!/com/google/common/collect/Constraints.java'
Line 254: The method subList(int, int) is undefined for the type List
Without the jsr jar on the classpath, I get the following errors:
The import javax.annotation cannot be resolved
Nullable cannot be resolved to a type
Thanks
Rich
Add these jars to your classpath. If you're using IDE, add them to your Build Path by right-clicking "Referenced Libraries" in your Package Exporer, select "Configure Build Path" and add them as external JARs.
You only need to inherit the modules you plan on using in your .gwt.xml file. For example, if you only use the common.collect package, just add <inherits name="com.google.common.collect.Collect" />
You probably don't need jsr305.jar, but if you do, just add it the same way as you added the other jars.
Guava should work just fine with GWT 1.6, if it doesn't then it's probably a bug.
Guava isn't compatible with GWT 1.6. List.subList, in particular, is added to GWT in GWT 2. The earliest version that we fully supported is GWT 2.0.4

scaladoc Ant task writing for multiple source directories?

I wish to compile scaladoc and javadoc in a project with both scala and java in it. The project has more than one source directory (each of which may have scala or javadoc). I tried:
<scaladoc destdir="doc">
<classpath refid="compile.classpath"/>
<src>
<fileset dir="src"/>
<fileset dir="tweetstream4j/src"/>
</src>
<include name="src/**/*.scala"/>
</scaladoc>
and several variants thereof. For the above variant I get:
BUILD FAILED
/blah/build.xml:119: /blah/src/com/foo/Blah.java is not a directory.
where "Blah.java" is a file in my source tree.
I've looked at the scaladoc ant task doc and the scaladoc man page. I'm using scala2.7.7final (because that's what apt-get puts on my Ubuntu system).
Suggestions of how to get scaladoc and javadoc out of "src" and "tweetstream4j/src"?
I had the same question and came across this. Those <src> elements don't seem well documented but I was able to put together something that works from various examples I found on the web:
<scaladoc destdir="doc">
<classpath refid="compile.classpath"/>
<src path="src"/>
<src path="tweetstream4j/src"/>
<include name="**/*.java"/>
<include name="**/*.scala"/>
</scaladoc>
That will look in both source directories for all Java and Scala code and build one set of documentation for it all.