为Ant build.xml创建GWT编译规则

时间:2012-02-10 21:07:17

标签: gwt ant

我正在尝试使Eclipse兼容的GWT项目也可以在命令行上编译(通过Ant)。

Eclipse提供了导出build.xml的功能;这适用于编译类,但由于GWT的特殊内容都是通过插件提供的,因此不包括这些规则。

Google提供a tool来为新项目创建build.xml个文件。我已将此生成的规则合并到Eclipse导出的文件中。

编译(Eclipse提供的部分)是成功的。

以下是compile-things-to-javascript任务:

  <target name="gwtc" depends="build" description="GWT compile to JavaScript (production mode)">
    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
      <classpath>
        <pathelement location="war/WEB-INF/classes"/>
        <pathelement location="src"/>
        <path refid="project.class.path"/>
        <pathelement location="/Applications/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.4.0.v201201120043-rel-r37/gwt-2.4.0/validation-api-1.0.0.GA.jar" />
        <pathelement location="/Applications/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.4.0.v201201120043-rel-r37/gwt-2.4.0/validation-api-1.0.0.GA-sources.jar" />
      </classpath>
      <!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->
      <jvmarg value="-Xmx256M"/>
      <!--<arg line="-style PRETTY"/>-->
      <arg line="-war"/>
      <arg value="war"/>
      <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
      <arg line="${gwt.args}"/>
      <arg value="edu.calpoly.csc.scheduler"/>
    </java>
  </target>

.gwt.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>                                                 
<module rename-to='gwtview'>                                                           
  <!-- Inherit the core Web Toolkit stuff.                        -->                  
  <inherits name='com.google.gwt.user.User'/>                                          

  <!-- Inherit the default GWT style sheet.  You can change       -->
  <!-- the theme of your GWT application by uncommenting          -->                  
  <!-- any one of the following lines.                            -->                  
  <inherits name='com.google.gwt.user.theme.clean.Clean'/>                             
  <!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->              
  <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->                  
  <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->                  

  <!-- Other module inherits                                      -->                  

  <!-- Specify the app entry point class.                         -->                  
  <entry-point class='edu.calpoly.csc.scheduler.view.client.GWTView'/>                                                                                                        
  <!-- Specify the paths for translatable code                    -->                  
  <source path='client'/>                                                              
  <source path='shared'/>
  <source path='view'/>                                                                

</module> 

尝试运行任务时,出现此错误:

gwtc:
     [java] Compiling module edu.calpoly.csc.scheduler
     [java]    Finding entry point classes
     [java]       [ERROR] Unable to find type 'edu.calpoly.csc.scheduler.view.client.GWTView'
     [java]          [ERROR] Hint: Previous compiler errors may have made this type unavailable
     [java]          [ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly

这对我来说很奇怪,因为课程非常重要:

[$]> ls war/WEB-INF/classes/edu/calpoly/csc/scheduler/view/client 
GWTView$1.class            GWTView$1MyHandler.class   GreetingService.class
GWTView$1MyHandler$1.class GWTView.class              GreetingServiceAsync.class

HALP?

2 个答案:

答案 0 :(得分:2)

您确定在类路径上有GWT应用程序的来源吗? GWT正在将java源代码编译为js,而不是* .class文件。

答案 1 :(得分:1)

确保您传递给GWT编译器的类路径上有所需的库/ jar和任何其他SOURCE,因为它需要能够在GWT库中找到Java源代码才能编译它们,与Javac编译器不同,只能使用提供的.class文件对库进行编译。

这是我的ant gwtc编译宏。 (删除了前导<以避免格式化问题...)

macrodef name="gwtCompileApplication"  >
    <attribute name="app" />
    <attribute name="extraArgs" default="" />
    <attribute name="gwtcExtras" default="" />
    <sequential>
        <java classpathref="gwtCompile.classpath" classname="com.google.gwt.dev.Compiler" fork="true" failonerror="true">
            <jvmarg value="-Xmx512M" />

            <arg value="-strict" />

            <!-- Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
            <arg value="-logLevel" />
            <arg value="DEBUG" />
    -->

            <!-- request detailed, non-obfuscated JS output 
            <arg value="-style" />
            <arg value="DETAILED" />
    -->

            <arg value="-localWorkers" />
            <arg value="4" />

            <arg value="-war" />
            <arg value="${war.dir}" />

            <arg value="-deploy" />
            <arg value="deploy" />

            <!-- These two lines have been removed as otherwise it won't work on Mac OS X 
                    <arg value="@{extraArgs}" />
                    <arg value="@{gwtcExtras}" />
            -->

            <!-- This can be used to see more details about warnings, but they will be converted to errors and build will fail
            <arg value="-strict" />
            -->

            <arg value="-logLevel" />
            <arg value="INFO" />

            <arg value="@{app}" />
        </java>
    </sequential>
</macrodef>

这可以从任何目标调用: 编译我的一个特定模块的目标,名为'Admin',其中文件Admin.gwt.xml文件位于.ta.Admin内。我有一个调试和生产构建类型和一个.gwt.xml,每个模块都有不同的加速编译以进行调试(更少的用户代理和语言=更少的排列)

target name="gwtcAdmin" depends="compile, buildtype" description="GWT Compile Admin" >
    <gwtCompileApplication app="com.bcntouch.ta.Admin.${build_type}_Admin" extraArgs="${gwtcArgs}" gwtcExtras="${gwtcExtras}"/>
</target>

但是关键部分是我使用的GWT编译类路径。这是我设置路径的目标:

target name="gwtPath">
    <path id="gwt.classpath">
        <pathelement location="${gwt.sdk.dir}/gwt-user.jar" />
        <pathelement location="${gwt.sdk.dir}/gwt-servlet.jar" />
    </path>

    <!-- For GWT compile we need a path with source AND the GWT Compiler classes -->
    <path id="gwtCompile.classpath">
        <path refid="source.classpath" />
        <!-- This is put after source, so GWT Compiler it doesn't pick up out of date versions of old css and uibinder
             files in classes generated by Eclipse/GWT Plugin -->
        <path refid="classpath" /> 
        <path refid="tool.classpath" />
    </path>     
</target>