如何让 Eclipse 解析使用 Maven 2 生成的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1192596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Eclipse to resolve classes generated with Maven 2?
提问by Hanno Fietz
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.
我正在使用 Google Protocol Buffers 为我的项目生成一些 Java 类。使用 Maven 2 及其“antrun”插件,这些类在编译之前新鲜生成,输出到目标/生成源并在构建期间放在类路径上。所以从 POM 构建项目是没有问题的。
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.
但是,Eclipse 不知道如何解析生成的类,因为它所在的文件夹在开发过程中似乎不在 IDE 的类路径中。我正在使用 m2eclipse 并让它为我管理依赖项,所以我希望 Maven 来处理这个问题。
How can I get IDE support (code completion etc.) for the generated code?
如何为生成的代码获得 IDE 支持(代码完成等)?
采纳答案by Hanno Fietz
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.
您应该在项目浏览器中看到的是一个名为“Maven Dependencies”的容器,而不是通常的“引用库”。这意味着 m2eclipse 正在管理您的构建路径。
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.
就我而言,为了实现这一点,我在项目->属性的“Maven”部分选中了“包含模块”并取消选中“处理资源时跳过Maven编译器插件”。
回答by Aaron Digulla
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.
m2eclipse 不支持这个。您必须手动将该文件夹添加target/generated-sources
为源文件夹。当您告诉 m2eclipse“更新项目配置”时,这将被覆盖,您必须恢复它。
Also, make sure that Eclipse looks for changes in the workspace.
此外,请确保 Eclipse 查找工作区中的更改。
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
.
不过可能有些问题。最终,您会遇到某些类无法编译的错误,因为某些其他类无法解析。但是,代码完成将起作用。此问题的根本原因是当 Maven 更改target
.
To solve this, you must tell Eclipse to compile to a different place than Maven.
要解决此问题,您必须告诉 Eclipse编译到与 Maven 不同的位置。
回答by koppor
m2eclipse supports this. First, add the path to your build path:
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>
Second, add support for that to m2e:
其次,将对此的支持添加到 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.
如果您的 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
手动提取片段。
回答by Michael Rutherfurd
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.
我个人通过将生成的类设置为单独的项目并使其成为我的主(非生成)项目中的依赖项来解决这个问题。我使用 wsdl2java 来生成 webservice 类,所以我的子项目中的“源”是 wdsl 和 xsds。即使 wsdl 定期更改,也能正常工作。
回答by Jono
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:
我在使用 Maven 和 wsdl2java 生成的代码时遇到了这个问题,这是我在 Eclipse Juno 中为解决它所做的。假设我的项目名为 project1:
- Right-click project1 and select Properties
- Choose Java Build Path from the left and select the Libraries tab
- Click Add Class Folder
- Select the bin directory and click OK (project1/target/generated-sources/bin)
- Click OK and Refresh the project
- 右键单击 project1 并选择 Properties
- 从左侧选择 Java Build Path 并选择 Libraries 选项卡
- 单击添加类文件夹
- 选择bin目录,点击确定(project1/target/generated-sources/bin)
- 单击确定并刷新项目
As an added bonus you can also attach the source code:
作为额外的奖励,您还可以附上源代码:
- Click the arrow next to the new class folder you just created
- Click on Source attachment
- Click the Edit button
- Set the Path to /project1/target/generated-sources/axis2/src
- Click OK
- 单击您刚刚创建的新类文件夹旁边的箭头
- 点击源附件
- 单击编辑按钮
- 将路径设置为 /project1/target/generated-sources/axis2/src
- 单击确定
回答by VonC
Did you try to refresh the Eclipse project?
您是否尝试刷新 Eclipse 项目?
(source: oyvindhauge.com)
(来源: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.
当外部工具生成新文件或更新旧文件时,Eclipse 将无法检测到更改,直到下一个请求。
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
替代文字 http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
回答by Ranjith Reddy
- Right-click project and select Properties
- Choose Java Build Pathfrom the left and select the Source tab
- Click Add Folder
- Select the bin directory and click OK
- (project/target/generated-sources/xxxx) Click OK and Refresh the project
- 右键单击项目并选择属性
- 从左侧选择 Java Build Path 并选择 Source 选项卡
- 点击添加文件夹
- 选择bin目录,点击确定
- (project/target/generated-sources/xxxx) 点击 OK 并刷新项目
回答by Gray
How can I get IDE support (code completion etc.) for the generated code?
如何为生成的代码获得 IDE 支持(代码完成等)?
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.
通常,我会将 m2e 生命周期映射插件添加到 pom.xml 文件中,如@koppor 的回答中所述。然而,将 per-eclipse 代码添加到我的 pom.xml 文件中并不是一个主要是 IntelliJ 商店的工作选项。
My solution first adds the build-helper-maven-plugin
to the pom.xml which works fine from the command line but not in eclipse.
我的解决方案首先将build-helper-maven-plugin
pom.xml添加到 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>
To fix eclipse I installed the Apt M2E Connectorfrom 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:
为了修复 eclipse,我从 Eclipse Marketplace安装了Apt M2E 连接器。我认为在我重新启动然后重建我的所有项目后事情就开始工作了。我现在在我的源目录中看到以下内容:
src/main/java
target/generated-sources
...
Feature!
特征!
回答by vlp
To generate Java source files from .proto
files use Protocol Buffers Pluginwhich works out-of-the-box in eclipse Oxygen.
要从文件生成 Java 源文件,请.proto
使用协议缓冲区插件,该插件在 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...
)
确保您的系统上安装了本机
protoc
编译器更新您的
pom.xml
文件:确保您至少使用 Java 6(建议使用 Java 7+)
添加插件调用
添加对应的依赖
com.google.protobuf:protobuf-java
将您的 .proto 文件放在项目
src/main/proto
目录中更新项目(通过
Maven -> Update project...
)
Example pom.xml
:
示例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 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)
如果
protoc
可执行文件在 PATH 中,protocExecutable
则可以省略配置条目test-only protobuf 消息定义可以放入项目
src/test/proto
目录我建议安装协议缓冲区描述符编辑器(市场链接)
Good luck!
祝你好运!