---
title: "利用Ant脚本导出BIRT"
author: "lurma"
date: "2013-06-27T00:12:28Z"
tags: ["Ant", "birt", "自动编译"]
categories: ["开源软件"]
slug: "ant-birt"
draft: false
lastmod: "2013-06-27T00:26:16Z"
id: 1894944
url: "https://lurma.iteye.com/blog/1894944"
---

最近公司在对birt进行二次开发，需要对birt进行自动化编译，因此对其研究了一番，写了下面的这篇文章，希望对初学者有些帮助。

谈到自动化编译，很容易让人想到ant工具，到网上找些资料，还是有些不错的，像下面的：

         *meteors1113的专栏的使用Ant导出Eclipse RCP全攻略*  

         网址：http://blog.csdn.net/meteors1113/article/details/5522538

       这篇文章对于单插件和多插件导出RCP产品都是可以导出的，但是对于BIRT来讲，还是差了些东西，因为BIRT的插件工程有二百多个，各插件之间的依赖关系比较复杂，本文也是在这篇博文的基础上做些修改和调整，原理其实是一样的，都是利用PDE进行编译和打包。

       下面就来说说利用ant脚本打包编译BIRT的步骤：

 

**1. 下载birt源码**

       这里就不作介绍了，网上还是有很多资料的，本文用的是birt3.7.0的源码；

       还有一点要说明，自己手动下载源码跟通过cvs下载的源码结构不太一样，cvs下载的源码结构不太一样，手动下载的包含编译时所依赖的jar包，但是cvs下载的源码得自己来配置所需要的jar包，建议大家选择手动下载，如果用cvs下载的也没关系，本文就是用cvs下载的源码，不包含依赖包。

 

**2. 搭建环境**

       在源码下载完成后，需要修改源码目录的结构，并且放到设定好的目录中，整个环境的结构如下所示（假设是在D盘）；

            

           D:\\BIRT-BUILD

                    |------build

                             |-------features

                             |-------plugins

                    |------repos

                             |-------rcp

                    |------target

 

       创建完目录后，将源码复制到plugins目录中，然后将源码中的feature类型的工程剪贴到features文件夹中，以nl1结尾的国际化工程和所有的测试工程都要放到plugins文件夹中，不能放到子目录中，否则会出现插件找不到的问题。

       源码配置好以后，就需要下载各种的环境包，列表如下：

                *eclipse-SDK-3.7.2-win32.zip（网址待更新）*

                *eclipse-3.7.2-delta-pack.zip（网址待更新）*

                *org.eclipse.rcp-3.7.2.zip（网址待更新）*

                *emf2.6.x（ http://archive.eclipse.org/modeling/emf/emf/downloads/drops/）*

                *GEF-ALL-3.7.2（网址待更新）*

                *dtp-sdk\_1.9.x（网址待更新）*

 

       下载完成后，将eclipse-SDK-3.7.2-win32.zip解压到target中，解压后的目录：

     

                target

                    |----eclipse

 

       这个target文件夹就是用来放置eclipse的，之后的PDE就是从这里调用。

       解压eclipse-3.7.2-delta-pack.zip到eclipse目录，并进行替换。如果不解压eclipse-3.7.2-delta-pack的话，发布的RCP程序将没有可执行程序（eclipse.exe)。

       解压org.eclipse.rcp-3.7.2.zip到rcp目录下，emf、gef以及dtp包都解压到eclipse目录中。

       如果你已经手动下载了3.7.0源码的话，就将源码plugins文件夹下的所有jar包以及下列文件夹复制到eclipse目录下的plugins文件夹中：

               *javax.xml.rpc\_1.1.0.v201005080400*

                *javax.xml.soap\_1.2.0.v201005080501*

               *org.apache.axis\_1.4.0.v201005080400*

               *org.apache.commons.discovery\_0.2.0.v201004190315*

               *org.w3c.sac*

      

       如果没有手动下载3.7.0源码的话（如果下载了那就更容易了，环境都省了好多，话又说回来，如果手动下载了源码的话，那谁还这么麻烦的用cvs下载的源码来搭建环境啊），建议下载EclipseIDE for Java and Report Developers 简称eclipse4report，地址： http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR1/eclipse-reporting-juno-SR1-win32.zip ，这个IDE包含birt的功能，因此在plugins里会有编译所依赖的一些插件，缺少插件时可以到这个IDE中去copy，不然的话，找这些个插件会挺麻烦的。

 

**3. 编写脚本**

       在eclipse中创建一个一般工程，名为org.eclipse.birt.report.designer.build，里面创建两个文件，build.properties和build.xml，这两个文件是最关键的文件，最终编译就靠这两个文件，也就是ant脚本。 

        build.xml文件内容如下：

```
<project name="org.eclipse.birt.report.designer.build" default="build">
    <property file="build.properties" />
    <!--
        Paths that allow running PDE Build without knowing specific bundle versions    
    -->
    <path id="equinox.launcher.path">
        <fileset dir="${eclipseLocation}/plugins">
            <include name="org.eclipse.equinox.launcher_*.jar" />
        </fileset>
    </path>
    <property name="equinox.launcher" refid="equinox.launcher.path" />
    <path id="pde.build.dir.path">
        <dirset dir="${eclipseLocation}/plugins">
            <include name="org.eclipse.pde.build_*" />
        </dirset>
    </path>
    <property name="pde.build.dir" refid="pde.build.dir.path" />
            
    <!--
        This target actually executes the PDE Build process by launching the
        Eclipse antRunner application.
    -->
    <target name="pde-build">
        <java classname="org.eclipse.equinox.launcher.Main" fork="true" failonerror="true">
            <arg value="-application" />
            <arg value="org.eclipse.ant.core.antRunner" />
            <arg value="-buildfile" />
            <arg value="${pde.build.dir}/scripts/productBuild/productBuild.xml" />
            <arg value="-Dtimestamp=${timestamp}" />
            <classpath>
                <pathelement location="${equinox.launcher}" />
            </classpath>
        </java>
    </target>
    <target name="clean">
        <delete dir="${buildDirectory}" />
    </target>
    <target name="build" depends="pde-build" />
</project>
```

 

build.properties文件内容如下：

       

```
###############################################################################
# Copyright (c) 2003, 2009 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     IBM Corporation - initial API and implementation
###############################################################################
#####################
             
eclipseLocation=D:/BIRT-BUILD/target/eclipse
pde.build.includeFetch=true
generateSourceReferences=true
product=D:/BIRT-BUILD/build/plugins/org.eclipse.birt.report.designer.ui.rcp/BIRT.product
             
runPackager=true
basedir=D:/BIRT-BUILD
             
# The prefix that will be used in the generated archive.
archivePrefix=BIRT
             
# The directory into which the build elements are fetched and where
# the build takes place.
buildDirectory=D:/BIRT-BUILD/build
             
# The location underwhich all of the build output will be collected.
collectingFolder=${archivePrefix}
             
# The list of {os, ws, arch} configurations to build.  This
# value is a '&' separated list of ',' separate triples.  For example,
#     configs=win32,win32,x86 & linux,motif,x86
# By default the value is *,*,*
#configs = *, *, *
configs=win32, win32, x86
             
#Allow cycles involving at most one bundle that needs to be compiled with the rest being binary bundles.
allowBinaryCycles = true
             
#Arguments to send to the zip executable
zipargs=
             
#Arguments to send to the tar executable
tarargs=
             
# Type of build.  Used in naming the build output.  Typically this value is
# one of I, N, M, S, ...
buildType=I
             
# ID of the build.  Used in naming the build output.
buildId=BIRT
             
# Label for the build.  Used in naming the build output
buildLabel=${buildType}.${buildId}
             
# Timestamp for the build.  Used in naming the build output
timestamp=007
             
base=D:/BIRT-BUILD
baseLocation=${base}/target/eclipse
             
#Folder containing repositories whose content is needed to compile against
repoBaseLocation=${base}/repos
#Folder where the content of the repositories from ${repoBaseLocation} will be made available as a form suitable to be compiled against
transformedRepoLocation=${buildDirectory}/transformedRepos
             
#Os/Ws/Arch/nl of the eclipse specified by baseLocation
baseos=win32
basews=win32
basearch=x86
             
#this property indicates whether you want the set of plug-ins and features to be considered during the build to be limited to the ones reachable from the features / plugins being built
filteredDependencyCheck=false
             
#this property indicates whether the resolution should be done in development mode (i.e. ignore multiple bundles with singletons)
resolution.devMode=false
             
#pluginPath is a list of locations in which to find plugins and features.  This list is separated by the platform file separator (; or :)
#a location is one of: 
#- the location of the jar or folder that is the plugin or feature : /path/to/foo.jar or /path/to/foo
#- a directory that contains a /plugins or /features subdirectory
#- the location of a feature.xml, or for 2.1 style plugins, the plugin.xml or fragment.xml
pluginPath=${buildDirectory}/transformedRepos/features/
             
skipBase=true
eclipseURL=<url for eclipse download site>
eclipseBuildId=<Id of Eclipse build to get>
eclipseBaseURL=${eclipseURL}/eclipse-platform-${eclipseBuildId}-win32.zip
             
skipMaps=true
mapsRepo=:pserver:anonymous@example.com/path/to/repo
mapsRoot=path/to/maps
mapsCheckoutTag=HEAD
             
             
#tagMaps=true
mapsTagTag=v${buildId}
             
skipFetch=true
             
# Specify the output format of the compiler log when eclipse jdt is used
logExtension=.log
             
# Whether or not to include debug info in the output jars
javacDebugInfo=false
```

  

**4. 编写批处理文件**

       windows的bat为例。在windows中进入org.eclipse.birt.report.designer.build的目录，创建一个bat文件（比如build.bat ），内容如下： 

```
rem 设置java jdk的路径，本文用的是jdk1.5.0_22
set JAVA_HOME=D:/DevTools/jdk1.6.0_19
rem 设置ant路径，本文用的是ant-1.8.2
set ANT_HOME=D:/DevTools/apache-ant-1.7.1
%ANT_HOME%/bin/ant.bat -file build.xml
```

 

        以后每次打包就可以只运行这个bat文件了。

       设置java jdk的路径，本文用的是jdk1.5.0\_22设置ant路径，本文用的是ant-1.8.2

 

**5. 常见问题**

       双击运行批处理的时候，很有可能会出现问题，但是窗口会闪退，因此最好在命令提示符中运行批处理文件。

       下面对运行脚本的过程中可能会出现的一些问题进行一下说明：

    

       运行脚本，会出现如下问题：

```
[java]BUILD FAILED
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:39:The following error occurred while executing this line:
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:69:Unable to find plug-in: com.lowagie.text_0.0.0. Please check the error log formore details.
[java]
[java]Total time: 4 seconds
[java]An error has occurred. See the log file
```

 

       由异常信息可以知道，导致编译失败的原因是由于缺少com.lowagie.text插件，到eclipse4report目录下plugins文件夹中复制过来即可。

 

       再次运行，会出现以下错误：

```
[java]BUILD FAILED
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:39:The following error occurred while executing this line:
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:69:Bundle com.shentong.kfront.designer.gdm_1.0.0.201302261654 failed to resolve.:
[java]     Missing required plug-inorg.eclipse.birt.report.model_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.report.designer.core_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.report.designer.ui_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.report.designer.ui.views_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.core_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.report.designer.ui.data_3.7.0.
[java]     Missing required plug-in org.eclipse.birt.report.data.adapter_3.7.0.
[java]     Missing required plug-inorg.eclipse.birt.data_3.7.0.
[java]     Unsatisfied import packageorg.eclipse.birt.report.engine.api_0.0.0.
[java]     Unsatisfied import packageorg.eclipse.birt.report.engine.api.impl_0.0.0.
[java]     Unsatisfied import packageorg.eclipse.birt.report.model.adapter.oda.impl_0.0.0.
[java]
[java]
[java]Total time: 5 seconds
[java]An error has occurred. See the log file
```

 

       在eclipse4report目录下plugins文件夹中找到如下插件并导入：

              *org.apache.batik.\*.jar  （以org.apache.batik开头的都要导入）*

              *org.mozilla.javascript\_\*.jar*

              *org.w3c.css.sac\_\*.jar*

              *org.w3c.dom.events\_\*.jar*

              *org.w3c.dom.smil\_\*.jar*

              *org.w3c.dom.svg\_\*.jar*

       

       导入后运行脚本：

```
[java]BUILD FAILED
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:39:The following error occurred while executing this line:
[java]D:\BIRT-BUILD\target\eclipse\plugins\org.eclipse.pde.build_3.7.0.v20111116-2009\scripts\productBuild\productBuild.xml:69:Unable to find plug-in: javax.xml.rpc_0.0.0. Please check the error log formore details.
[java]
[java]Total time: 4 seconds
[java]An error has occurred. See the log file
```

 

       还是报找不到插件的异常，同理，缺少哪个插件，到eclipse4report中复制过来即可。

       导入完成后，脚本应该就可以正常编译了，但是打包出来的产品可能会出现如下的问题：

```
java.lang.RuntimeException:Application "{0}" could not be found in the registry. Theapplications available are: org.eclipse.equinox.app.error, org.eclipse.help.base.infocenterApplication,org.eclipse.help.base.helpApplication, org.eclipse.help.base.indexTool,org.eclipse.update.core.standaloneUpdate,org.eclipse.update.core.siteOptimizer.
       atorg.eclipse.equinox.internal.app.EclipseAppContainer.startDefaultApp(EclipseAppContainer.java:248)
       at org.eclipse.equinox.internal.app.MainApplicationLauncher.run(MainApplicationLauncher.java:29)
       at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
       atorg.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
       atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
       atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
       atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       atjava.lang.reflect.Method.invoke(Method.java:597)
       at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
       atorg.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
       atorg.eclipse.equinox.launcher.Main.run(Main.java:1410)
```

 

       造成这个问题的原因有很多种，我只说我遇到的，在将下载的包导入到eclipse中时，很有可能会出现版本冲突的问题，在运行脚本的时候就是因为如下包的版本不对，才出现了如上所示的异常：

              *org.w3c.dom.smil\_\*.jar*

版本本该是1.0.0.\*，但是由于导入了1.0.1版本的jar包，导致出现了上述的问题。

 

       继续运行批处理文件，解决了以上的问题后，应该会成功的导出BIRT产品的，祝大家成功！！！