Saturday, August 28, 2010

Eclipse Ant tasks codepage issues

If you are using Ant to build your projects, you can take advantage of Eclipse's Ant view and configure your build.xml file(s) in there, so you can quickly launch its goals.

I recently had to use this view for a project in which the source files contained international characters. The build was failing in my newest version of Eclipse, although it worked in a previous version.

After looking into the issue, it turns out the codepage used in the Ant launcher is inherited from different places depending on the version of Eclipse. In order to check which codepage is being used, right click on the build file in the Ant view and choose Run As -> Ant Build... , then in the Common tab you can see both the Default - inherited encoding and Other encodings. The problem was I needed Cp1252 and it was not appearing in the list of Other encodings, while the Default - inherited encoding was UTF-8. Also, this list did not allow me to type in my own desired encoding. I tried selecting ISO-8859-1 as the encoding in this window, and the build succeeded, however ISO-8859-1 is not equivalent to Cp1252 as explained in http://en.wikipedia.org/wiki/Windows-1252.

So I needed to figure out where the Default encoding was being inherited from and get it to be Cp1252. In my previous versions, it was being inherited from the General -> Workspace preferences (accessed from Windows -> Preferences in the IDE), which I had correctly set to Cp1252 (as taken from my System settings). However, in my later version it was being inherited from the Content Types, Text -> XML -> Ant Buildfile, build.xml, which was indeed set to UTF-8 for default encoding, unless I set a specific encoding for the file, by right clicking on it, selecting Properties and changing the encoding in the resource tab.

At a glance, the following is a summary of the different places where the encoding can be set in Eclipse:

  • The file itself: right click on the file, choose Properties, and inside the Resource page of the preferences select either the Default (determined from content) encoding or another encoding from the Other list. In this window, you can type an encoding not appearing in the same list.
  • The General -> Workspace page in the preferences accessed from Windows -> Preferences in the IDE. The Default encoding is taken from the System properties, while other encodings can be selected from the Other list, which allows a different encoding to be typed in.
  • The General -> Content Types page in the preferences accessed from Windows -> Preferences in the IDE. In this page, a default encoding can be chosen for each file type. The Ant Buildfile encoding appears under the Text -> XML node. You can type your own encoding in the text field.
  • The buildfile in the Ant view, by right clicking on it and selecting Run As -> Ant Build... and going to the Common tab. In there, the Console Encoding can be selected to be either the Default - inherited or Other encoding, but the latter won't let you type in your own.
  • The XML -> XML Files page in the preferences accessed from Windows -> Preferences in the IDE. I have not had to use this page and it will not let you type in a different encoding from the ones offered in the list.
I have not tried this in every Eclipse version, but I think the switch for the Ant launcher from inheriting the default encoding from the Workspace to doing so from the Content Types was done in Eclipse Galileo (3.5).

My personal recommendation, if you encounter this problem, is to change the encoding for the build.xml file you need to run, by editing its individual properties.

No comments: