如何用Maven将GWT客户端代码编译为JavaScript(原用Ant实现)
Alright, let's convert your Ant GWT compilation task to Maven smoothly. The official gwt-maven-plugin is exactly what you need here—it replicates all the functionality of your Ant task while fitting into Maven's build lifecycle. Here's how to set it up:
1. Add the GWT Visualization Dependency
First, declare the gwt-visualization JAR as a dependency in your pom.xml, matching the classpath entry from your Ant task:
<dependencies> <!-- GWT Visualization Library --> <dependency> <groupId>com.google.gwt.google-apis</groupId> <artifactId>gwt-visualization</artifactId> <version>1.1.2</version> <!-- Replace with your actual library version --> <scope>provided</scope> </dependency> <!-- Include core GWT dependency (match your project's GWT version) --> <dependency> <groupId>com.google.gwt</groupId> <artifactId>gwt-user</artifactId> <version>2.9.0</version> <scope>provided</scope> </dependency> </dependencies>
The provided scope tells Maven this JAR is only needed for compilation (GWT will compile it to JavaScript, so it won't be included in runtime artifacts).
2. Configure the GWT Maven Plugin
Add the gwt-maven-plugin to your build plugins, mapping every parameter from your Ant task to Maven's configuration:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>gwt-maven-plugin</artifactId> <version>2.9.0</version> <!-- Match your GWT version --> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <!-- Equivalent to `-out build` in your Ant task --> <outputDirectory>${project.build.directory}/build</outputDirectory> <!-- Equivalent to `-style OBF` for obfuscated output --> <style>OBF</style> <!-- Your target GWT module (matches the final Ant argument) --> <module>org.compiere.gwt.Compiere</module> <!-- Equivalent to `maxmemory="512M"` --> <extraJvmArgs>-Xmx512M</extraJvmArgs> <!-- Equivalent to `fork="yes"` to run compiler in a separate JVM --> <fork>true</fork> <!-- Point to your source directory (matches `/src` in Ant classpath) --> <sources> <source>${project.basedir}/src</source> </sources> </configuration> </plugin> </plugins> </build>
Quick Parameter Breakdown (Ant → Maven):
-out build→<outputDirectory>${project.build.directory}/build</outputDirectory>-style OBF→<style>OBF</style>org.compiere.gwt.Compiere→<module>org.compiere.gwt.Compiere</module>maxmemory="512M"→<extraJvmArgs>-Xmx512M</extraJvmArgs>fork="yes"→<fork>true</fork>
3. Run the Compilation
To trigger the GWT JavaScript compilation, you can either:
- Run the plugin directly with:
mvn gwt:compile - Or, since we bound the plugin to the
compilephase, just run:mvn compile
This will generate the obfuscated JavaScript files in target/build (Maven's default build directory), mirroring the output of your original Ant task.
内容的提问来源于stack exchange,提问作者nimour pristou

