Ant, MSBuild, Shell Scripts8 March 2010
What Is Ant?A build tool like make Open source from the Apache Jakarta project http://jakarta.apache.org/ant Implemented in Java Used to build many open source products
Why Use Ant?Develop big programs by automating all tasks of compiling code, running tests, packaging and etc.Ant is more portable Ant only requires a Java VMAnt targets are described in XMLDoes not have cryptic syntax.
How Does Ant Work? Ant commands (or tasks) are implemented by Java classesmany are built-inothers come in optional JAR filescustom tasks can be createdEach project using Ant will have a build filetypically called build.xml since Ant looks for this by defaultEach build file is composed of targetsthese correspond to common activities like compiling and running codeEach target is composed of tasksexecuted in sequence when the target is executed Ant targets can have dependencies for example, modified source files must be compiled	before the application can be run
How Does Ant Work? (Cont’d)Targets to be executedcan be specified on the command line when invoking Antif none are specified then the default target is executedexecution stops if an error is encountered so all requested targets may not be executed Each target is only executed once for examplethe “test” and “deploy” targets both depend on “compile”the “all” target depends on “test” and “deploy”but “compile” is only executed once when “all” is executed Some tasks are only executed when they need to befor example, files that have not changed since the last time they were compiled are not recompiled
<?xml version="1.0" ?><project name="testAnt" default="run" > <property name="src" location="src/test" /> <property name="output" location="build/classes" /> <property name="msg" value="First Ant Making Directories..." /><target name="init">	<echo> ${msg} </echo>	<mkdir dir="${output}" />	</target><target name="compile" depends="init">	<echo> Compiling... </echo>	<javac srcdir="${src}" destdir="${output}"/></target>Sample Build File (build.xml)Default target nameProperty valuesA target named by “init”Tasks which is executed in the sequence orderUsing property value “output”
Sample Build File (Cont’d)<target name="run" depends="init,compile" >	<java fork="true" classname="test.Main"> 	<classpath>	<pathelement location="${output}"/>	</classpath>	</java>	</target><target name="clean">	       <delete dir="${output}" /></target></project>Means that the init,compile targets must be executed before this target
MSBuildMicrosoft Build Engine 
What Is MSbuild?A Build tool like AntShips with Microsoft.NET FrameworkGeneral purpose build automation systemUses XML-based language Syntax similar to Ant
The Basic ElementsRoot container for Msbuild file (.csproj)<Project DefaultTargets="SampleTarget" 	xmlns="http://schemas.microsoft.com/developer/msbuild/2003">	</Project>Properties A property defines a value associated with a name<PropertyGroup>   		<OutputDirectory>Output\</OutputDirectory>			<AppName>test</AppName>		</PropertyGroup>Using properties<Target Name="CreateDirectories“>    	<MakeDir            						Directories="$(MSBuildProjectDirectory)\$(OutputDirectory)"/>		</Target>All other elementsUsing property value
The Basic Elements (cont’d)ItemsAn item is a named reference to a file or to many files.Contain associated metadata, such as the full path or filename. 		<ItemGroup>       	    <Compile Include="Program.cs" />		    <Compile Include=“Form.cs" />		</ItemGroup>Using ItemsReference the item collection in the example above with @(Compile)Accessing well known meta data of itemssyntax %(ItemMetadataName)Item collection named as Compile
The Basic Elements (cont’d)TargetsIs a container for related tasks that will be executed sequentially Equivalent to MethodsMust have a name		<Target Name="SampleTarget" DependsOnTargets="DepTarget">		   <Message Text="SampleTarget executed" />		</Target>		<Target Name=“DepTarget">		   <Message Text="DependentTarget executed " />		</Target>Means that the DepTarget must be executed before this target
The Basic Elements (cont’d)TasksEquivalent to a StatementSmallest unit of executionMust be declared within a Target Many standard ones included in MSBuildMakeDir,Copy, Message, Csc, Exec, etc.Custom tasks can be created<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”>	<Target Name="CreateDirectories">    	<MakeDir Directories=“\output\"/>	</Target></Project>Simple task which create directory called “output”
Shell ScriptsWindows scripting 
About Shell ScriptsAlso called batch programs or scriptsContains one or more commands and has a .bat or .cmd file name extensionfor, goto, call and if, enable you to do conditional processing of the commandsCommands are not case sensitive Executed by the command interpreter
Batch Parameters  Similar to Command line argumentsUsing Batch Parameters		%{argument_no}Using modifiers with batch parametersecho %~f0How to change the position of batch parameter?Using SHIFT /N  commandSHIFT /2	would shift %3 to %2 and so on, and leave %0 and %1 unaffected.%*  Reference to all the arguments, not including %0Expands %0 to a fully qualified path name.
More Information About CommandsCall

ANT

  • 1.
    Ant, MSBuild, ShellScripts8 March 2010
  • 2.
    What Is Ant?Abuild tool like make Open source from the Apache Jakarta project http://jakarta.apache.org/ant Implemented in Java Used to build many open source products
  • 3.
    Why Use Ant?Developbig programs by automating all tasks of compiling code, running tests, packaging and etc.Ant is more portable Ant only requires a Java VMAnt targets are described in XMLDoes not have cryptic syntax.
  • 4.
    How Does AntWork? Ant commands (or tasks) are implemented by Java classesmany are built-inothers come in optional JAR filescustom tasks can be createdEach project using Ant will have a build filetypically called build.xml since Ant looks for this by defaultEach build file is composed of targetsthese correspond to common activities like compiling and running codeEach target is composed of tasksexecuted in sequence when the target is executed Ant targets can have dependencies for example, modified source files must be compiled before the application can be run
  • 5.
    How Does AntWork? (Cont’d)Targets to be executedcan be specified on the command line when invoking Antif none are specified then the default target is executedexecution stops if an error is encountered so all requested targets may not be executed Each target is only executed once for examplethe “test” and “deploy” targets both depend on “compile”the “all” target depends on “test” and “deploy”but “compile” is only executed once when “all” is executed Some tasks are only executed when they need to befor example, files that have not changed since the last time they were compiled are not recompiled
  • 6.
    <?xml version="1.0" ?><projectname="testAnt" default="run" > <property name="src" location="src/test" /> <property name="output" location="build/classes" /> <property name="msg" value="First Ant Making Directories..." /><target name="init"> <echo> ${msg} </echo> <mkdir dir="${output}" /> </target><target name="compile" depends="init"> <echo> Compiling... </echo> <javac srcdir="${src}" destdir="${output}"/></target>Sample Build File (build.xml)Default target nameProperty valuesA target named by “init”Tasks which is executed in the sequence orderUsing property value “output”
  • 7.
    Sample Build File(Cont’d)<target name="run" depends="init,compile" > <java fork="true" classname="test.Main"> <classpath> <pathelement location="${output}"/> </classpath> </java> </target><target name="clean"> <delete dir="${output}" /></target></project>Means that the init,compile targets must be executed before this target
  • 8.
  • 9.
    What Is MSbuild?ABuild tool like AntShips with Microsoft.NET FrameworkGeneral purpose build automation systemUses XML-based language Syntax similar to Ant
  • 10.
    The Basic ElementsRootcontainer for Msbuild file (.csproj)<Project DefaultTargets="SampleTarget" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> </Project>Properties A property defines a value associated with a name<PropertyGroup> <OutputDirectory>Output\</OutputDirectory> <AppName>test</AppName> </PropertyGroup>Using properties<Target Name="CreateDirectories“> <MakeDir Directories="$(MSBuildProjectDirectory)\$(OutputDirectory)"/> </Target>All other elementsUsing property value
  • 11.
    The Basic Elements(cont’d)ItemsAn item is a named reference to a file or to many files.Contain associated metadata, such as the full path or filename. <ItemGroup> <Compile Include="Program.cs" /> <Compile Include=“Form.cs" /> </ItemGroup>Using ItemsReference the item collection in the example above with @(Compile)Accessing well known meta data of itemssyntax %(ItemMetadataName)Item collection named as Compile
  • 12.
    The Basic Elements(cont’d)TargetsIs a container for related tasks that will be executed sequentially Equivalent to MethodsMust have a name <Target Name="SampleTarget" DependsOnTargets="DepTarget"> <Message Text="SampleTarget executed" /> </Target> <Target Name=“DepTarget"> <Message Text="DependentTarget executed " /> </Target>Means that the DepTarget must be executed before this target
  • 13.
    The Basic Elements(cont’d)TasksEquivalent to a StatementSmallest unit of executionMust be declared within a Target Many standard ones included in MSBuildMakeDir,Copy, Message, Csc, Exec, etc.Custom tasks can be created<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”> <Target Name="CreateDirectories"> <MakeDir Directories=“\output\"/> </Target></Project>Simple task which create directory called “output”
  • 14.
  • 15.
    About Shell ScriptsAlsocalled batch programs or scriptsContains one or more commands and has a .bat or .cmd file name extensionfor, goto, call and if, enable you to do conditional processing of the commandsCommands are not case sensitive Executed by the command interpreter
  • 16.
    Batch Parameters Similar to Command line argumentsUsing Batch Parameters %{argument_no}Using modifiers with batch parametersecho %~f0How to change the position of batch parameter?Using SHIFT /N commandSHIFT /2 would shift %3 to %2 and so on, and leave %0 and %1 unaffected.%* Reference to all the arguments, not including %0Expands %0 to a fully qualified path name.
  • 17.