如何在Eclipse中正确地部署和构建项目以Tomcat? [英] How do I properly deploy and structure projects in Eclipse against Tomcat?

查看:129
本文介绍了如何在Eclipse中正确地部署和构建项目以Tomcat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对较新的Java开发人员,被抛在了最后端,我通常的技巧在于微软的产品和C#。



然而,我已经设法写了一个很好的Web服务使用Restlet并并入Toplink和做一些数据库CRUD的东西等。



我在Eclipse中针对Tomcat 5.5开发了这个。我遵循教程和例子,并设法扔东西在一起工作。



我的项目的WebContent / WEB-INF / lib目录中有我的所有jar文件 - 我很快意识到,当我将它导出为WAR文件并部署在Tomcat ,它需要这些jar文件,并将它们存储在应用程序中。



很好,但是当您尝试取消部署应用程序时,它仅部分作为Tomcat持有到其使用的某些jar,即Oracle JDBC和Toplink。这是有道理的,因为Web服务正在使用这些jar,因为有一个活的Oracle连接继续。



所以我以为我应该把所有这些罐子放在一个共同的地方所有部署的应用程序都可以访问它们,该文件夹我相信:



C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib



(在我的情况下)。这是完全有道理的,因为您不希望Tomcat上的所有应用程序都加载相同的jar,这是合乎逻辑的。所以我删除了我的项目lib文件夹中的所有jar,并将它们放在common\lib目录中。



现在我的应用程序不能正常工作 - 它不能在src中找到我的源代码,这是我针对这些jar编写的定制定制代码。它只会在我的源代码被加载并放在上面列出的目录中。奇怪的是,

我显然非常混淆类路径,并构建路径和所有这些东西,并且必须有这样的东西都错了,因为我不是Java专家,因为我很容易承认我已经相当多地将这个黑客入侵了 - 所以任何人都可以用外行人的方式向我解释如何构建我的项目,以使它与Tomcat中的一个公用文件夹中保存的jar一起工作。或者有什么好的资源在网上帮助解释我应该做什么。



希望这一切都有道理...



以下是我当前项目的图片:



解决方案

听起来你没有使用WTP Web项目。



如果您已经安装了Eclipse IDE for Java EE开发人员Eclipse的风格,那么WTP已经捆绑在其中。否则,其更新站点 - > http://download.eclipse.org/webtools/updates



安装WTP后,您应该创建动态Web项目。
一般来说,我将建议仅将您的表示层放在这里(JSP,CSS,HTML),并将所有纯java项目放入标准的Java项目中,以后您将添加为Dynamic Web Project的依赖项。 / p>

WTP可以在多个容器下运行您的Web应用程序,幸运的是支持Tomcat。
您将需要通过Window-> Preferences-> Server-> Runtime Environments进行配置。



一旦运行环境配置完成,您可以创建服务器运行时


  1. 右键单击服务器视图。

  2. 选择新建 - >服务器

  3. 选择服务器类型:Tomcat v.5.5服务器

  4. (可选)将服务器名称更改为任何适合您的信息

  5. 选择服务器运行时环境:这是您以前配置的运行时环境

  6. 点击下一步按钮

  7. 添加您的动态Web 项目项目为配置项目:面板

  8. 点击完成按钮

之后服务器已配置,您只需运行它,或者您可以将其置于调试模式。依赖项目引用的所有源代码将可用于调试。



注意:
从个人经验来看,我不建议使用common / lib。将您的Web应用程序所依赖的所有jar放入WAR文件中。如果您担心依赖关系跟踪,则开始查找Maven和m2eclipse。


I am relatively new Java developer that's been thrown in the deep end, my usual skillset lies in Microsoft products and C#.

However I have managed to write a nice web service using Restlet and incorporating Toplink and doing some database CRUD stuff, etc etc.

I developed this in Eclipse against Tomcat 5.5. I followed tutorials and examples and managed to throw something together that works.

I had all my jar files in WebContent/WEB-INF/lib directory of my project - I soon realised that when I export this as a WAR file and deploy in Tomcat, it takes these jar files with it and stores them local to the application.

Fine, but then when you try to undeploy the app, it only does it partially as Tomcat "holds on" to some of the jars it was using i.e. the Oracle JDBC, and Toplink ones. This makes sense as the web service was using these jars as there was a live Oracle connnection going on.

So then I thought I should have all these jars in a common place where all deployed apps can access them, that folder I believe is:

C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib

(in my case). This made total sense, it's logical as you don't want loads of apps on Tomcat all referencing the same jars. So I removed all the jars out of my projects lib folder and put them in the common\lib directory.

Now my app will not work properly - it can't find my source code in src, the custom bespoke code that I have programmed against these jars. It will only work if I jar up my source code and put in the directory stated above. Odd.

I am obviously very confused with class paths and build paths and all of that stuff, and must have got this sort of stuff all wrong as I am no Java expert, as I readily admit I have pretty much hacked this together - so can anyone explain to me in laymans terms how I should structure my project to get it working with jars held in a common folder in Tomcat. Or are there any good resoureces on the web to help explain to me what I should do.

Hope this all makes sense...

Here is a pic of my current project:

解决方案

Sounds like you are not using WTP web project.

If you've installed 'Eclipse IDE for Java EE Developers' flavor of Eclipse, the WTP is already bundled in there. Otherwise here its update site -> http://download.eclipse.org/webtools/updates

Once you've installed WTP you should create 'Dynamic Web Project'. In general I would recomment to put only your presentation layer here ( JSPs, CSS, HTML ) and put all pure java projects into standard 'Java' project that you later add as a dependency to 'Dynamic Web Project'.

WTP can run your web application under a number of containers, fortunately Tomcat is supported. You will need to configure it through Window->Preferences->Server->Runtime Environments.

Once runtime envirnonment is configured, you can create your server runtime:

  1. Right click in 'Servers' view.
  2. Choose New->Server
  3. Select server type: 'Tomcat v.5.5 Server'
  4. (Optional) Change server name to whatever makes sense for you
  5. Select server runtime environment: This is the Runtime Environemtn that you have configured previously
  6. Hit 'Next' button
  7. Add your 'Dynamic Web Project' project to 'Configured projects:' panel
  8. Hit 'Finish' button

After the server is configured you can just run it, or you can put it in Debug mode. All source code referenced by dependent projects will be available for debugging.

NOTE: From personal experience, I would not recommend using common/lib. Put all the jars that your web application relies upon into its WAR file. If you are worried about dependency tracking then start looking into Maven and m2eclipse.

这篇关于如何在Eclipse中正确地部署和构建项目以Tomcat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆