Menu

MSBuild And Java

February 18, 2015 - Programming

Recently I had the opportunity to create an automated Build script for all our software. Being that “all our software” was over 50 applications and over a dozen libraries, that was no straightforward task.

Because all but 2 of our programs were written in .NET, MSBuild was pretty much a shoe-in for the task. And while getting all the information organized properly (dependencies, project file locations, etc) was a lot of gruntwork, I was able to get all the C# Applications building.

This did leave some outliers- those 2 pieces of software that use Java. Specifically, a Java program that effectively acts as an adapter between C# and Java, and some Jasper Reports.

My goal was to have the build script, and the files it needs, directly in source control. For MSBuild itself, I was able to have a batch file try to find the appropriate Visual Studio Tools location by simply checking for the existence of files from newest versions to oldest versions. For Java, I settled on the use of the build.xml that is created by and maintained by Netbeans, which we’ve settled on using for the Java projects we maintain. This still left us with a few considerations.

Wherefore art thou, JDK?

The first issue was that Ant requried a JDK to build the Java project, which is reasonable. What wouldn’t be reasonable, however, would be slapping the 130MB+ JDK files directly into the SVN. And since my goal was a “configure-free” usage, it seemed like I needed to come up with a way to find an appropriate JDK version.

My “solution” was a bit of a hack; as this was all running from a batch file, I created a C# console program that effectively searched through the two system Program Files folders to try to find the latest version of the Java JDK to use.

Probably not a perfect solution to the problem, but it got the job done for what I was after. Effectively, it searches for a JDK install by looking for the appropriate folders, and then trying to sort them based on parsing the directory names for the Java install and grabbing the most recent one.

With that problem out of the way, I ran the build script- and found that while Ant claimed a successful build, the output directory only had the jar for our program- that is, the dist folder did not also have a lib folder. This was puzzling as building from Netbeans created that folder. As it turned out, this was because the functionality to copy the libraries was a “add-on” file. I was able to find this add-on file in my Netbeans install folder, within C:\Program Files (x86)\NetBeans 8.0\java\ant\extra\org-netbeans-modules-java-j2seproject-copylibstask.jar, and copied it into mt local folder. Than I invoked the ant task with:

technically, you cannot build Java with MSBuild. however, you can use an exec node to run a batch file which can run ant and build Java, as we see here. Cross-language build scripting is usually going to involve something like that, particularly if those languages cross an eco-system. Visual Basic and C# or F# is no problem; but if you have Java and C# or Scala and Rust or any other set of combinations, you’ll find that the tooling is not going to be generic enough because those different software ecosystems have different goals and different designs which often don’t lend themselves well to building the other language, so you need to improvise.

Have something to say about this post? Comment!