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

发布于 2024-07-29 20:44:02 字数 308 浏览 2 评论 0 原文

我正在使用 Google Protocol Buffers 为我的项目生成一些 Java 类。 使用 Maven 2 及其“antrun”插件,这些类在编译前新鲜生成,输出到 target/ generated-sources 并在构建期间放在类路径中。 所以从 POM 构建项目是没有问题的。

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

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

I'm using Google Protocol Buffers to generate some Java classes for my project. Using Maven 2 and its "antrun" plugin, these classes are freshly generated before compile, output to target/generated-sources and put on the classpath during the build. So building the project from the POM is no problem.

However, Eclipse doesn't know how to resolve the generated class, because the folder it's in doesn't seem to be on the IDE's classpath during development. I'm using m2eclipse and have it manage dependencies for me, so I had expected Maven to take care of this.

How can I get IDE support (code completion etc.) for the generated code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

北凤男飞 2024-08-05 20:44:02

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 -> 获得 首选项-> Maven-> 发现。 目前,这在 Eclipse Kepler 中不起作用,因此,我获取了 JAR(从 目录 URL)并手动从 org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml 中提取片段。

m2eclipse supports this. First, add the path to your build path:

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

Second, add support for that to 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>

The second step might not be necessary, if your eclipse installation has installed the "org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml" plugin. This plugin is available via Window -> Preferences -> Maven -> Discovery. Currently, that does not work here at Eclipse Kepler, therefore, I fetched the JAR (linked from the xml shown in the Catalog URL) and extracted the fragments from org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml by hand.

动听の歌 2024-08-05 20:44:02

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

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

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

要解决此问题,您必须告诉 Eclipse 编译到不同的目录比 Maven 的地方

m2eclipse doesn't support this. You must manually add the folder target/generated-sources as a source folder. When you tell m2eclipse to "Update Project Configuration", this will be overwritten and you have to restore it.

Also, make sure that Eclipse looks for changes in the workspace.

There might be some issues, though. Eventually, you'll run into errors that some class can't be compiled because some other class can't be resolved. Code completion will work, though. The root cause of this issue is that Eclipse gets confused when Maven changes class files in target.

To solve this, you must tell Eclipse to compile to a different place than Maven.

无所谓啦 2024-08-05 20:44:02

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

就我而言,为了实现此目的,我在“项目”->“属性”的“Maven”部分中选中了“包含模块”并取消选中“处理资源时跳过 Maven 编译器插件”。

What you should see in your project explorer is a container named "Maven Dependencies" in place of the usual "Referenced libraries". This means m2eclipse is managing your build path.

In my case, to achieve this, I checked "Include Modules" and unchecked "Skip Maven compiler plugin when processing resources" on the "Maven" section of Project->Properties.

就此别过 2024-08-05 20:44:02

就我个人而言,我通过将生成的类设置为单独的项目并使其成为我的主(非生成)项目中的依赖项来解决此问题。 我使用 wsdl2java 生成 Web 服务类,因此我的子项目中的“源”是 wdsl 和 xsds。 即使 wsdl 定期更改也能正常工作。

Personally I resolved this problem by setting up the generated classes as a seperate project and made it a dependency in my main (non-generated) project. I was using wsdl2java to generate webservice classes so the "source" in my sub-project was the wdsl and xsds. Worked well even when the wsdl was changing regularly.

残疾 2024-08-05 20:44:02

我在使用 Maven 和 wsdl2java 生成的代码时遇到了这个问题,下面是我在 Eclipse Juno 中解决该问题的方法。 假设我的项目名为project1:

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

作为添加的另外你还可以附上源代码:

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

I had this issue with code generated using Maven and wsdl2java and here's what I did in Eclipse Juno to resolve it. Assume my project is named project1:

  1. Right-click project1 and select Properties
  2. Choose Java Build Path from the left and select the Libraries tab
  3. Click Add Class Folder
  4. Select the bin directory and click OK (project1/target/generated-sources/bin)
  5. Click OK and Refresh the project

As an added bonus you can also attach the source code:

  1. Click the arrow next to the new class folder you just created
  2. Click on Source attachment
  3. Click the Edit button
  4. Set the Path to /project1/target/generated-sources/axis2/src
  5. Click OK
尐偏执 2024-08-05 20:44:02
  1. 右键单击项目并选择属性
  2. 从左侧选择 Java 构建路径并选择源选项卡
  3. 单击添加文件夹
  4. 选择 bin 目录并单击确定
  5. (project/target/ generated-sources/xxxx) 单击确定并刷新项目
  1. Right-click project and select Properties
  2. Choose Java Build Path from the left and select the Source tab
  3. Click Add Folder
  4. Select the bin directory and click OK
  5. (project/target/generated-sources/xxxx) Click OK and Refresh the project
岁月无声 2024-08-05 20:44:02

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

通常,我会将 m2e 生命周期映射插件添加到 pom.xml 文件中,如 @koppor 的答案中所述。 然而,将 per-eclipse 代码添加到我的 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 Con​​nector。 我认为在我重新启动并重建我的所有项目后一切就开始工作了。 我现在在我的源目录中看到以下内容:

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

功能!

How can I get IDE support (code completion etc.) for the generated code?

Typically I would add the m2e lifecycle-mapping plugin to the pom.xml file as described in @koppor's answer. However adding per-eclipse code to my pom.xml files is not an option at work which is mostly an IntelliJ shop.

My solution first adds the build-helper-maven-plugin to the pom.xml which works fine from the command line but not in 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>

To fix eclipse I installed the Apt M2E Connector from the Eclipse Marketplace. I think things started working right after I restarted and then rebuilt all of my projects. I now see the following in my source dirs:

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

Feature!

﹎☆浅夏丿初晴 2024-08-05 20:44:02

您是否尝试刷新 Eclipse 项目?

替代文本

(来源: oyvindhauge.com 外部工具生成新文件或更新旧文件,Eclipse 将无法检测到更改,直到下一个请求。

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

替代文本 http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif

Did you try to refresh the Eclipse project?

alt text
(source: oyvindhauge.com)

When an external tool generate new files or updates old ones, Eclipse will not be able to detect the change until the next request.

Another option would be to define a new Custom builder, specifying for that builder to "refresh resources upon completion":

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

南街九尾狐 2024-08-05 20:44:02

为我工作(但你每次都必须遵循这一点,以便你可以在 pom.xml 中添加此路径)

  • 右键单击你的项目> 构建路径> 配置构建路径
  • 在sources标签中,单击[添加文件夹]按钮
  • 检查target/ generated-sources/annotations

在此处输入图像描述

Worked for me (But you will to have to follow this every time so you can add this path in pom.xml)

  • Right click on your project > Build Path > Configure Build Path
  • In sources tag, click on [Add Folder] button
  • Check target/generated-sources/annotations

enter image description here

蓝梦月影 2024-08-05 20:44:02

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

基本用法(请参阅此处了解详细说明):

  • make确保本机 protoc 编译器安装在您的系统上

  • 更新您的 pom.xml 文件:

    • 确保您至少使用 Java 6(建议使用 Java 7+)

    • 添加插件调用

    • com.google.protobuf:protobuf-java 添加相应的依赖项

  • 将您的 .proto 文件放入项目的 src/main/proto 目录

  • 更新项目(通过 Maven -> 更新项目...

示例 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>

一些附加说明:

  • 如果 protoc 可执行文件位于 PATH 中,则 < code>protocExecutable 配置条目可以省略

  • 仅测试protobuf 消息定义可以放入项目的 src/test/proto 目录

  • < p>我建议安装协议缓冲区描述符编辑器 (市场链接)

祝你好运!

To generate Java source files from .proto files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.

Basic usage (see here for detailed description):

  • make sure that native protoc compiler is installed on your system

  • update your pom.xml file:

    • make sure you use at least Java 6 (Java 7+ is recommended)

    • add plugin invocation

    • add the corresponding dependency for com.google.protobuf:protobuf-java

  • put your .proto files inside project's src/main/proto directory

  • update the project (via Maven -> Update project...)

Example 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>

Some additional notes:

  • if protoc executable is in the PATH the protocExecutable configuration entry can be omitted

  • test-only protobuf message definitions can be put into project's src/test/proto directory

  • I recommend installing Protocol Buffer Descriptor Editor (marketplace link)

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文