【问题标题】:How to compile a GWT project using maven ONLY如何仅使用 maven 编译 GWT 项目
【发布时间】:2020-12-16 10:43:15
【问题描述】:

我有一个 GWT 项目,我目前使用 Eclipse GWT plugin 编译,但现在我想让我的项目可独立于 IDE 进行编译,方法是仅使用 Maven。

根据我的研究,我发现您需要将 GWT Compiler plugin 添加到 pom.xml 以便将代码从 Java 编译为 Javascript 文件(JS 文件可以稍后打包到战争中 em>),但我似乎无法在 maven 存储库中找到编译器依赖项,也找不到编译器的良好文档。您能否发布一个“基本”工作 pom.xml 文件,该文件可用于编译 Hello world GWT 项目,非常感谢,谢谢。

编辑:

  • 我使用的是 GWT 2.7.0

【问题讨论】:

  • 您使用的是什么版本的 GWT?
  • @tgdavies 我正在使用 GWT 2.7.0
  • 您是否尝试使用 GWT 原型来生成起始 pom.xml
  • @Yassir Archetypes 应该对大多数此类情况有所帮助,因为它们可以生成特定项目堆栈所需的其余样板。我记得 5 到 6 年前在 GWT 中编码,出于某种原因,我没有使用原型来试图找出模块化 GWT 项目的整个构建和运行周期。这导致浪费了大量时间试图找出问题所在。因此,无论是 tgdavies 的想法还是 gwt-maven-plugin.github.io/gwt-maven-plugin(最后一次是在 2017 年)都很可能对你有用。

标签: java maven gwt compilation


【解决方案1】:

您可以使用 gwt-maven-plugin。您已经提到您已经拥有 Eclipse GWT 插件,因此您可以创建一个新的 GWT 项目(例如,右键单击 Project Explorer -> New -> Other... -> GWT Web Application Project)。填写项目名称和包,并确保启用复选框“生成 Maven 项目”。然后你有一个 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">
   <!-- POM file generated with GWT webAppCreator -->
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.world.hello</groupId>
   <artifactId>HelloWorld</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>com.world.hello.HelloWorld</name>
   <properties>
      <!-- Setting maven.compiler.source to something different to 1.8
         needs that you configure the sourceLevel in gwt-maven-plugin since
         GWT compiler 2.8 requires 1.8 (see gwt-maven-plugin block below) -->
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <!-- Don't let your Mac use a crazy non-standard encoding -->
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   </properties>
   <dependencyManagement>
      <dependencies>
         <!-- ensure all GWT deps use the same version (unless overridden) -->
         <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt</artifactId>
            <version>2.9.0</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>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <!-- Output classes directly into the webapp, so that IDEs and "mvn process-classes" update them in DevMode -->
      <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
      <plugins>
         <!-- GWT Maven Plugin-->
         <plugin>
            <groupId>net.ltgt.gwt.maven</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>1.0-rc-8</version>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>test</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <moduleName>com.world.hello.HelloWorld</moduleName>
               <moduleShortName>HelloWorld</moduleShortName>
               <failOnError>true</failOnError>
               <!-- GWT compiler 2.8 requires 1.8, hence define sourceLevel here if you use
               a different source language for java compilation -->
               <sourceLevel>1.8</sourceLevel>
               <!-- Compiler configuration -->
               <compilerArgs>
                  <!-- Ask GWT to create the Story of Your Compile (SOYC) (gwt:compile) -->
                  <arg>-compileReport</arg>
                  <arg>-XcompilerMetrics</arg>
               </compilerArgs>
               <!-- DevMode configuration -->
               <warDir>${project.build.directory}/${project.build.finalName}</warDir>
               <classpathScope>compile+runtime</classpathScope>
               <!-- URL(s) that should be opened by DevMode (gwt:devmode). -->
               <startupUrls>
                  <startupUrl>HelloWorld.html</startupUrl>
               </startupUrls>
            </configuration>
         </plugin>
         <!-- Skip normal test execution, we use gwt:test instead -->
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
               <skip>true</skip>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

在控制台上运行 mvn clean install 将使用 Maven 编译您的项目。或者您在 Project Explorer 上右键单击您的项目并选择 Maven -> Update Project...

有时,在创建 GWT Maven 项目后,Eclipse 不会自动将其识别为 Maven 项目。如果是这种情况,请在 Project Explorer 上右键单击您的项目,然后选择 Configure -> Convert to Maven Project。

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多