Mozilla + Firefox extension development

Wednesday, November 17, 2004

Build your Firefox extension with Ant

You need to download 4 things:
Structure your Firefox extension's source directory as follows:
  • your-extension-dir/
    • build.xml
    • do.bat
    • src/
      • install.rdf
      • chrome/
        • content/
          • contents.rdf
          • *.xul, *.js,
        • locale/
          • en-US/
          • ...
        • skin/
          • classic/
          • ...
      • components/
        • *.idl
        • *.js
      • defaults/
    • build/ (containing intermediate generated files)
    • dist/ (containing the final distribution XPI file)
The build.xml file should look like this:

<?xml version="1.0" ?>
<project name="your-project-name" default="createDistributionXPI">
<!-- Tool directories : *** Make sure you fix these to match your own environment *** -->
<property name="gecko_sdk_path" location="C:\Tools\gecko-sdk" />
<property name="buildtools_path" location="C:\Tools\buildtools" />

<!-- Derived tool directories -->
<property name="xpidl_exe" location="${gecko_sdk_path}/bin/xpidl.exe" />
<property name="IDLs_path" location="${gecko_sdk_path}/idl" />
<property name="libIDL_path" location="${buildtools_path}/windows/bin/x86" />

<!-- Project directories -->
<property name="src_dir" location="./src" />
<property name="build_dir" location="./build" />
<property name="dist_dir" location="./dist" />
<property name="components_dir" location="${src_dir}/components" />

<!-- Custom tasks -->
<taskdef resource="net/sf/antcontrib/"/>

<!-- Targets -->
<target name="createChromeJAR">
<zip destfile="${build_dir}/${}.jar" update="true"
includes="content/**, locale/**, skin/**"

<target name="createComponentInterfaceXPTs">
<foreach target="compileIDL" param="idl_file">
<fileset dir="${src_dir}/components" includes="*.idl" />

<target name="compileIDL">
<exec executable="${xpidl_exe}" dir="${build_dir}">
<env key="Path" path="${env.Path};${libIDL_path}" />
<arg line="-m typelib -w -v -I ${IDLs_path} -I ${components_dir} ${idl_file}" />

<target name="createDistributionXPI" depends="createChromeJAR, createComponentInterfaceXPTs">
<zip destfile="./dist/${}.xpi" update="true">
<zipfileset dir="${build_dir}" includes="${}.jar" prefix="chrome" />
<zipfileset dir="${src_dir}/components" includes="*.js" prefix="components" />
<zipfileset dir="${build_dir}" includes="*.xpt" prefix="components" />
<zipfileset dir="${src_dir}/defaults" includes="**" prefix="defaults" />
<zipfileset dir="${src_dir}" includes="install.rdf" />
<copy file="${dist_dir}/${}.xpi" tofile="${dist_dir}/${}" />

You need to change the 3 strings in blue toward the beginning of the file to match your project's name and your tool installations. Note that the dist/ directory will contain both an XPI file and a ZIP file. The ZIP file is just a copy of the XPI file with a .ZIP extension so that you can use WinZip to open it up and inspect its content.

Finally, you should create a batch file in your project's directory that looks something like this:

call c:\Tools\apache-ant-1.6.2\bin\ant
"C:\Program Files\Mozilla Firefox\firefox.exe"

One more note: it's probably convenient to put a link to your project's XPI file on Firefox's toolbar because you will need to reinstall the extension many times:
  • Navigate Firefox to the dist/ directory containing the XPI (not to the XPI itself)
  • Drag the link to the XPI file to the toolbar
Oh, if you delete a file in your src directory, it might still be stuck on one of the generated zip files. Make sure to delete all files in the build and dist directory for a clean build.


Post a Comment

<< Home