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.

Saturday, 5 December 2015

Apply Build Number to Assembly Version with New TFS Builds (Build vNext)

To assign the build number as the assembly version, PowerShell script available here can be used. Let’s see how to set this up for TFS builds step by step.
First, a small console application created below as the sample.01
Code segment written to output the assembly version.02 03
Setup a build in Visual Studio Team Services to build the application. Build number is set to represent version 4.1.3.X (the X is the build number for version 4.1.3). This can be set to match regex pattern "\d+\.\d+\.\d+\.\d+". 04
05.2
Set the repository path05
This builds successfully and the build output contains the .exe.06 07 08 09
Next download the “ApplyVersionToAssemblies.ps1” from here. Download link can be found in this page.10
Add the script to the Team Project and check in.11
Add new build step to execute PowerShell script.12
Move the added step as the first step.13
Set the script.14 15
16
When build script downloaded from github this error occurs due to incorrect environment variable. The script is developed to work with TFS 2013 XAML template builds. The scriptceript should be updated to “$Env:BUILD_SOURCESDIRECTORY”, “$Env:BUILD_BUILDNUMBER”, instead of “$Env:TF_BUILD_SOURCESDIRECTORY”, “$Env:TF_BUILD_BUILDNUMBER”. Remove the “-not $Env:TF_BUILD” condition.18
The above changes make the compatible to run with new TFS build. The assemblies now get updated with the build number as version number.19 20
21