【问题标题】:Not able to get reference to class file generated in "target" folder of Maven project in GWT无法获取 GWT 中 Maven 项目的“目标”文件夹中生成的类文件的引用
【发布时间】:2014-02-12 17:34:21
【问题描述】:

我正在使用 Google Gin 在我的 GWT 项目中生成一个事件,如下所示:

package com.lokur.motd.client.events;

import com.gwtplatform.dispatch.annotation.GenEvent;

@GenEvent
public class EditorChange {
}

现在,当我使用“mvn clean install -e”编译我的项目时,它会生成所需的类,即。我项目的目标文件夹中的 EditorChangeEvent.class、EditorChangeEvent$EditorChangeHandler.class、EditorChangeEvent$HasEditorChangeHandlers.class。

但是,当我尝试访问特定生成的类时,例如EditorChangeEvent 在我的项目中,它给了我编译错误消息:

"包 com.lokur.motd.client.events.EditorChangeEvent 不存在”

...

我不明白这种行为。我们是否需要在类路径或 pom.xml 中的某处显式添加 Maven 的“目标”文件夹或其他原因导致此问题?

这里是来自 maven pom.xml 的插件:

<plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>gwt-maven-plugin</artifactId>
                        <version>${gwt.plugin.version}</version>
                        <configuration>
                            <runTarget>myMessage/myMessage.html</runTarget>
                            <disableCastChecking>true</disableCastChecking>
                            <disableClassMetadata>true</disableClassMetadata>
                            <!-- <logLevel>INFO" -bindAddress 0.0.0.0 -logLevel "INFO</logLevel> -->
                            <logLevel>ERROR</logLevel>
                            <style>OBF</style>
                            <!-- OBF, PRETTY, DETAILED -->
                            <noServer>false</noServer>
                            <additionalPageParameters>log_level=OFF</additionalPageParameters>
                            <extraJvmArgs>-Xmx512m -Ddev.mode=true -DuseCache=false</extraJvmArgs>
                            <extraTestArgs>-Xmx512m -Ddev.mode=true</extraTestArgs>
                            <gwtVersion>${gwtVersion}</gwtVersion>
                            <testFilter>*</testFilter>
                            <hostedWebapp>
                                ${project.build.directory}/${project.build.finalName}
                            </hostedWebapp>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>compile</goal>
                                    <goal>test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>Unzip usp Config &amp; Env Modules</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>com.dig.configs</groupId>
                                            <artifactId>common-configs</artifactId>
                                            <version>${commonConfigs.version}</version>
                                            <type>zip</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${basedir}/target/tmp</outputDirectory>
                                        </artifactItem>
                                        <artifactItem>
                                            <groupId>${pom.groupId}</groupId>
                                            <artifactId>usp-env</artifactId>
                                            <version>${pom.version}</version>
                                            <type>zip</type>
                                            <outputDirectory>${basedir}/target/tmp</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>Copy Config, Env Modules</id>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <tasks>
                                        <copy todir="${basedir}/target/myMessage/WEB-INF/classes/config" overwrite="true" verbose="true" failonerror="true">
                                            <fileset dir="${basedir}/target/tmp/config">
                                                <include name="**" />
                                            </fileset>
                                            <fileset dir="${basedir}/target/tmp/env">
                                                <include name="**" />
                                            </fileset>
                                        </copy>
                                        <copy todir="${basedir}/target/myMessage/WEB-INF/classes/config" overwrite="true" verbose="true" failonerror="true">
                                            <fileset dir="../config/test" includes="**" />
                                        </copy>
                                        <copy file="${basedir}/../config/usp-app-config.xml" todir="${basedir}/target/myMessage/WEB-INF/classes/config" />
                                        <copy file="${basedir}/../usp-env/src/test/env/usp-app-config.properties" todir="${basedir}/target/myMessage/WEB-INF/classes" />
                                        <copy file="${basedir}/../usp-env/src/test/env/app-config.properties" todir="${basedir}/target/myMessage/WEB-INF/classes" />
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>

【问题讨论】:

  • “尝试访问”是什么意思?你是说你的IDE吗?
  • @Nick Holt:是的。在同一个项目中访问其他类文件以创建它的实例或用于其他目的..
  • 你使用什么 Maven 插件来生成类?
  • @Nick Holt:我不是 Maven 专家。有没有这样的插件来生成类?你能举个例子吗...
  • 您说您的 Maven 构建正在生成 EditorChangeEvent 等 - 默认情况下不会这样做,通常您需要添加一个插件 - 什么插件已添加到您的 pom.xml ?

标签: java gwt intellij-idea maven-2 gwt-gin


【解决方案1】:

我对 GWT 不够熟悉,不知道是不是那个插件生成了 EditorChangeEvent 类,但我猜是。

也就是说,我对 IntelliJ 和 Maven 都非常熟悉。因此,我可以解释这个问题是如何出现的以及如何解决它。

如果您要尝试将所有内容构建为单个项目,那么您需要找到 EditorChangeEvent 类的源文件。生成类的插件通常会将这些类的源放在target/generated-sources 中,但在 GWT 的情况下我不能肯定地说(有时它是插件上的一个选项来指示是否保留源)。如果是这种情况,插件应该已经向 Maven 项目添加了一个编译源根。 IntelliJ 将识别这一点并将target/generated-sources 目录标记为IntelliJ 项目中的源根。如果target/generated-sources 目录存在但没有被自动标记为源根目录,您可以通过右键单击该目录并选择将目录标记为来做到这一点。 p>

另一种可能性是源永远不存在。如果是这种情况,那么您将不得不将您的项目分成两个项目 - 一个项目包含生成的类,另一个项目包含使用生成的类的类。显然这第二个项目依赖于第一个使用Maven的依赖机制。

【讨论】:

  • 我在目标文件夹中看到了,但里面没有“生成源”文件夹。此外,在我的 IntelliJ IDE 中,我看不到“目标”文件夹。虽然它在文件系统上。
  • IntelliJ 对你隐藏了目标文件夹——如果插件在target 目录中添加了编译源根目录,它就会显示出来。鉴于它不存在,我认为您将不得不拆分您的项目。
  • 在此处添加了另一个相关问题:stackoverflow.com/questions/21326341/…
【解决方案2】:

http://mojo.codehaus.org/gwt-maven-plugin/compile-mojo.html

生成目录

将创建生成源的文件夹(自动添加到 编译类路径)。 ${project.build.directory}/generated-sources/gwt

所以 Maven 应该能够正确编译您的项目。 但是,由于 Nick 点 target/generated-sources/gwt 作为源目录包含在您的 IDE 中,您将遇到编译问题。

您需要用更具体的细节更新您的问题,因为您还不清楚目标文件夹中生成类的确切位置;或者是什么给了您编译错误(Maven 或您的 IDE - 以及您使用的是哪个 IDE)

【讨论】:

  • 我添加了以下插件:org.bsc.mavenmaven-processor-pluginorg.codehaus.mojobuild-helper-maven-plugin 所以,现在 maven 正在目标目录中生成“生成的源”文件夹。但是,它现在在 classes/target/ 以及“generated-sources”文件夹中生成相同的 Java 源文件。因此,得到编译错误。请参阅下面的第二条评论..
  • 我的意思是,正在生成两个 EditorChangeEvent.java 文件。一个在“generated-sources”文件夹中,另一个在“target/classes/<..package..>”文件夹中。因此,导致以下编译失败: .../target//events/EditorChangeEvent.java:[9,7] duplicate class: .EditorChangeEvent
  • 在此处添加了另一个相关问题:stackoverflow.com/questions/21326341/…
  • 请使用正确的信息而不是在这些 cmets 中更新您的问题。你的项目开源可以看吗?您需要应用标准调试技术来解决您的问题。首先从 Maven pom.xml 中删除所有内容,然后一次添加内容。你会发现罪魁祸首在哪里。
【解决方案3】:

问题出在我们项目支持的 GWT 版本上。 它不支持@GenEvent,因为该项目未配置为使用 Gin。因此,手动创建了 Event 和 Handler 类,而不是依赖于 Gin 的注解。现在,它运作良好。关闭此线程。 谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 2016-01-17
    • 1970-01-01
    • 2018-08-23
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多