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支持(代码完成等)?
9 个解决方案
#1
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更改目标中的类文件时,Eclipse会感到困惑。
To solve this, you must tell Eclipse to compile to a different place than Maven.
要解决这个问题,您必须告诉Eclipse编译到与Maven不同的地方。
#2
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(从目录URL中显示的xml链接)并手动从org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml中提取了片段。
#3
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”的容器,而不是通常的“Referenced库”。这意味着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.
在我的例子中,为了实现这一点,我检查了“Include Modules”并在Project-> Properties的“Maven”部分取消选中“处理资源时的Skip Maven编译器插件”。
#4
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经常变化,也能很好地工作。
#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:
我在使用Maven和wsdl2java生成的代码时遇到了这个问题,这就是我在Eclipse Juno中所做的解决方法。假设我的项目名为project1:
- Right-click project1 and select Properties
右键单击project1并选择“属性”
- Choose Java Build Path from the left and select the Libraries tab
从左侧选择Java Build Path,然后选择Libraries选项卡
- Click Add Class Folder
单击添加类文件夹
- Select the bin directory and click OK (project1/target/generated-sources/bin)
选择bin目录并单击OK(project1 / target / generated-sources / bin)
- Click OK and Refresh the project
单击“确定”并刷新项目
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
将路径设置为/ project1 / target / generated-sources / axis2 / src
- Click OK
#6
Did you try to refresh the Eclipse project?
您是否尝试刷新Eclipse项目?
alt text http://oyvindhauge.com/blog/wp-content/eclipse_refresh.png
替代文字http://oyvindhauge.com/blog/wp-content/eclipse_refresh.png
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
alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
#7
- 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)单击“确定”并刷新项目
#8
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的回答中所述。但是,在我的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,它可以从命令行运行,但不能在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 rebooted and then rebuilt all of my projects. I now see the following in my source dirs:
为了修复eclipse,我从Eclipse Marketplace安装了Apt M2E Connector。我认为事情在我重新启动然后重建我的所有项目之后就开始工作了。我现在在源代码中看到以下内容:
src/main/java
target/generated-sources
...
Feature!
#9
To generate Java source files from .proto
files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.
要从.proto文件生成Java源文件,请使用Protocol Buffers Plugin,它在eclipse Oxygen中开箱即用。
Basic usage (see here for detailed description):
基本用法(详见此处):
-
make sure that native
protoc
compiler is installed on your system确保您的系统上安装了本机protoc编译器
-
update your
pom.xml
file:更新您的pom.xml文件:
-
make sure you use at least Java 6 (Java 7+ is recommended)
确保至少使用Java 6(建议使用Java 7+)
-
add plugin invocation
添加插件调用
-
add the corresponding dependency for
com.google.protobuf:protobuf-java
为com.google.protobuf添加相应的依赖项:protobuf-java
-
-
put your .proto files inside project's
src/main/proto
directory将.proto文件放在项目的src / main / proto目录中
-
update the project (via
Maven -> Update project...
)更新项目(通过Maven - >更新项目...)
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 theprotocExecutable
configuration entry can be omitted如果protoc可执行文件在PATH中,则可以省略protocExecutable配置条目
-
test-only protobuf message definitions can be put into project's
src/test/proto
directorytest-only protobuf消息定义可以放入项目的src / test / proto目录中
-
I recommend installing Protocol Buffer Descriptor Editor (marketplace link)
我建议安装协议缓冲区描述符编辑器(市场链接)
Good luck!
#1
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更改目标中的类文件时,Eclipse会感到困惑。
To solve this, you must tell Eclipse to compile to a different place than Maven.
要解决这个问题,您必须告诉Eclipse编译到与Maven不同的地方。
#2
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(从目录URL中显示的xml链接)并手动从org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml中提取了片段。
#3
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”的容器,而不是通常的“Referenced库”。这意味着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.
在我的例子中,为了实现这一点,我检查了“Include Modules”并在Project-> Properties的“Maven”部分取消选中“处理资源时的Skip Maven编译器插件”。
#4
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经常变化,也能很好地工作。
#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:
我在使用Maven和wsdl2java生成的代码时遇到了这个问题,这就是我在Eclipse Juno中所做的解决方法。假设我的项目名为project1:
- Right-click project1 and select Properties
右键单击project1并选择“属性”
- Choose Java Build Path from the left and select the Libraries tab
从左侧选择Java Build Path,然后选择Libraries选项卡
- Click Add Class Folder
单击添加类文件夹
- Select the bin directory and click OK (project1/target/generated-sources/bin)
选择bin目录并单击OK(project1 / target / generated-sources / bin)
- Click OK and Refresh the project
单击“确定”并刷新项目
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
将路径设置为/ project1 / target / generated-sources / axis2 / src
- Click OK
#6
Did you try to refresh the Eclipse project?
您是否尝试刷新Eclipse项目?
alt text http://oyvindhauge.com/blog/wp-content/eclipse_refresh.png
替代文字http://oyvindhauge.com/blog/wp-content/eclipse_refresh.png
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
alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
#7
- 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)单击“确定”并刷新项目
#8
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的回答中所述。但是,在我的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,它可以从命令行运行,但不能在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 rebooted and then rebuilt all of my projects. I now see the following in my source dirs:
为了修复eclipse,我从Eclipse Marketplace安装了Apt M2E Connector。我认为事情在我重新启动然后重建我的所有项目之后就开始工作了。我现在在源代码中看到以下内容:
src/main/java
target/generated-sources
...
Feature!
#9
To generate Java source files from .proto
files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.
要从.proto文件生成Java源文件,请使用Protocol Buffers Plugin,它在eclipse Oxygen中开箱即用。
Basic usage (see here for detailed description):
基本用法(详见此处):
-
make sure that native
protoc
compiler is installed on your system确保您的系统上安装了本机protoc编译器
-
update your
pom.xml
file:更新您的pom.xml文件:
-
make sure you use at least Java 6 (Java 7+ is recommended)
确保至少使用Java 6(建议使用Java 7+)
-
add plugin invocation
添加插件调用
-
add the corresponding dependency for
com.google.protobuf:protobuf-java
为com.google.protobuf添加相应的依赖项:protobuf-java
-
-
put your .proto files inside project's
src/main/proto
directory将.proto文件放在项目的src / main / proto目录中
-
update the project (via
Maven -> Update project...
)更新项目(通过Maven - >更新项目...)
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 theprotocExecutable
configuration entry can be omitted如果protoc可执行文件在PATH中,则可以省略protocExecutable配置条目
-
test-only protobuf message definitions can be put into project's
src/test/proto
directorytest-only protobuf消息定义可以放入项目的src / test / proto目录中
-
I recommend installing Protocol Buffer Descriptor Editor (marketplace link)
我建议安装协议缓冲区描述符编辑器(市场链接)
Good luck!