`
brofe
  • 浏览: 230184 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用 Maven GWT Plugin 创建 GWT 工程(编译、调试、发布)

阅读更多
本文主要介绍如何使用Maven快速构建GWT项目。(本文假定你对GWT、Maven都有过一定了解)

LZ使用如下环境:
C:\Users\Administrator>mvn -version
    Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
    Java version: 1.6.0_18
    Java home: D:\install\dev\Java\jdk1.6.0_18\jre
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 7" version: "6.1" arch: "x86" Family: "windows"

    Eclipse 3.6、GWT 2.1.1


Step 1:创建GWT项目
mvn archetype:generate -DarchetypeRepository=repo1.maven.org -DarchetypeGroupId=org.codehaus.mojo -DarchetypeArtifactId=gwt-maven-plugin -DarchetypeVersion=2.1.1-SNAPSHOT


使用Maven创建的GWT工程包结构如下图所示:
http://www.yupoo.com/photos/brofe/79719131/

Step 2:修改 pom.xml, 添加其它所需依赖,LZ使用内嵌的 Jetty 7 故完整的POM如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<!-- POM file generated with GWT webAppCreator -->
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.brofe.gwt.samples</groupId>
	<artifactId>gwt-samples</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>GWT Maven Archetype</name>

	<properties>
		<!-- Convenience property to set the GWT version -->
		<gwt.version>2.1.1</gwt.version>
		<jetty.version>7.2.2.v20101205</jetty.version>
		<maven.compiler.source>1.6</maven.compiler.source>
		<maven.compiler.target>1.6</maven.compiler.target>
		<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.gwt</groupId>
			<artifactId>gwt-servlet</artifactId>
			<version>${gwt.version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.google.gwt</groupId>
			<artifactId>gwt-servlet-deps</artifactId>
			<version>${gwt.version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.google.gwt</groupId>
			<artifactId>gwt-user</artifactId>
			<version>${gwt.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>com.google.gwt</groupId>
			<artifactId>gwt-dev</artifactId>
			<version>${gwt.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>

		<!-- jetty -->
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-start</artifactId>
			<version>${jetty.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>${jetty.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-jsp-2.1</artifactId>
			<version>${jetty.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jsp-2.1-glassfish</artifactId>
			<version>2.1.v20100127</version>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.mortbay.jetty</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- jetty end -->
	</dependencies>

	<build>
		<!-- Generate compiled stuff in the folder used for developing mode -->
		<outputDirectory>${webappDirectory}/WEB-INF/classes</outputDirectory>

		<plugins>

			<!-- GWT Maven Plugin -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>gwt-maven-plugin</artifactId>
				<version>2.1.1-SNAPSHOT</version>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>test</goal>
							<!--<goal>i18n</goal> 表示不自动生成国际化类-->
							<!--<goal>generateAsync 表示不自动生成RPC异步类</goal>-->
						</goals>
					</execution>
				</executions>
				<!-- Plugin configuration. There are many available options, see gwt-maven-plugin documentation at codehaus.org -->
				<configuration>
					<runTarget>GWTSamples.html</runTarget>
					<hostedWebapp>${webappDirectory}</hostedWebapp>
					<i18nMessagesBundle>com.brofe.gwt.samples.client.Messages</i18nMessagesBundle>
				</configuration>
			</plugin>

			<!-- Copy static web files before executing gwt:run -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>exploded</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<webappDirectory>${webappDirectory}</webappDirectory>
				</configuration>
			</plugin>

		</plugins>
	</build>

</project>


Step 3:将gwt-samples项目转化成Eclipse项目:
mvn eclipse:eclipse


Step 4: 编写启动 Jetty 服务的类:
/**
 * $Id: StartGWTSamplesJetty.java, 2011-2-6 下午11:55:43, brofe Exp $
 */
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * @author <a href="brofe.pan@gmail.com">brofe</a>
 * @since 2011-2-6 下午11:55:53
 * @version v 1.0
 */
public class StartGWTSamplesJetty {

	protected int port = 8088;

	protected String webapp = "gwt-samples-1.0";

	protected String deployPath = "./target/" + webapp;

	public static void main(String[] args) throws Exception {
		StartGWTSamplesJetty jetty = new StartGWTSamplesJetty();
		jetty.init();
		jetty.run();
	}

	protected void init() {
		
	}

	protected void run() throws Exception {
		Server server = new Server(this.port);
		
		WebAppContext context = new WebAppContext();
		context.setDescriptor(this.webapp + "/WEB-INF/web.xml");
		context.setResourceBase(deployPath);
		context.setContextPath("/");
        context.setParentLoaderPriority(true);
        
        server.setHandler(context);
        
        server.start();
        server.join();
	}
}


Step 5:使用Maven编译gwt-samples,编译结果在:“target/gwt-samples-1.0/GWTSamples”目录
mvn gwt:compile


Step 6:运行StartGWTSamplesJetty.java,然后在浏览器中(http://localhost:8088)即可访问

Step 7:在 Eclipse Debug Configurations 中配置GWT调试模式,并且使用自己的Jetty服务器,而不是GWT调试模式自带的服务器,详情看如下截图

1、http://www.yupoo.com/photos/brofe/79719127/

2、http://www.yupoo.com/photos/brofe/79719128/
-war target/gwt-samples-1.0 -startupUrl GWTSamples.html com.brofe.gwt.samples.GWTSamples -noserver -port 8088


3、http://www.yupoo.com/photos/brofe/79719130/

Step 8:启动调试

1、先 Debug 运行:StartGWTSamplesJetty.java

2、再 Debug 运行刚才配置的DevMode:GWTSamples-Debug

3、然后浏览器中访问:http://localhost:8088,即可单步调试GWT程序。

Step 9:使用Maven打包发布,发布包会在target目录下
mvn war:war


其它:关于 Maven GWT Plugin 其它Goals,有兴趣的同学可以试试,比如mvn:gwt:run,详情见:http://mojo.codehaus.org/gwt-maven-plugin/plugin-info.html

由于Img标签貌似用不好,所以其中图片都是用URL的方式,如果有会用的同学请教教我。

本文完。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics