如何让Eclipse解析使用Maven 2生成的类?

问题描述 投票:48回答:9

我正在使用Google Protocol Buffers为我的项目生成一些Java类。使用Maven 2及其“antrun”插件,这些类在编译之前新生成,输出到目标/生成源并在构建期间放入类路径。因此,从POM构建项目是没有问题的。

但是,Eclipse不知道如何解析生成的类,因为它所在的文件夹在开发过程中似乎不在IDE的类路径中。我正在使用m2eclipse并让它为我管理依赖项,所以我原本期望Maven能够解决这个问题。

如何为生成的代码获得IDE支持(代码完成等)?

eclipse maven-2 code-generation protocol-buffers
9个回答
26
投票

m2eclipse不支持这个。您必须手动将文件夹target/generated-sources添加为源文件夹。当你告诉m2eclipse“更新项目配置”时,这将被覆盖,你必须恢复它。

另外,确保Eclipse在工作区中查找更改。

但是可能存在一些问题。最终,您将遇到错误,某些类无法编译,因为某些其他类无法解析。但是,代码完成将起作用。这个问题的根本原因是,当Maven在target中更改类文件时,Eclipse会感到困惑。

要解决这个问题,您必须告诉Eclipse compile to a different place than Maven


22
投票

m2eclipse支持这个。首先,添加构建路径的路径:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

其次,增加对m2e的支持:

<pluginManagement>
    <plugins>
        <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>build-helper-maven-plugin</artifactId>
                            <versionRange>[1.0,)</versionRange>
                            <goals>
                              <goal>parse-version</goal>
                              <goal>add-source</goal>
                              <goal>maven-version</goal>
                              <goal>add-resource</goal>
                              <goal>add-test-resource</goal>
                              <goal>add-test-source</goal>
                            </goals>
                          </pluginExecutionFilter>
                          <action>
                            <execute>
                              <runOnConfiguration>true</runOnConfiguration>
                              <runOnIncremental>true</runOnIncremental>
                            </execute>
                          </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

如果您的eclipse安装已安装“org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml”插件,则可能不需要第二步。这个插件可以通过Window - > Preferences - > Maven - > Discovery获得。目前,这在Eclipse Kepler中不起作用,因此,我获取了JAR(从Catalog URL中显示的xml链接)并用手从org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml中提取了片段。


13
投票

您应该在项目资源管理器中看到的是一个名为“Maven Dependencies”的容器,而不是通常的“Referenced库”。这意味着m2eclipse正在管理您的构建路径。

在我的例子中,为了达到这个目的,我检查了“Include Modules”并在Project-> Properties的“Maven”部分取消选中“处理资源时的Skip Maven编译器插件”。


12
投票

我个人通过将生成的类设置为单独的项目并使其成为我的主(非生成)项目中的依赖项来解决此问题。我使用wsdl2java生成webservice类,所以我的子项目中的“源”是wdsl和xsds。即使wsdl经常变化,也能很好地工作。


9
投票

我使用Maven和wsdl2java生成的代码存在这个问题,这就是我在Eclipse Juno中为解决它所做的事情。假设我的项目名为project1:

  1. 右键单击project1并选择“属性”
  2. 从左侧选择Java Build Path,然后选择Libraries选项卡
  3. 单击添加类文件夹
  4. 选择bin目录并单击OK(project1 / target / generated-sources / bin)
  5. 单击“确定”并刷新项目

作为额外的奖励,您还可以附加源代码:

  1. 单击刚刚创建的新类文件夹旁边的箭头
  2. 单击“源附件”
  3. 单击“编辑”按钮
  4. 将路径设置为/ project1 / target / generated-sources / axis2 / src
  5. 单击确定

2
投票
  1. 右键单击项目,然后选择“属性”
  2. 选择Java Build Path从左侧选择Source选项卡
  3. 单击添加文件夹
  4. 选择bin目录,然后单击“确定”
  5. (project / target / generated-sources / xxxx)单击“确定”并刷新项目

2
投票

您是否尝试刷新Eclipse项目?

alt text (来源:oyvindhauge.com

当外部工具生成新文件或更新旧文件时,Eclipse将无法在下一个请求之前检测到更改。

另一种选择是定义新的“自定义”构建器,指定该构建器“在完成时刷新资源”:

alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif


0
投票

如何为生成的代码获得IDE支持(代码完成等)?

通常我会将m2e生命周期映射插件添加到pom.xml文件中,如@ koppor的回答中所述。但是,在我的pom.xml文件中添加每日食代码不是一个工作选项,主要是IntelliJ商店。

我的解决方案首先将build-helper-maven-plugin添加到pom.xml,它可以在命令行中正常工作,但不能在eclipse中工作。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

为了修复eclipse,我从Eclipse Marketplace安装了Apt M2E Connector。我认为事情在我重新启动然后重建我的所有项目之后就开始工作了。我现在在源代码中看到以下内容:

src/main/java
target/generated-sources
...

特征!


0
投票

要从.proto文件生成Java源文件,请使用Protocol Buffers Plugin,它在eclipse Oxygen中开箱即用。

基本用法(see here for detailed description):

  • 确保本机protoc编译器是installed on your system
  • 更新您的pom.xml文件: 确保至少使用Java 6(建议使用Java 7+) 添加插件调用 为com.google.protobuf:protobuf-java添加相应的依赖项
  • 将.proto文件放在项目qazxsw pmi目录中
  • 更新项目(通过src/main/proto

示例Maven -> Update project...

pom.xml

一些额外的说明:

  • 如果<project> ... <build> <plugins> <!-- Require at least Java 6 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <!-- Generate .java files from .proto definitions --> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.1</version> <configuration> <protocExecutable>/usr/local/bin/protoc</protocExecutable> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> <dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.5.1</version> </dependency> ... </dependencies> ... </project> 可执行文件在PATH中,则可以省略protoc配置条目
  • test-only protobuf消息定义可以放入项目的protocExecutable目录中
  • 我建议安装src/test/protoProtocol Buffer Descriptor Editor

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.