Java 如何将 maven 管理的依赖项复制到 war\web-inf\lib 中,以便我可以在 Eclipse 中以调试模式运行我的 GWT 2.0 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008821/
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 maven managed dependencies copied into war\web-inf\lib so I can run my GWT 2.0 app in debug mode within Eclipse?
提问by Tony
I am updating an existing project from GWT 1.5.2 to GWT 2.0.0. We use maven 2 to manage our dependencies and do all of our development in Eclipse 3.5.
我正在将现有项目从 GWT 1.5.2 更新到 GWT 2.0.0。我们使用 maven 2 来管理我们的依赖项,并在 Eclipse 3.5 中完成我们所有的开发。
Because we use maven to manage our dependencies, I do not have all of those jars in the war\web-inf\lib directory as GWT specifies. Instead, they are in the maven repository, just where maven likes them. I have the project set up so that maven can successfully build and launch in either dev or web mode and the application runs correctly.
因为我们使用 maven 来管理我们的依赖项,所以我没有 GWT 指定的 war\web-inf\lib 目录中的所有这些 jar。相反,它们位于 maven 存储库中,就在 maven 喜欢它们的地方。我已经设置了项目,以便 maven 可以在开发模式或 Web 模式下成功构建和启动,并且应用程序可以正确运行。
The problem is that when I launch from Eclipse, I get a java.lang.NoClassDefFoundError. If I manually copy of my dependencies into war\web-inf\lib before launching, everything runs fine, but that doesn't lend itself to a long-term solution. First, if I check all of those jars into our version control, that will subvert much of the value we get from maven. As annoying as maven can be, ditching it is not the answer. Second, having developers manually copy them over every time they want to debug something is ridiculous.
问题是当我从 Eclipse 启动时,我得到一个 java.lang.NoClassDefFoundError。如果我在启动之前手动将我的依赖项复制到 war\web-inf\lib 中,一切运行良好,但这并不适合长期解决方案。首先,如果我将所有这些 jar 都检查到我们的版本控制中,这将破坏我们从 maven 获得的大部分价值。尽管 maven 很烦人,但放弃它并不是答案。其次,让开发人员每次想要调试某些东西时都手动复制它们是荒谬的。
So can I get Eclipse to copy the dependencies into war\web-inf\lib before launching? Is there an alternate solution that I'm missing?
那么我可以让 Eclipse 在启动之前将依赖项复制到 war\web-inf\lib 中吗?是否有我缺少的替代解决方案?
Thanks,
谢谢,
Tony
托尼
采纳答案by Pascal Thivent
Running the gwt:eclipse
goal will copy the maven dependencies into war/WEB-INF/lib
. See the Eclipse Configurationsection of the Eclipse IDE Integrationdocumentation of the Maven GWT pluginfor more details. Also have a look at this answerabout Maven GWT 2.0 and Eclipse.
运行gwt:eclipse
目标会将 Maven 依赖项复制到war/WEB-INF/lib
. 有关更多详细信息,请参阅Maven GWT 插件的Eclipse IDE 集成文档的Eclipse 配置部分。也看看这个关于Maven GWT 2.0 和 Eclipse 的答案。
回答by cletus
If you have the packaging method in your pom.xml set to war it should copy runtime depdencies into target/war/WEB-INF/lib.
如果您将 pom.xml 中的打包方法设置为 war,它应该将运行时依赖项复制到 target/war/WEB-INF/lib。
Or is your project to build something larger like an ear? If so you should probably split your pom.xml into multi-project format.
或者你的项目是建造像耳朵一样大的东西?如果是这样,您可能应该将 pom.xml 拆分为多项目格式。
As for Eclipse, I'm not terribly familiar with it so I can't really help you there. Are you (or can you) run a maven target (like "install") when you do a run or debug?
至于 Eclipse,我对它不是很熟悉,所以我无法真正帮助你。您在运行或调试时是否(或可以)运行 Maven 目标(如“安装”)?
回答by crowne
You should install the m2eclipse pluginand use that to build your project within eclipse. This will invoke maven as an external tool from within eclipse.
您应该安装m2eclipse 插件并使用它在 eclipse 中构建您的项目。这将从 Eclipse 中调用 maven 作为外部工具。
Your maven project artifact type should be set to war, which will let maven discover the dependencies and bundle them.
您的 maven 项目工件类型应设置为 war,这将使 maven 发现依赖项并将其捆绑。
See these links:
请参阅这些链接:
force Maven2 to copy dependencies into target/lib
http://maven.apache.org/plugins/maven-war-plugin/
http://maven.apache.org/plugins/maven-war-plugin/
http://maven.apache.org/plugins/maven-war-plugin/examples/rapid-testing-jetty6-plugin.html
http://maven.apache.org/plugins/maven-war-plugin/examples/rapid-testing-jetty6-plugin.html
回答by Yoni
This is not supposed to be a problem. If you are using Eclipse+WTP then WTP lets you declare jars listed under 'J2EE module dependency' in the project's properties. Those jars are automatically published to tomcat (see note at the end of this comment) You need to declare an M2_REPO variable in your eclipse environment (search eclipse help for 'classpath variables'), it should point to your local maven repository, and just add external jars from there to your project and mark them as 'J2EE dependencies'. I did that with tons of projects, Maven and Eclipse live side-by-side :) (without m2eclipse)
这不应该是一个问题。如果您使用 Eclipse+WTP,那么 WTP 允许您声明在项目属性中的“J2EE 模块依赖项”下列出的 jar。这些 jars 会自动发布到 tomcat(请参阅本评论末尾的注释)您需要在 eclipse 环境中声明一个 M2_REPO 变量(在 eclipse 帮助中搜索“classpath variables”),它应该指向您本地的 maven 存储库,并且只需从那里将外部 jar 添加到您的项目中,并将它们标记为“J2EE 依赖项”。我用大量的项目做到了这一点,Maven 和 Eclipse 并排:)(没有 m2eclipse)
A second option is to try to integrate Eclipse and Maven with m2eclipse plugin, as crowne mentioned, but I found this unnecessary.
第二种选择是尝试将 Eclipse 和 Maven 与 m2eclipse 插件集成,正如 crone 所提到的,但我发现这没有必要。
There is a third option, if you don't use WTP (but you should). Assuming that you use tomcat, then you can copy those jars to $(tomcat.home)/common/lib. If you can do it just for your private instance of tomcat (on each developer's machine) then you are good to go. This is supposed to be a low-impact solution that will get those jars into the classpath at runtime, but it doesn't scale well.
如果您不使用 WTP(但您应该使用),还有第三种选择。假设您使用tomcat,那么您可以将这些jar 文件复制到$(tomcat.home)/common/lib。如果您可以只为您的私有 tomcat 实例(在每个开发人员的机器上)执行此操作,那么您就可以开始了。这应该是一个低影响的解决方案,可以在运行时将这些 jars 放入类路径,但它不能很好地扩展。
note: unfortunately the classpath of Eclipse web projects can be confusing. When you add jars to the regular build path of the project, they are not deployed (copied over) to tomcat. To solve this, WTP defines a special library called Webapp Library - this library contains all the jars in the project's WEB-INF/lib folder, as well as all the external jars that are marked as J2EE Module Dependencies. All those jars find their way appropriately to WEB-INF/lib when published to tomcat.
注意:不幸的是,Eclipse Web 项目的类路径可能会令人困惑。当您将 jars 添加到项目的常规构建路径时,它们不会部署(复制)到 tomcat。为了解决这个问题,WTP 定义了一个名为 Webapp Library 的特殊库——这个库包含项目的 WEB-INF/lib 文件夹中的所有 jar,以及所有标记为 J2EE 模块依赖项的外部 jar。当发布到 tomcat 时,所有这些 jar 都会找到适合 WEB-INF/lib 的方式。
回答by manuel aldana
a maven aware IDE (idea, eclipse, netbeans) should do this packaging automatically. maybe you:
Maven 感知 IDE(idea、eclipse、netbeans)应该自动执行此打包。可能是你:
- forgot to enable maven import inside IDE?
- did not add these dependencies to the pom.xml (so they aren't included in the 'mvn package' phase)
- added wrong scope to dependency declaration (e.g. scope 'provided' or 'tested'), so they are ignored for runtime
- 忘记在 IDE 中启用 maven 导入?
- 没有将这些依赖项添加到 pom.xml (因此它们不包含在 'mvn package' 阶段)
- 向依赖声明添加了错误的范围(例如范围“提供”或“测试”),因此它们在运行时被忽略
回答by Venkatt
Quite simple: 1. Create a "lib" folder under your project like this: "\src\main\webapp\WEB-INF\lib" 2. Copy needed "jars" etc that you want included inside your WAR bundle folder. 3. Invoke your maven build as you normally do. I use "mvn install", which creates builds the war file.
很简单: 1. 在您的项目下创建一个“lib”文件夹,如下所示:“\src\main\webapp\WEB-INF\lib” 2. 复制您想要包含在 WAR 包文件夹中的所需“jars”等。3. 像往常一样调用您的 Maven 构建。我使用“mvn install”,它创建构建war文件。
If you examine the WAR file, your jars that you included in step-1 and step-2 will be there.
如果您检查 WAR 文件,您在步骤 1 和步骤 2 中包含的 jar 将在那里。
Cheers.
干杯。