Wednesday, 16 December 2015

Build Java Code with Visual Studio Team Services Using Ant

Visual Studio Team Services (VSTS) has evolved into cross platform capable, great Application Lifecycle Management and DevOps tool. Building java applications with VSTS hosted build services is possible now.
Let’s look at step by step how we can achieve this.
First we need a java code. The below is very simple java code just printing hello world.
1
2
3
4
5
6
7
8
9
/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldAppAnt {
    public static void main(String[] args) {
        System.out.println("Hello World - @ Forum!"); // Display the string.
    }
}


Check this code in and define a build in VSTS. TO the Build definition add an Ant build step.
Set the repository.
We need an Ant Build xml file to do an Ant build. Simple Ant build file looks like below.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<project name="HelloWorld" basedir="." default="main">

    <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="HelloWorldAppAnt"/>



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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </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"/>

</project>

More information on Ant build xml files can be found in below links. https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html https://ant.apache.org/manual/using.html
Add this file to source control.
Specify the path to Ant build file in the Ant build step.
Add a publish step to enable downloading contents of the build.
Now a build can be queued and it will build the java code in to runnable bytecode (.class). This build output can be downloaded as shown below.
The byte code can run with “java classname” in command prompt.
.jar file can be run with “java –jar jarfilename” in command prompt.

No comments:

Popular Posts