Gwt Maven 多模块。如何配置依赖项以将请求工厂代理放入共享模块?

Gwt Maven multimodule. How to configure dependencies to put Request Factory Proxies in shared module?

Gwt 项目由 3 部分组成:clientsharedserver

我想用 maven 创建那个结构,但是对于 gwt 应用程序的每个部分我都想有单独的模块。

所以我有一个多模块 maven 项目。它由 4 个模块组成:

  1. 父模块
  2. 网络模块
  3. 共享模块
  4. 服务器模块

我想将 Request Factory Proxies 接口放在 shared 模块中。我该怎么做?

我当前的 pom.xml 配置不允许我这样做。我收到错误,即:

Classes from server module cannot be resolved to a type


这是我的配置:


1.父模块 没有任何 classes 或 GWT 配置。 它只有 pom.xml。它聚集了其余的模块。这是 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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <modules>
        <module>server</module>
        <module>shared</module>
        <module>web</module>
    </modules>

    <dependencies>
    </dependencies>


    <build>
    </build>
</project>

2。 Web 模块pom.xmlgwt.xml 配置。 xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<module rename-to='parent'>
    <inherits name='com.google.gwt.user.User' />
    <inherits name='com.google.web.bindery.requestfactory.RequestFactory' />

    <!-- Application module inherits -->
    <inherits name="pl.derp.shared" />
    <inherits name="pl.derp.server" />

    <!-- Specify the app entry point class. -->
    <entry-point class='pl.derp.web.Parent2' />

    <!-- Specify the paths for translatable code -->
    <source path='web' />
</module>

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>

    <parent>
        <groupId>pl.derp</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>web</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>pl.derp</groupId>
            <artifactId>shared</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>pl.derp</groupId>
            <artifactId>server</artifactId>
            <version>${project.version}</version>
        </dependency> 
    </dependencies>

    <build>
    </build>
</project>

3。共享模块pom.xmlgwt.xml 配置。 xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<module rename-to='shared'>
    <inherits name="com.google.gwt.user.User"/>

    <source path="web"/>
    <source path="shared"/>
</module>

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>pl.derp</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>shared</artifactId>
    <packaging>jar</packaging>
    <dependencies>
    </dependencies>
        <build>
        </build>
    </project>

4.服务器模块 pom.xmlgwt.xml 配置。 xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<module rename-to='server'>
    <inherits name="com.google.gwt.user.User"/>

    <inherits name="pl.derp.shared"/>

    <source path="server"/>
</module>

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>pl.derp</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>server</artifactId>
    <packaging>jar</packaging>

    <properties>
    </properties>

    <dependencies>
        <dependency>
            <groupId>pl.derp</groupId>
            <artifactId>shared</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>


    <build>
    </build>
</project>

请帮助我。


编辑

新的 Maven 依赖项命题:

父模块

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>
  <artifactId>mrf</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>

  <modules>
    <module>mrf-domain</module>
    <module>mrf-server</module>
    <module>mrf-shared</module>
    <module>mrf-client</module>
  </modules>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <mavenVersion>3.0</mavenVersion>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt</artifactId>
        <version>2.7.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>net.ltgt.gwt.maven</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <inherited>false</inherited>
        <configuration>
          <launcherDir>${project.build.directory}/gwt/launcherDir</launcherDir>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.2</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>9.2.3.v20140905</version>
        </plugin>
        <plugin>
          <groupId>net.ltgt.gwt.maven</groupId>
          <artifactId>gwt-maven-plugin</artifactId>
          <version>1.0-rc-2</version>
          <extensions>true</extensions>
          <configuration>
            <sourceLevel>1.7</sourceLevel>
            <failOnError>true</failOnError>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-source-plugin</artifactId>
          <version>2.4</version>
          <executions>
            <execution>
              <id>attach-sources</id>
              <phase>package</phase>
              <goals>
                <goal>jar-no-fork</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat6-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com</groupId>
    <artifactId>mrf</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>mrf-domain</artifactId>

  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>


  <build>
    <plugins>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

服务器:

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com</groupId>
    <artifactId>mrf</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>mrf-server</artifactId>
  <packaging>war</packaging>

  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>

  <dependencies>


    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mrf-shared</artifactId>
      <version>${project.version}</version>
    </dependency>


    <dependency>
      <groupId>com.google.web.bindery</groupId>
      <artifactId>requestfactory-apt</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.web.bindery</groupId>
      <artifactId>requestfactory-server</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>process-classes</phase>
            <id>requestfactory-validation-tool</id>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>java</executable>
              <classpathScope>compile</classpathScope>
              <arguments>
                <argument>-Dverbose=true</argument>
                <argument>-cp</argument>
                <classpath />
                <argument>com.google.web.bindery.requestfactory.apt.ValidationTool</argument>
                <argument>${project.build.outputDirectory}</argument>
                <argument>pl.AppFactory</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <versionRange>[1.2,)</versionRange>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <profiles>
    <profile>
      <!-- XXX: We want to exclude mrf-client from 'env-dev' profile, Maven forces us to make a 'env-prod' profile -->
      <id>env-prod</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>mrf-client</artifactId>
          <version>${project.version}</version>
          <type>war</type>
          <scope>runtime</scope>
        </dependency>
      </dependencies>
    </profile>
    <profile>
      <id>env-dev</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
      </activation>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <configuration>
                <scanIntervalSeconds>1</scanIntervalSeconds>
                <webApp>
                  <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">
                    <resourcesAsCSV>src/main/webapp,${basedir}/../target/gwt/launcherDir/</resourcesAsCSV>
                  </baseResource>
                  <extraClasspath>${basedir}/../mrf-shared/target/classes/</extraClasspath>
                  <extraClasspath>${basedir}/../mrf-domain/target/classes/</extraClasspath>
                </webApp>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat6-maven-plugin</artifactId>
              <configuration>
                <addWarDependenciesInClassloader>false</addWarDependenciesInClassloader>
                <contextFile>${basedir}/src/main/tomcatconf/context.xml</contextFile>
                <path>/</path>
                <uriEncoding>UTF-8</uriEncoding>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <configuration>
                <addWarDependenciesInClassloader>false</addWarDependenciesInClassloader>
                <contextFile>${basedir}/src/main/tomcatconf/context.xml</contextFile>
                <path>/</path>
                <uriEncoding>UTF-8</uriEncoding>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </profile>
  </profiles>
</project>

共享

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com</groupId>
    <artifactId>mrf</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>mrf-shared</artifactId>

  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mrf-domain</artifactId>
      <version>${project.version}</version>
    </dependency>


    <dependency>
      <groupId>com.google.web.bindery</groupId>
      <artifactId>requestfactory-apt</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.web.bindery</groupId>
      <artifactId>requestfactory-server</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.web.bindery</groupId>
      <artifactId>requestfactory-client</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

客户

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com</groupId>
    <artifactId>mrf</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>mrf-client</artifactId>
  <packaging>gwt-app</packaging>

  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mrf-shared</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mrf-shared</artifactId>
      <version>${project.version}</version>
      <classifier>sources</classifier>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-dev</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-codeserver</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>net.ltgt.gwt.maven</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <configuration>
          <moduleName>pl.App</moduleName>
          <moduleShortName>mrf</moduleShortName>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

不幸的是,当我尝试在 proxy class 中使用时: @ProxyFor(pl.domain.GreetingResponse2.class)

我收到一个错误:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] mrf ............................................... SUCCESS [0.106s]
[INFO] mrf-domain ........................................ SUCCESS [0.893s]
[INFO] mrf-shared ........................................ SUCCESS [0.277s]
[INFO] mrf-client ........................................ SUCCESS [6.519s]
[INFO] mrf-server ........................................ FAILURE [0.599s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.824s
[INFO] Finished at: Mon Oct 05 13:33:27 CEST 2015
[INFO] Final Memory: 28M/311M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (requestfactory-validation-tool) on project mrf-server: Command execution failed. Process exited with an error: 255 (Exit value: 255) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (requestfactory-validation-tool) on project mrf-server: Command execution failed.

当我改为使用:@ProxyForName("pl.server.GreetingResponse") 时,编译正常。请帮助。

您可以让 shared(和 client)依赖于 server,这样您就可以从 @ProxyFor 和 [= 引用服务器端 类 14=];但是你会冒着从共享和客户端代码中引用服务器端 类 的风险,并且只有在使用 GWT 编译器或开发模式(即延迟)时才会注意到它,因此在使用多模块 Maven 项目时绝对没有任何好处.

或者您可以使用 @ProxyForName@ServiceName,通过隐含的方式减轻 sharedserver 的依赖;这样 client 只依赖于 shared 而不依赖于 server.
您可以在 https://github.com/tbroyer/gwt-maven-archetypes

modular-requestfactory Maven 原型中看到该方法的示例

顺便说一句,我认为 server+shared+client 三重奏是一个单一的 "unit"(只是人为地分为 3 层以适应 Maven 限制 wrt 依赖范围之类的),我只是让它们共享一个公共根包(有或没有 serversharedclient 子包)。这样我就可以在 client 模块中有一个 *.gwt.xml,从而在 GWT 的 "source path" 中引入客户端和共享文件; shared 模块可以对 GWT 有零依赖(只有 providedrequestfactory-clientrequestfactory-server 的依赖;client 将使用 gwt-userserver 将使用 requestfactory-servergwt-servlets).

你必须尊重maven结构。 在具有每个模块的 pom 的目录中,您必须具有目录 /src,您必须在其中保存源代码。

Maven 在构建模块时查找此目录。 在这里你可以阅读更多关于这个 https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

但我不建议您更改 GWT 应用程序的标准结构。

想想那些想要进一步处理您的代码的开发人员。他们会期待标准,一开始很难理解结构。