Regarding ant: how i can give few aruments to main function? - java

Here is my "run" in my ant build file:
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
I want to run it like this:
ant run -Darg0=First.txt -Darg1=Second.txt -Darg2=Third.txt -Darg3=Fourth.txt -Darg4=Fifth.txt
What changes should i make in my "run"?
Many thanks for help!

Here's the ant task to run any program (including, but not limited to Java programs):
<target name="run">
<exec executable="name-of-executable-file">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</exec>
</target>
Here's the task to run a Java program from a .jar file:
<target name="run-java">
<java executable="path for jar">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</java>
</target>
You can invoke either from the command line like this:
ant -Darg0=Hello -Darg1=World run
Updated task
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
<arg value="${arg0}" />
<arg value="${arg1}" />
</java>
</target>

Check out the <java> task documentation
Use nested <arg> and <jvmarg> elements to specify arguments for the
Java class and the forked VM respectively.
and the <arg> subtask

Related

Compile vaadin widgetset from command line

In eclipse there is a plugin for vaadin.
When mark *.widgetset file in your project and click on on Compile vaading widgetset it compiles widgetset under WebContent\VAADIN\widgetsets. So my question is how to do it from command line without eclipse?
Note that: I've searched, but there are example of maven. This is old project and there is not maven configuration in it
You did not say which Vaadin version you are using but compiler class at least in Vaadin7 seems to be this:
https://vaadin.com/api/framework/7.6.8/com/vaadin/tools/WidgetsetCompiler.html
Not sure of commandline but I have this kind of ANT script for Vaadin7, maybe it will help a bit:
<target name="compile-widgetset" depends="init,resolve">
<delete includeEmptyDirs="true">
<fileset dir="${basedir}/WebContent/VAADIN/gwt-unitCache/" includes="**/*" defaultexcludes="no"/>
</delete>
<java classname="com.vaadin.tools.WidgetsetCompiler" failonerror="yes" fork="yes" maxmemory="600m">
<jvmarg value="-Xms512M"/>
<jvmarg value="-Xmx1024M"/>
<jvmarg value="-Xss8M"/>
<jvmarg value="-Djava.awt.headless=true" />
<arg value="-war"/>
<arg value="WebContent/VAADIN/widgetsets"/>
<arg value="${widgetset}"/>
<arg value="-logLevel"/>
<arg value="DEBUG"/>
<arg value="-style"/>
<arg value="OBF"/>
<classpath>
<pathelement path="${module.src.dir}"/>
<pathelement path="${module.build.dir}/WebContent/WEB-INF/classes" />
<pathelement path="${module.build.dir}/WebContent/WEB-INF/lib" />
<path refid="widgetset.path"/>
</classpath>
</java>
</target>
Compile instructions: https://vaadin.com/docs/v7/framework/clientside/clientside-compiling.html
I figured out using this url
https://github.com/canthony/simple-vaadin-7-compile-widgetset-ivy
I only added manifest tag to include dependecies in the META-INF/MANIFEST.MF file
<war destfile="${artifacts}/${warfilename}"
basedir="${workdir}"
webxml="${webroot}/WEB-INF/web.xml"
>
<manifest>
<attribute name="Dependencies" value="org.jboss.xnio, org.hibernate"/>
</manifest>
<lib dir="${webroot}/WEB-INF/lib" erroronmissingdir="no">
<include name="*.jar"/>
</lib>
<lib dir="${libraries}" erroronmissingdir="no">
<include name="*.jar"/>
</lib>
</war>

ant build file terminating tag error

I am trying to run an ant build file for a java jar. For some reason, whenever I try to run ant run, it comes back and says, "Element of type target must be terminated by the matching </target> tag" This seems strange because I see that I do have a terminating tag for run. Would there be something else that could be causing this? Any help would be greatly appreciated!
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
<target name="view">
<exec executable="less">
<arg value="PlayTour.java" />
<arg value="KnightsTour.java" />
</exec>
</target>
<target name= "doc">
<mkdir dir="build/docs"/>
<javadoc sourcefiles="KnightsTour.java, PlayTour.java" destdir="build/docs"/>
</target>
</project>
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
You are closing the Java tag in the block above twice, first using /> in-line then writing </java>, which causes your error. This is what it looks like fixed:
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar" >
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
Whenever you suspect an XML validation error, you should use a free online XML validation utility to at least confirm that the structure of your file is correct. This is what I used to quickly locate your error: http://www.xmlvalidation.com/index.php?id=1&L=0

Ant command line arguments

Program works fine when run with eclipse run configurations, but when run with ant, it is unable to parse int from args[0], which I do not understand. Full code is available here https://gist.github.com/4108950/e984a581d5e9de889eaf0c8faf0e57752e825a97
I believe it has something to do with ant,
target name="run" description="run the project">
java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6 in.txt"/>
/java>
/target>
the arg value will be changed via the -D flag, as in ant -Dargs="6 testData1.txt" run.
Any help would be much appreciated, it is very frustrating.
You need to supply the arguments as two different arg values:
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6" />
<arg value="in.txt" />
</java>
</target>
You can also use the line attribute; From the ANT docs:
<arg value="-l -a"/>
is a single command-line argument containing a space character, not separate commands "-> l" and "-a".
<arg line="-l -a"/>
This is a command line with two separate arguments, "-l" and "-a".
Expanding epoch 's answer.
java task supports sysproperty and jvmarg.
For example (from ant java task page)
<java classname="test.Main"
fork="yes" >
<sysproperty key="DEBUG" value="true"/>
<arg value="-h"/>
<jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/> </java>
So you could construct the args from the command line passed to ant.
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<sysproperty key="testarg" value="${testarg}"
<arg value="${arg1}" />
<arg value="${arg2}" />
</java>
</target>
Now if you call ant with ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt, then testarg will be available via property. Others will become normal arguments to the java program.

Groovy unable to resolve java class

i got a problem by executing groovy from an ant file.
In Eclipse with a launcher, everything works fine but wehn i run the ant file i got the following output:
Main.groovy: 71: unable to resolve class InitializeDatabase
[groovyc] # line 71, column 40. [groovyc] java.lang.Object
javaClassInstance = new InitializeDatabase()
[groovyc]
[groovyc] 1 error
InitializeDatabase is a java class in the same package..
public class InitializeDatabase {
public void test() {
System.out.println("Hello Groovy");
}
}
I guess the problem is located at the ant file:
<project name="tp" basedir="." default="dbsetup">
<target name="dbsetup">
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar" />
</classpath>
</taskdef>
<delete dir="bin" />
<mkdir dir="bin" />
<groovyc srcdir="src" destdir="bin" />
<java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar"/>
<pathelement location="bin"/>
</classpath>
<arg line="build/scripts/src/build/Main.groovy" />
</java>
</target>
</project>
Can someone help me please?
You need to include the javac task inside your groovyc one. Change:
<groovyc srcdir="src" destdir="bin" />
to
<groovyc srcdir="src" destdir="build">
<javac/>
</groovyc>
And it should work fine. As it says here:
Joint compilation means that the Groovy compilation will parse the
Groovy source files, create stubs for all of them, invoke the Java
compiler to compile the stubs along with Java sources, and then
continue compilation in the normal Groovy compiler way. This allows
mixing of Java and Groovy files without constraint.
...
The right way of working is, of course, to use a nested tag and all
the attributes and further nested tags as required.
Here is the final file which works great.
Thanks to tim_yates!
<target name="dbsetup">
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar" />
</classpath>
</taskdef>
<delete dir="bin" />
<mkdir dir="bin" />
<groovyc srcdir="src" destdir="bin">
<javac source="1.6" target="1.6" debug="on" />
</groovyc>
<java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar"/>
<pathelement location="bin"/>
</classpath>
<arg line="build/scripts/src/build/access/AccessDbSetup.groovy" />
</java>
</target>

Ant Freezes with no warning on exception

I have been experiencing a weird problem that I can't find a solution to. When I am running a project through ant and there's an uncaught exception, ant simply freezes with absolutely no error. Here is my build file. This is a multi threaded environment, also
<fileset id="masterlibs" dir="${lib.dir}">
<patternset>
<include name="*.jar"/>
</patternset>
</fileset>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
</javac>
</target>
<target name="run" depends="compile">
<java classname="stockData.AlertHandler" fork="true">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="false"/>
<arg line="27_4"/>
<arg line="false"/>
</java>
</target>
Ctrl+Break will show you a dump of threads and indicate what each is doing (plus any deadlocks). I'm guessing it's your spawned AlertHandler process and it may be easier to debug this outside the Ant environment.
If Ctrl+Break doesn't work (from the article)
On UNIX platforms you can send a
signal to a program by using the kill
command. This is the quit signal,
which is handled by the JVM. For
example, on Solaris you can use the
command kill -QUIT process_id, where
process_id is the process number of
your Java program.
Ant can sometimes freeze when it runs a java class that asks for interactive input. Is it possible that your AlertData class asks for input?

Categories