Jeysym
Jeysym

Reputation: 186

Ant - run .jar file without knowing its full name

I have ant build.xml that looks like this:

<project name="project_name" default="build" basedir=".">
<property name="src.dir" value="./src"/>
<property name="build.dir" value="./build"/>
<property name="lib.dir" value="./lib"/>
<property name="jar.name.prefix" value="myprogram-"/>
<property name="output_file.name" value="output"/>

<path id="lib.path">
    <fileset dir="${lib.dir}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<target name="init">
    <mkdir dir="${build.dir}"/>
</target>

<target name="build" depends="init">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
        <classpath refid="lib.path"/>
    </javac>
</target>

<target name="dist" depends="build">
    <tstamp/>
    <jar destfile="${jar.name.prefix}${DSTAMP}.jar" basedir="${build.dir}">
        <manifest>
            <attribute name="Main-Class" value="my.app.MainClass"/>
            <attribute name="Project" value="MyApp"/>
            <attribute name="BuildDate" value="${DSTAMP}"/>
        </manifest>
    </jar>
</target>

<target name="clean">
    <delete dir="${build.dir}"/>
    <delete file="${output_file.name}"/>
    <delete>
        <fileset dir=".">
            <include name="*.jar"/>
        </fileset>
    </delete>
</target>
</project>

And I would like to add the run target. But the dist target produces file in form of: myprogram-20161029.jar. So the name of the file that should be executed varies, depending on the time when the file was built. The run target should look like this:

<target name="run" depends="dist">
     <java jar="what to put here??" fork="true" output="output.file.name"/>
</target>

But from what i've read jar parameter of java target accepts only location of the .jar file, which I don't know. I only know that the file name satisfies pattern ./${jar.name.prefix}*.jar. Does anybody have an idea, how to get the file name of .jar file that has been built and pass it to the jar parameter of the java task?

Upvotes: 0

Views: 457

Answers (1)

rsutormin
rsutormin

Reputation: 1649

First of all since "run" target depends on "dist" then all properties defined in "dist" are accessible inside "run". So you can easily use same ${DSTAMP} (defined by "tstamp" target) like this:

<target name="run" depends="dist">
    <java jar="${jar.name.prefix}${DSTAMP}.jar" fork="true" output="output.file.name"/>
</target>

Second is I would recommend to define custom variable name instead of DSTAMP with something like:

<tstamp prefix="jardate"/>

And then usage of DSTAMP will be ${jardate.DSTAMP} instead of ${DSTAMP}.

Upvotes: 1

Related Questions