Eclipse/Maven 和“解决工作区项目的依赖关系”不能混合 jars 和源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16848600/
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
Eclipse/Maven and "Resolve dependencies from workspace projects" can't mix jars and source?
提问by TheCoolah
I've got what seems like a corner case for Eclipse/Maven and "Resolve dependencies from workspace projects". My project has a mix of written code and generated code, with the generated code coming from a dependency which uses JAXWS.
我有一个看起来像是 Eclipse/Maven 和“解决工作区项目的依赖关系”的角落案例。我的项目混合了编写的代码和生成的代码,生成的代码来自使用 JAXWS 的依赖项。
The problem is that if I check "Resolve dependencies", Eclipse/Maven ignores any JAR dependencies and tries to resolve everything by only looking at the workspace, which results in Eclipse showing errors like "Package/Class not found" (related to the generated code) even though the project will build fine with Maven from the command line.
问题是,如果我检查“解决依赖项”,Eclipse/Maven 会忽略任何 JAR 依赖项并尝试通过仅查看工作区来解决所有问题,这导致 Eclipse 显示“未找到包/类”之类的错误(与生成的代码),即使该项目可以从命令行使用 Maven 正常构建。
On the other hand, if I uncheck it, it resolves everything by only looking at the JARs in the Maven repository. The second option generally works, but when I do something like Ctrl-click on a class or variable, I get the Class File Editor and "Source not found", which isn't terribly useful. Also, it can get out of sync if I edit code in the IDE but don't run "maven install" after that.
另一方面,如果我取消选中它,它只会通过查看 Maven 存储库中的 JAR 来解决所有问题。第二个选项通常有效,但是当我在类或变量上按 Ctrl 键单击时,我会看到类文件编辑器和“未找到源”,这并不是很有用。此外,如果我在 IDE 中编辑代码但之后不运行“maven install”,它可能会不同步。
I suppose this is mainly an inconvenience with Eclipse but it's annoying. I am considering resolving this by modifying the Maven dependencies to build with source (or debug) but I can't necessarily do this with everything. Is the "Resolve dependencies" option intended to work exclusively one way or the other as I've described?
我想这主要是 Eclipse 带来的不便,但很烦人。我正在考虑通过修改 Maven 依赖项以使用源代码(或调试)构建来解决此问题,但我不一定能对所有内容都执行此操作。“解决依赖关系”选项是专门按照我所描述的一种方式还是另一种方式工作?
采纳答案by benzonico
You might want to have a look at the build helper maven plugin.
您可能想查看构建助手 maven 插件。
You can configure it like this :
您可以像这样配置它:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources</source>
<source>target/jaxws/wsimport/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
This will tell your eclipse maven plugin to have a look at the generated sources and include it in your project classpath.
这将告诉您的 eclipse maven 插件查看生成的源代码并将其包含在您的项目类路径中。
You can also add the generated sources manually to your classpath in eclipse. (right-click on the generated folder -> add to build path)
您还可以将生成的源手动添加到 eclipse 中的类路径中。(右键单击生成的文件夹-> 添加到构建路径)
回答by Amir Keibi
I know this is an old issue. But I encountered the same thing in Juno with an updated "m2e-wtp" plugin. So I'm answering solely for other readers' benefit.
我知道这是一个老问题。但是我在 Juno 中遇到了同样的事情,并且有一个更新的“m2e-wtp”插件。所以我只是为了其他读者的利益而回答。
This was only happening in war projects. The only thing resolved it eventually was removing the ".settings" folder under the war project's folder and restarting eclipse.
这仅发生在War项目中。最终解决的唯一问题是删除war项目文件夹下的“.settings”文件夹并重新启动eclipse。
回答by Chris Gerken
I think that since you want to reference files that only exist after a build that you somehow force the build to happen before you need the references resolved. You could cheat by just doing a build from within Eclipse. That would leave the generated source files in place ready to be referenced. I think, however, that the maven philosophy would have you move the generated code to another maven artifact entirely. That would let you separate the lifecycle of the two groups of code so that when you're ready to use Eclipse to edit the hand-coded code, references to generated classes are resolved because you've already generated that code in the build of an separate, independent module.
我认为,由于您想要引用仅在构建之后存在的文件,因此您以某种方式强制构建在您需要解析引用之前发生。您可以通过在 Eclipse 中进行构建来作弊。这将使生成的源文件准备好被引用。但是,我认为 maven 哲学会让您将生成的代码完全移到另一个 maven 工件中。这将使您能够将两组代码的生命周期分开,以便当您准备使用 Eclipse 来编辑手动编码的代码时,对生成的类的引用将得到解析,因为您已经在构建独立的独立模块。