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/antcontrib.properties"/>

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

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

<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}" />
</exec>
</target>

<target name="createDistributionXPI" depends="createChromeJAR, createComponentInterfaceXPTs">
<zip destfile="./dist/${ant.project.name}.xpi" update="true">
<zipfileset dir="${build_dir}" includes="${ant.project.name}.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" />
</zip>
<copy file="${dist_dir}/${ant.project.name}.xpi" tofile="${dist_dir}/${ant.project.name}.xpi.zip" />
</target>
</project>


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.

7 Comments:

Post a Comment

<< Home