Spring Boot + GWT 嵌入式配置

12

我想配置Spring Boot嵌入式Servlet容器+GWT,希望的方式是创建一个仅包含已编译的gwt文件和静态资源的jar / war文件。我想从lib/*加载jar和从类路径加载配置文件。

我找不到任何可行的示例。确实有一个,https://github.com/Ekito/spring-boot-gwt,但所有依赖项和配置仍在war中。

有人能提供解决方案吗?

2个回答

8
经过长时间的搜索和测试,我想到了以下解决方案:
```
<!-- POM -->
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ekito.gwt</groupId>
<artifactId>gwt-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Ekito Spring Boot GWT WebApp</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <start-class>fr.ekito.gwt.server.ServerApplication</start-class>
    <java.version>1.7</java.version>

    <gwtVersion>2.6.0</gwtVersion>
    <googleGin>2.1.2</googleGin>
    <outputFolder>${project.build.directory}/${artifactId}</outputFolder>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.undertow</groupId>
                <artifactId>undertow-websockets-jsr</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>

    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-user</artifactId>
        <version>${gwtVersion}</version>
    </dependency>
    <dependency>
        <groupId>com.google.gwt.inject</groupId>
        <artifactId>gin</artifactId>
        <version>${googleGin}</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${start-class}</mainClass>
                <layout>ZIP</layout>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>${gwtVersion}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <runTarget>GwtWebApp.html</runTarget>
                <persistentunitcachedir>${project.build.directory}</persistentunitcachedir>
                <deploy>${project.build.directory}/gwt-deploy</deploy>
                <webappDirectory>${project.build.directory}/classes/public</webappDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

我的项目结构与原始项目相同,https://github.com/Ekito/spring-boot-gwt,只是做了一些小的更改:

  • 我有一个src/main/resources/public文件夹代替了webapp文件夹,并将html和css文件放在那里。
  • 不需要web.xml文件,spring-boot会处理它。
  • 不需要WEB-INF文件夹。

因此,我有一个可运行的jar包,但由org.springframework.boot.loader.PropertiesLauncher运行。单个jar包按预期工作,Tomcat无法按照此处所述工作。

而且,我可以将lib文件夹移到jar文件外部,但是为了设置loader.path属性,我需要将其放到jar文件内的application.properties中。我应该能够使用-Dloader.path,但并没有起作用。

我会与spring团队联系。但到目前为止,进展很有希望。


你能否分享一下完整的工作示例?你是如何运行GWT服务器的?mvn gwt:run会启动Jetty而不是Spring Boot应用程序,对吗?你是从集成开发环境中使用它的吗?如何进行完整的mvn compile编译并运行jar包? - Martin C.
我认为我大部分都弄清楚了,只要使用 mvn gwt:compile 进行一次编译,然后运行 mvn gwt:run-codeserver 即可启用 "魔法"。只需要修复自动检测更改而无需手动点击 superDevMode 的 "compile" 书签即可... :) - Martin C.
我用Gradle做了同样的事情。像你一样做了相同的“更改”,现在运行得很好。https://github.com/feedm3/spring-boot-gwt - Fabian
我无法让它正常工作。我的Spring Boot单独运行良好,但是当我与GWT一起运行时,它就会出问题。我认为问题在于Jetty。你能帮我吗? - mor222

0

我想为那些没有看到gwt编译器生成的静态文件(如*.nocache.js)被请求和响应的位置的其他人提供更多细节,以补充Gokhan的答案。

我也是从https://github.com/Ekito/spring-boot-gwt示例开始的,但没有像https://github.com/interseroh/demo-gwt-springboot那样开箱即用(我也不知道为什么它能工作)。所以你应该使用

<webappDirectory>${project.build.directory}/${artifactId}/WEB-INF/classes/public</webappDirectory>

替代

 <webappDirectory>${project.build.directory}/classes/public</webappDirectory>

这是根据Gokhan的答案。同时,不要忽略打包部分中的war和war-maven-plugin。这样,生成的静态文件将被放置在实际的/public/classes/文件夹中,然后打包到jar中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接