I am trying to automate the build process of my projects (both java and .net) using Ant and MSBuild. I’ve read about them and know how to write build scripts in Ant and MSBuild. But I wonder, is there any guidelines or best practices for writing build scripts in general? Here are two that I’ve found, but I would like to hear more from other developers.
I've seen this post as well, but it is detailed on the way of writing tasks. I need more high level guidelines, as mentioned above.
Here are the guidelines I collected from answers:
clean
target. compile
, package
and test
targets.If you're building VisualStudio applications, you are better off using msbuild
over Ant. The msbuild
command will do the build using the solution file your developers created, and basically emulate the same build they do.
If you truly want to automate everything, take a look at Jenkins. It has a plugin that can execute msbuild
and will automatically trigger a build every time someone makes a change. I use a combination fo msbuild
and Ant with Jenkins. I do the build using msbuild
, then use Ant to gather up and zip up all of my built artifacts. Then, people can download the built artifacts directly from Jenkins.
Now, with your Java applications, you'll have to use Ant. I use the following guidelines
clean
target. This target removes any files that were added during the build process, returning the working directory to a state before the clean
took place.target
directory. That way, my clean command can simply do a <delete dir="${target.dir}/>
and cleans everything up nice and sparkly.build.xml
file. My master build.xml
file simply calls all of the sub-projects' build.xml
files.Just a comment on the second bullet point you mentioned above:
If a project contains a set of sub-systems and each sub-system has its own build scripts, the build file of the project should just call the build scripts of sub-systems.
Each subsystem should have its own build file but that file should import a common build file. The common build file would contain targets such as compile, test, package etc.
http://ant.apache.org/manual/Tasks/import.html
The subsystem build files are then very simple, don't contain duplication and only contain information that is specific to that subsystem (e.g. compile.classpath).
In MSBuild, targets should specify inputs and outputs, wherever possible (to enable dependency calculation). If necessary, make use of the Returns attribute to specify a target that returns different items than the ones used for dependency calculation.
The input and output items for a given target should be generated using either item transforms (for simple, path-based transformations), or another target (for more sophisticated transformations).
For MSBuild pre-4.x, for targets you define in a .targets file, consider using the following pattern to enable consumers to inject their own targets before the ones you are defining:
<PropertyGroup>
<MyTargetDependsOn>
Target1;
Target2;
SomeOtherTarget
</MyTargetDependsOn>
</PropertyGroup>
<Target
Name="MyTarget"
DependsOnTargets="$(MyTargetDependsOn)">
</Target>
This enables consumers to inject their own targets before the specified target simply by modifying the value of the MyTargetsDependsOn property:
<PropertyGroup>
<MyTargetDependsOn>
$(MyTargetDependsOn);
YetAnotherTarget
</MyTargetDependsOn>
</PropertyGroup>
<Target
Name="YetAnotherTarget">
</Target>
In MSBuild 4.x, you can simply use the BeforeTargets and AfterTargets attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With