How to pass host and port parameter to ant, which will be used by the main Java function

I am having significant difficulties finding the correct use of arguments for ant purposes.

I have ant target, "our client", which must accept two (or zero) arguments, a host and a port to listen on the client. I thought it was easy to pass arguments to the shell by typing:

ant our-client -Dhost.arg=127.0.0.1 -Dport.arg=8081

This does not work. In my main function of the program, I have a logger that prints the host and port that were transferred. After that, I will explain what I have in the ant build file.

This is the goal that I wrote for the client.

<target name="our-client" depends="package" description="compiles, packages, and runs the student client">
    <java jar="${java.dist}/catan-client.jar" dir="${java.dist}" fork="yes">
        <arg value="${host.arg}"/>
        <arg value="${port.arg}"/>
        <sysproperty key="com.sun.management.jmxremote" value=""/>
        <assertions>
            <enable/>
        </assertions>
    </java>
</target>

The part that I can’t understand is how these “args” work. At the top of the ant build file, I defined these two properties as follows.

<property name="port.arg" value="8081"/>
<property name="host.arg" value="localhost"/>

, . , .

. , , localhost, 8081.

public static void main(final String[] args)
{
...
  if (args.length == 2)
  {
    String host = args[0];
    String port = args[1];
    LOGGER.info("host: " + host + ", port: " + port);
  }
...
}

:

INFO: host: localhost, port: 8081

, - ant. , .

.

+4
1

PowerShell. ant , , PowerShell. PowerShell, ( "." ) , PowerShell - (, ant). :

ant our-client "-Dhost.arg=127.0.0.1" "-Dport.arg=8081"
+3

Source: https://habr.com/ru/post/1612330/


All Articles