Find cause of ant failure - java

I have some ant files that one imports the other. Specifically build.xml imports a project_default.xml. When I try to do a build I get following error:
Buildfile: C:\myproject\build.xml [taskdef] Could not load
definitions from resource net/sf/antcontrib/antcontrib.properties. It
could not be found.
BUILD FAILED C:\myproject\build.xml:14: The following error occurred
while executing this line: C:\myproject\project_default.xml:17:
Problem: failed to create task or type if Cause: The name is
undefined. Action: Check the spelling. Action: Check that any
custom tasks/types have been declared. Action: Check that any
/ declarations have taken place.
Line 17 that is reported is the following:
<if>
<contains string="${env.PROJECT_SELECTION}" substring="env.PROJECT_SELECTION" />
I also added the following in the build.xml:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="D:\UserData\ant-contrib-0.6-bin\lib\ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
But got the same error. Any idea of what is the problem here?

with ant version > 1.6 you should use "net/sf/antcontrib/antlib.xml" instead of "net/sf/antcontrib/antcontrib.properties"
so change the taskdef to this:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="D:\UserData\ant-contrib-0.6-bin\lib\ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
cheers,

The first thing to check is that you have the ant-contrib jar path corrext. Run ant with -verbose to get more info about what it's doing and what tasks it's trying to load from where.
Where did you add the taskdef? If you're using the <if> task outside a target then you need the taskdef before it, also outside a target. If you're using it inside a target then the taskdef can be (textually) after the call as long as it is either outside a target or in a target that runs before the <if> in dependency order.

Related

Trouble Running simple JUnit Test through Ant on eclipse

I have the following JUnit test in eclipse:
package test;
import org.junit.Test;
public class SimpleJUnitTest
{
#Test
public void doTest() { System.out.println("Test did run"); }
}
And the following build.xml in the same folder:
<?xml version="1.0" encoding="UTF-8"?>
<project name="LoggerTest" default="JUnitTest" basedir=".">
<target name="JUnitTest">
<junit>
<classpath location="../../lib/junit.jar" />
<test name="test.SimpleJUnitTest" />
</junit>
<echo>boo</echo>
</target>
</project>
If I run the test class under "Run As..." and choose JUnit, it runs without error. If I run the build.xml under "Run As..." and choose Ant Build, I get the following output:
Buildfile: C:\Users\995868\workspace\JUnit1\tst\test\build.xml
JUnitTest:
[junit] Test test.SimpleJUnitTest FAILED
[echo] boo BUILD SUCCESSFUL Total time: 390 milliseconds
If I remove the classpath attribute under JUnit, I get a different error message about needing the junit jar on the classpath, so I think JUnit is getting invoked. I just don't understand what its error is here. I've tried putting static block code in the class to do a System.out.println() when the class is loaded, and it does not appear, so there seems to be something I'm doing wrong in the configuration.
Can someone please tell me what's wrong here?
EDIT:
directory structure:
JUnit1
--bin
--test
--SimpleJUnitTest
--lib
--junit.jar
--scripts
--build.xml
--src
--tst
--test
--SimpleJUnitTest.java
I also copied build.xml to tst and ran it from the command line from that directory, same result.
I've copied junit.jar to %ant_home%\lib with no effect, though when I took the pathelement line out of the classpath I got the message "The for must include junit.jar if not in Ant's own classpath". I'm not sure where "Ant's own classpath" is specified. The classpath block with the new error message is this:
<classpath>
<pathelement location="c:/users/995868/apache-ant-1.9.4/lib" />
<pathelement location="../bin" />
</classpath>
I'm not using hamcrest features anywhere, so I haven't looked it up and put it in. I was trying to make a simple example, and the documentation for junit under ant (at least) does not mention that hamcrest is necessary.
I suppose you are missing hamcrest-core.jar from your ant classpath.
EDIT: and you are also missing the test file class itself from your classpath. So you need to update your classpath in the following manner:
<classpath>
<pathelement location="bin"/> <!--here is your test class-->
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="lib/junit-4.12.jar"/>
</classpath>
Your class does not contain any testcases, which is an error. Your #Test annotation will be ignored, as you have not specified an #RunWith that will actually use it, so JUnit searches for methods named "test...".
So you have two choices:
a) Rename your method to "testSomething" or similar.
b) Add #RunWith(BlockJUnit4ClassRunner.class) above your class definition
Both ways will ensure that at least one test exists in your class, either via naming convention (a) or via annotion (b).

ant-contrib post task is not working

I have following task in my build script.
<target name="upload" depends="init">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<post to="http://testapp.com/api/builds.format" verbose="true" wantresponse="true" maxwait="0">
<prop name="file" value="./release/temp.ipa"/>
<prop name="notes" value="release notes"/>
</post>
</target>
I run this build script from my mac machine. It gives the following error.
Problem: failed to create task or type post
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
I already download the ant-contrib-0.3.jar and copied it to ANT_HOME/lib directory where ANT_HOME is /usr/share/ant.
When I search the web here they have asked to use ant-contrib-version.jar as well by building the jar by ourselves using the source. But I could not run the ant script in that source since it throwing some errors.
Can some one guide me to solve this issue.
ant-contrib-0.3.jar is a pretty ancient version of Ant-Contrib. The <post> task doesn't exist in it.
Instead, you'll find <post> in the latest version of Ant-Contrib: ant-contrib-1.0b3

MongoDB java driver: Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient

After I included the jar lib in ant build.xml, I still got the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient
My ant related configuration are as below:
<target name="compile" depends="clean">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeantruntime="false" debug="on">
<classpath id="classpath">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="mongo-2.10.1.jar"/>
</fileset>
</classpath>
</javac>
</target>
The "include" part stuff is working, because with out the include part or even I change the pathname a little bit, I will get symbol not found errors, which means currently the classpath is truly included into the file. But why it still does not work, anyone gets some idea?
And by the way, it is the first line of my java codes get the error:
MongoClient mongoClient = new MongoClient("localhost",27017);
Even not getting to the insert and querying operations.....
You've only shared your "compile" target here - if you've only added the mongo jar file to the compile target, when you actually run you're going to be missing the dependency.
Check that the mongo jar is also on the runtime classpath - this might be configured in the "java" ant target, depending upon your application.
You might want to directly check your mongo-java-driver-XXX.jar file. Older builds do not have the class com.mongodb.MongoClient. You can substitute Mongo instead.
Instead of
MongoClient client = new MongoClient();
Type this:
Mongo client = new Mongo();

How to add extraClasses option to Ant script when calling java2wsdl?

I'd like to pass extraClasses parameter when I generate java2wsdl. Here is my Ant task:
<target name="rews.all" depends="xews.aar">
<echo message="${axis2.classpath}" />
<delete file="${build.dir}/wsdl/XEWS.wsdl" />
<taskdef name="java2wsdl"
classname="org.apache.ws.java2wsdl.Java2WSDLTask"
classpathref="axis2.classpath">
</taskdef>
<java2wsdl className="com.dd.xews.XEWS"
outputLocation="${build.dir}/wsdl/"
targetNamespace="http://xews.dd.com/"
schemaTargetNamespace="http://xews.dd.com">
<classpath>
<pathelement path="${axis2.classpath}"/>
<pathelement location="${build.dir}/classes"/>
<pathelement location="${vendor.dir}/AWS/lib/aws-java-sdk-1.2.1.jar"/>
</classpath>
</java2wsdl>
<copy todir="${build.dir}/" file="${build.dir}/wsdl/XEWS.wsdl"/>
</target>
Tried everything but no luck.
Does anyone know the syntax? How do I add extraClasses here?
Test1 (failed)
This failed with error java2wsdl doesn't support the "extraClasses" attribute:
<java2wsdl className ="com.dd.xews.XEWS"
outputLocation ="${build.dir}/wsdl/"
targetNamespace ="http://xews.dd.com/"
schemaTargetNamespace ="http://xews.dd.com"
extraClasses ="com.dd.xews.XEWS.Emailer.java">
</java2wsdl>
How to find out which attributes java2wsdl Ant task does support?
My Axis2 version is 1.5.4.
Here's a link to the source code of Ant task: Java2WSDLTask.
#setExtraClasses accepts String parameter, and then tries to split it using comma delimiter. So try passing something like
<extraClasses>com.test.Class1,com.test.Class2</extraClasses>
EDIT
This will not work in older versions of Axis2 (to be more precise -- versions prior 1.6.0). It's because of 'extraClasses' attribute was specified as an array-type, which is obviously not supported as Ant task attribute. You can find all the details in this JIRA issue: AXIS2-4634: Ant task Java2WSDLTask does not allow the use of extraClasses
The easiest way to make it work is to upgrade Axis2 JARs to a newer 1.6.x version. If you are stuck with Axis2 version for some project-specific reasons (I don't see there should be any), you could take the source code of Java2WSDLTask from the newer version (see link to GrepCode above), and make a copy of this task in your project (you'll have to use different class name, or package), then use it as an Ant task just like you are using it currently. Except for it will be possible to use 'extraClasses' attribute.
Axis2 1.4 and up support the "-xc" attribute. Here's how I did it:
<java classname="org.apache.ws.java2wsdl.Java2WSDL" fork="true">
....
<arg value="-xc"/>
<arg value="com.example.mypackage.MyClass"/>
<arg value="-xc"/>
<arg value="com.example.anotherpackage.MyOtherClass"/>
....
</java>

Filtering sources before compilation in NetBeans & Ant

I need to filter java files before compilation, leaving the original sources unchanged and compiling from filtered ones (basically, I need to set build date and such).
I'm using NetBeans with its great Ant build-files.
So, one day I discovered the need to pre-process my source files before compilation, and ran into a big problem. No, I did not run to SO at once, I did some research, but failed. So, here comes my sad story...
I found the "filter" option of "copy" task, overrided macrodef "j2seproject3:javac" in build-impl.xml file and added filter in the middle of it. I got the desired result, yes, but now my tests are not working, since they use that macrodef too.
Next, I tired to overriding "-do-compile" target, copying&filtering files to directory build/temp-src, and passing an argument of new source directory to "j2seproject3:javac":
<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,
-compile-depend,-prepare-sources"
if="have.sources" name="-do-compile">
<j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
</copy>
</target>
And now Ant says to me, that macrodef in question does not exist!
The prefix "j2seproject3" for element "j2seproject3:javac" is not bound.
That's strange, since build-impl.xml contains that macrodef, and build-impl.xml is imported into main build file.
And, by the way, I cannot edit build-impl.xml directly, since NetBeans rewrites it on every other build.
So, my question is: how can I automatically filter sources before compiling in NetBeans, and do not break the build process?
Looking at the default build.xml, it contains a comment that reads (in part):
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
So, to inject some pre-compile processing, you would provide a definition for -pre-compile.
FWIW, the error you got is because the j2seprojectX prefix is defined on the project tag of build-impl.xml, and the code in build.xml is outside of that tag.
Since I found an answer, and because it seems that nobody knows the answer, I'll post my solution.
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Needed to add xmlns:j2seproject3 attribute, to be able to reference build-impl.xml macrodefs -->
<project name="Parrot" default="default" basedir="." xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
<import file="nbproject/build-impl.xml"/>
<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml, -compile-depend,-prepare-sources" if="have.sources" name="-do-compile">
<j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
</copy>
</target>
<!-- target to alter sources before compilation, you can add any preprocessing actions here -->
<target name="-filter-sources" description="Filters sources to temp-src, setting the build date">
<delete dir="build/temp-src" />
<mkdir dir="build/temp-src" />
<tstamp>
<format property="build.time" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<filter token="build-time" value="${build.time}" />
<copy todir="build/temp-src" filtering="true">
<fileset dir="src">
<filename name="**/*.java" />
</fileset>
</copy>
</target>
</project>

Categories