因为工作中经常用到GWT,所以整理GWT编译相关的内容记录下来。
- GWT的全称是 Google Web Toolkit
- GWT的开发模式是使用Java语言开发,经GWT编译为Javascript。
下面两种方式都可以实现调试和打包。
插件方式
在eclipse上安装GWT插件,在顶部菜单上找到help->Eclipse Marketplace
搜索GWT关键字,安装第一个选项中的GWT Eclipse Plugin 3.0.0
安装成功后,准备工作就完成了。下面假设你已经有了一个GWT工程来调试,
这里拿一个maven项目来做例子,后面有需要的话,我会演示非maven工程的操作,这里就不做介绍了。
首先把项目导入到eclipse中。
然后右键项目选中Properties进入如下界面,打勾Use GWT。然后就可以调试或者打包了。
下面先说调试的配置,右键工程,按下图进行操作
-startupUrl http://localhost:8080/poip/ -logLevel INFO -noserver -remoteUI "NULL:NULL" -codeServerPort 9997 com.xxx.gwt.main.poip-gwt-client -war D:\webapp\poip-webserver
-startupUrl 应用访问地址
-war 应用存储位置
com.xxx.gwt.main.poip-gwt-client为模块文件路径
此时应确保tomcat或承载你的后台服务的应用已经启动,并且通过 startupUrl 可以访问到,然后点击debug开始调试。
右键项目选中如下菜单进行打包,这里有两个地方的配置经常用到,一个是vm大小,调大可以加快编译速度,output style选项来控制编译模式,下面是对这几种模式的介绍。
Obfuscated 执行混淆操作
Pretty 选项不进行混淆,输出格式化的JavaScript
Detailed 也不进行混淆,输出的变量名和类名都附带原始Java类的详细信息
通过上面的操作已经能够基本满足编译和打包的需求,下面介绍一下不用在eclipse中配置直接使用maven的方式进行打包。
Maven方式
该模式下,是不需要把工程导入eclipse的。确保pom中有下面的gwt构建插件配置。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>NULL</source>
<target>NULL</target>
</configuration>
</plugin>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-8</version>
<extensions>true</extensions>
<configuration>
<classpathScope>compile</classpathScope>
<moduleName>com.xxx.gwt.main.poip-gwt-client</moduleName>
<moduleShortName>poipgwtclient</moduleShortName>
</configuration>
</plugin>
</plugins>
</build>
注意:上面的<configuration>中配置,moduleName对应的是项目中的下图标记的文件,moduleShortName对应的是模块文件中rename-to的配置。
上面的内容只是对一个gwt+maven工程的部分细节提示,下面再补充几个可能用到的maven配置。
<style>
Script output style: OBFUSCATED, PRETTY, or DETAILED.
• Type: java.lang.String
• Required: No
User Property: gwt.style
<jvmArgs>
Arguments to be passed to the forked JVM (e.g. -Xmx)
• Type: java.util.List
• Required: No
上面这些参数已经基本可以满足日常需要,如果需要了解更多的参数可以查看我的另一篇文章:http://www.gaojunxin.cn/2020/03/13/336/ 。下面进行编译,命令行切到当前工程路径下,执行下面命令:
mvn compile
mvn gwt:compile
执行完成后就得到目标js文件了。
Maven编译GWT的参数整理
Parameter Details
<compilerArgs>
Additional arguments to be passed to the GWT compiler.
• Type: java.util.List
• Required: No
<deploy>
The directory into which deployable but not servable output files will be written.
• Type: java.io.File
• Required: Yes
• Default: NULL/gwt/deploy
<draftCompile>
Enable faster, but less-optimized, compilations.
• Type: boolean
• Required: No
• User Property: gwt.draftCompile
• Default: false
<extra>
The directory into which extra files, not intended for deployment, will be written.
• Type: java.io.File
• Required: No
<failOnError>
Only succeed if no input files have errors.
• Type: java.lang.Boolean
• Required: No
• User Property: gwt.failOnError
<forceCompilation>
Require the GWT plugin to compile the GWT module even if none of the sources appear to have changed. By default, this plugin looks to see if the output *.nocache.js exists and inputs (POM, sources and dependencies) have not changed.
• Type: boolean
• Required: No
• User Property: gwt.forceCompilation
• Default: false
<jvmArgs>
Arguments to be passed to the forked JVM (e.g. -Xmx)
• Type: java.util.List
• Required: No
<localWorkers>
The number of local workers to use when compiling permutations. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point values are only accepted together with "C".
• Type: java.lang.String
• Required: No
• User Property: gwt.localWorkers
<logLevel>
Sets the level of logging detail.
• Type: java.lang.String
• Required: No
• User Property: gwt.logLevel
<moduleName>
Name of the module to compile.
• Type: java.lang.String
• Required: Yes
<moduleShortName>
The short name of the module, used to name the output .nocache.js file.
• Type: java.lang.String
• Required: No
<optimize>
Sets the optimization level used by the compiler. 0=none 9=maximum.
• Type: java.lang.Integer
• Required: No
• User Property: gwt.optimize
<skipCompilation>
Require the GWT plugin to skip compilation. This can be useful to quickly package an incomplete or stale application that's used as a dependency (an overlay generally) in a war, for example to launch that war in a container and then launch DevMode for this GWT application.
• Type: boolean
• Required: No
• User Property: gwt.skipCompilation
• Default: false
<sourceLevel>
Specifies Java source level.
• Type: java.lang.String
• Required: No
• User Property: maven.compiler.source
<staleMillis>
Sets the granularity in milliseconds of the last modification date for testing whether the module needs recompilation.
• Type: int
• Required: No
• User Property: lastModGranularityMs
• Default: 0
<style>
Script output style: OBFUSCATED, PRETTY, or DETAILED.
• Type: java.lang.String
• Required: No
• User Property: gwt.style
<systemProperties>
List of system properties to pass to the GWT compiler.
• Type: java.util.Map
• Required: No
<webappDirectory>
Specifies the location of the target war directory.
• Type: java.io.File
• Required: Yes
• Default: NULL/NULL
<workDir>
The compiler work directory (must be writeable).
• Type: java.io.File
• Required: Yes
Default: NULL/gwt/work