How to terminate <exec> task in ANT after starting server in Windows - ant

I'm trying to start a weblogic server using ant exec command.
After the operation triggers the exec process creates a child process i.e starts weblogic server. I wish to terminate the parent ant process after the server startup and keep alive the running Weblogic server.
I have tried using spawn="true" and task, but it does not help.
Also I am trying to call the ant buil.xml from eclipse.
I want to achieve that the task should start the server and then terminate the ant process and the calling java program should finish its execution from eclipse.
Following is my build.xml:
<target name="StartWeblogicServer" description="Starts the Weblogic Server" >
<exec dir="${DOMAIN_HOME}/bin" executable="cmd" failonerror="true" spawn="true">
<arg value="/c start startWeblogic.cmd" />
</exec>
</target>
The above code starts a weblogic server in a seperate command prompt. However the eclipse does not terminate the running program as the ant process is not terminate after the execution of task.
Please help.

Related

Calling Ant from Nant without batch

We have an automated build process that uses Nant scripts, and we just inherited a Java project that has an existing, working Ant script. I have searched all over and it seems most people use a batch file to call Ant scripts from Nant. This seems a little hacky to me. Is there a way to call Ant scripts directly from Nant in task format?
This similar question addresses it with a batch file but I want to do it without.
<!-- calling the Ant build Process-->
<target name="AntBuildProcess" description="Created to call the Ant build during this NAnt build process" >
<echo message="Starting the Ant Build Process..." />
<exec program="ant" commandline='-buildfile YourAntBuild.xml' failonerror="true"/>
</target>
Then during your Build process, you just call this target at the point you need it to be built. <call target="AntBuildProcess" />
Ant is a batch file. Take a look, and you'll see a file called ant.bat in the %ANT_HOME%\bin directory.
Ant is actually a Java program, so you could launch it from the java command by running the class org.apache.tools.ant.launch.Launcher which is basically what the ant.bat file is doing.
However, why reinvent the wheel? The ant.bat runs the Java command the right way, gives you options to change the way it's executed, and makes sure everything is setup correctly.
Addendum
I see, Nant, unlike Ant, will always call cmd.exe and use suffixes and %PATHEXEC% to figure out if something is a batch script or other type of script. Thus, if you want to run Ant using Ant as a batch script via <exec/> you would do this:
<exec executable="cmd.exe"
dir="${working.dir}">
<arg value="/c"/>
<arg value="ant.bat"/>
</exec>
However, in Nant you can simply do it this way:
<exec program="ant"
workingdir=${working.dir}"/>

about ant sshexec

how to specify command in sshexec task of ant to execute an ant task on a remote server machine. like for example:
<sshexec host="somehost"
username="dude"
keyfile="${user.home}/.ssh/id_dsa"
commandResource="to_run"/>
in above code what would be in the command attribute while trying to perform an ant task on a remote server machine, and also if the ant task is to deploy an application
please help me out on this one as the appache site is not of much help.

Can't specify Cobertura datafile location for server running under Ant?

I have an Ant build script that instruments some jar files, starts some servers using those jars files and then runs an integration test suite of junit tests against them.
I want to capture the cobertura.ser file from each server in a separate file.
The servers need to have their working directory set so they can pick up config files. It's a requirement of the system that the classpath must not be used to pick up these files.
Setting the net.sourceforge.cobertura.datafile system property allows the datafile to be set, and this works ok, until the "dir" property is set on the ant java task. Once dir is set, the server starts correctly, the test suite runs OK, but when the server shuts down no data file is written.
Here's a fragment of the build.xml:
<parallel>
<daemons>
<java fork="true" dir="src\main\resources\conf\my.server" classname="my.Server">
<sysproperty key="net.sourceforge.cobertura.datafile" file="target\cobertura.ser" />
<classpath>
...
</classpath>
<arg value="-server" />
</java>
...more servers...
...run junit tests...
</daemons>
</parallel>
The answer is to not run the servers as daemons. We were doing this so that the servers were automatically shut down when the junit task had completed, but in reality, the Cobertura instrumentation wasn't picking up the fact that the servers were shutting down and so never wrote out the various cobertura.ser files.
The solution was to remove the daemons task, and to add an explicit server shutdown mechanism which we could call from Ant once the tests were complete.

How to start/stop Tomcat service remotely using Ant

I'm following this link (http://raibledesigns.com/wiki/Wiki.jsp?page=TomcatAntTasks) to try and figure out how to start/stop tomcat service remotely and currently I'm getting an error saying none of the taskdef class can be found. I already have ant installed, what else am I missing? And is the link a good way to start/stop tomcat service?
You need to put the catalina-ant.jar in your ant's lib directory. It comes with Tomcat.
With ANT you don't need an external task. ;)
Simply use SSHExec task to start and stop (service or shell) and SCP task to copy WAR (or anything).
Quick, native and clean!
Did you change the path of the ant jar?
<taskdef file="tomcatTasks.properties">
<classpath>
<pathelement path="${tomcat.home}/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
on tomcat 6 there is no server folder.

Ant task to automate the start of my server and application

I want to write Ant task to automate the task of starting my server and then open Internet Explorer with the URL of my application.
Obviously I have to execute the startServer task first and then startApplication task.
But Ant is not coming out of startServer task even after starting the server to execute startApplication task.
Basically I want Ant to understand that startServer will not end and ANT has to come out of startServer task and runstartApplication task while startServer task is running in background.
My guess is that you have an exec task in startServer. Add spawn="true" to the exec. Ant will then execute the command in the background and continue without waiting for it to complete.
I agree with Aaron you can use exec to do this, you can also use waitfor to test your connection.
<exec executable="${jboss.startup.bat}" spawn="true"/>
<echo>Waiting to start</echo>
<waitfor maxwait="10" maxwaitunit="second" checkevery="5000">
<!-- try to detect when the server has started -->
<http url="${myurl}" />
</waitfor>
<echo>Started</echo>
You also need to be aware of the problems with exec'ing .bat files directly. Consult the manual page for the <exec> task for more information.

Resources