如何使用maven在一个胖JAR中打包GWT应用程序?

时间:2016-04-29 00:00:30

标签: java maven gwt jar

我的GWT应用程序目前使用maven打包成战争,然后将其部署到tomcat。我想更改为jar中的包装,以便可以使用java -jar执行,并使应用程序从Jetty等嵌入式Web服务器提供。类似于在开发模式下运行时发生的情况,但我希望它来自一个易于部署的胖/超级jar包括html和GWT js文件。我只能找到使用spring为Jetty执行此操作的说明,并且无法使其适应GWT。像Jetty's example uber jar project这样的东西很接近,只需要和GWT一起工作。

3 个答案:

答案 0 :(得分:0)

您只需更改maven项目的包装即可:

<project>
    ...
    <packaging>jar</packaging>
    ...
</project>

请注意couldnt deploy a jar file in tomcat

接下来,我将确保依赖项gwt-dev已提供范围(因为此依赖项已包含旧版本的jetty)。之后,您可以添加jersey依赖项并启动一个为您的GWT-Appliccation提供服务的嵌入式服务器(例如see here)。

答案 1 :(得分:0)

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.project</groupId>
  <artifactId>core</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>

for more info

答案 2 :(得分:0)

结合example Jetty project和@ mam10eks的答案,我想我还是设法找到了一个简单的GWT应用程序:

的pom.xml:

<?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">

<modelVersion>4.0.0</modelVersion>
<groupId>com.example.HelloJettyGwt</groupId>
<artifactId>HelloJettyGwt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloJettyGwt</name>

<properties>
    <gwtVersion>2.7.0</gwtVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty-version>9.3.2.v20150730</jetty-version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt</artifactId>
            <version>${gwtVersion}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-servlet</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-user</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-dev</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>javax-websocket-server-impl</artifactId>
        <version>${jetty-version}</version>
    </dependency>

</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>2.7.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test</goal>
                        <goal>generateAsync</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <runTarget>app.html</runTarget>
                <modules>
                    <module>com.example.HelloJettyGwt.app</module>
                </modules>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>
                                    src/main/webapp
                                </directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>**/*.html</include>
                                    <include>**/*.css</include>
                                    <include>**/web.xml</include>
                                </includes>
                            </resource>

                            <resource>
                                <directory>
                                    ${project.build.directory}/${project.build.finalName}/
                                </directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>**/app/**</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <archive>
                    <manifest>
                     <mainClass>com.example.HelloJettyGwt.ServerMain</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Implementation-Version>${jetty-version}</Implementation-Version>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <id>rebuild-war</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <artifactSet>
                            <includes>
                                <include>*:*</include>
                            </includes>
                        </artifactSet>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/VERSION.txt</exclude>
                                    <exclude>META-INF/LICENSE.txt</exclude>
                                </excludes>
                            </filter>
                            <!-- exclude the MANIFEST.MF entries that will cause problems -->
                            <filter>
                                <artifact>javax.annotation:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>javax.servlet:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>javax.websocket:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>org.eclipse.jetty:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>org.eclipse.jetty.websocket:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>org.ow2.asm:*</artifact>
                                <excludes>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <id>enforce-java</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireMavenVersion>
                                <version>[3.0.0,)</version>
                            </requireMavenVersion>
                            <requireJavaVersion>
                                <version>[1.8,)</version>
                                <message>[ERROR] OLD JDK [${java.version}] in use.
                                    Jetty
                                    ${jetty-version} requires JDK 1.8 or newer</message>
                            </requireJavaVersion>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

主要方法:

    Server server = new Server(8080);

    URL webRootLocation = this.getClass().getResource("/app.html");
    if (webRootLocation == null)
    {
        throw new IllegalStateException("Unable to determine webroot URL location");
    }

    URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/app.html$","/"));

    WebAppContext altHandler = new WebAppContext();
    altHandler.setResourceBase(Resource.newResource(webRootUri).toString());
    altHandler.setDescriptor(Resource.newResource(webRootUri).toString() + "WEB-INF/web.xml");
    altHandler.setContextPath("/");
    altHandler.setParentLoaderPriority(true);

    server.setHandler(altHandler);

    server.start();
    server.join();
}