Question

I have implemented an echo server Client : connects to server, sends string data to server. Server : prints incoming data, sends back data to client

I need to run both server and client using ant in cmd.

How do I make the same build.xml file run both server and client. Can I set both targets in the same file or do I need to use different build.xml files.

Please find below, build.xml file that I am using, which runs the echo server. I need to run the EchoClient.java as well. How do I modify the file.

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>

<property name="main-class"  value="EchoServer"/>



<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false"/>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>

Was it helpful?

Solution

Use two targets in your build file. You can then choose which one to run when launching ANT:

ant run-server
and run-client

First target

<target name="run-server" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

Second target

<target name="run-client" depends="jar">
    <java classname="EchoClient" fork="true">
      <classpath>
        <pathelement location="${jar.dir}/${ant.project.name}.jar""/>
      </classpath>
    </java>
</target>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top