我正在使用 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?
发布评论
评论(10)
m2eclipse 支持这个。 首先,将路径添加到构建路径:
其次,将对此的支持添加到 m2e:
如果您的 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:
Second, add support for that to m2e:
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.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.
您应该在项目资源管理器中看到一个名为“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.
就我个人而言,我通过将生成的类设置为单独的项目并使其成为我的主(非生成)项目中的依赖项来解决此问题。 我使用 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.
我在使用 Maven 和 wsdl2java 生成的代码时遇到了这个问题,下面是我在 Eclipse Juno 中解决该问题的方法。 假设我的项目名为project1:
作为添加的另外你还可以附上源代码:
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:
As an added bonus you can also attach the source code:
通常,我会将 m2e 生命周期映射插件添加到 pom.xml 文件中,如 @koppor 的答案中所述。 然而,将 per-eclipse 代码添加到我的 pom.xml 文件中并不是一个在工作中的选项,这主要是一个 IntelliJ 商店。
我的解决方案首先将
build-helper-maven-plugin
添加到 pom.xml 中,它可以在命令行中正常工作,但在 Eclipse 中则不行。为了修复 Eclipse,我从 Eclipse Marketplace 安装了 Apt M2E Connector。 我认为在我重新启动并重建我的所有项目后一切就开始工作了。 我现在在我的源目录中看到以下内容:
功能!
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.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:
Feature!
您是否尝试刷新 Eclipse 项目?
)
(来源: oyvindhauge.com 外部工具生成新文件或更新旧文件,Eclipse 将无法检测到更改,直到下一个请求。
另一种选择是定义一个新的自定义构建器,指定该构建器“完成后刷新资源”:
替代文本 http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
Did you try to refresh the Eclipse project?
(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
为我工作(但你每次都必须遵循这一点,以便你可以在 pom.xml 中添加此路径)
Worked for me (But you will to have to follow this every time so you can add this path in pom.xml)
要从
.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
:一些附加说明:
如果
protoc
可执行文件位于 PATH 中,则 < code>protocExecutable 配置条目可以省略仅测试protobuf 消息定义可以放入项目的
src/test/proto
目录祝你好运!
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 systemupdate 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
directoryupdate the project (via
Maven -> Update project...
)Example
pom.xml
:Some additional notes:
if
protoc
executable is in the PATH theprotocExecutable
configuration entry can be omittedtest-only protobuf message definitions can be put into project's
src/test/proto
directoryI recommend installing Protocol Buffer Descriptor Editor (marketplace link)
Good luck!