eclipse 引用另一个 Java 项目的 Java Web 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1691553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:07:36  来源:igfitidea点击:

Java Web Project referencing another Java project

javaeclipsejakarta-ee

提问by Jo?o Silva

I have a Java Project, for which I'm now creating a Web interface, using a Dynamic Web Projectfrom Eclipse. The Web project consists of a single servletand two JSP's. Something like this:

我有一个Java Project,我现在正在为它创建一个 Web 界面,使用Dynamic Web Project来自 Eclipse 的 。Web 项目由一个servlet和两个组成JSP。像这样的东西:

/JavaApplication
  /src
  /lib
  /resources
/WebApplication
  /src
    /Servlet.java
  /WebContent
    /WEB-INF
    index.jsp
    other.jsp

Now, I need to reference JavaApplicationfrom WebApplication, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jarof the JavaApplication, containing all the .classfiles, /resources, and /libs. In this way, I could include the .jar in the web application, and I could have a single .warfile that contained the entire application.

现在,我需要引用JavaApplicationfrom WebApplication,以便使用它的类来处理 Web 请求。完成此任务的最佳方法是什么?我的想法是创建.jarJavaApplication,包含所有.class文件,/resources以及/libs。通过这种方式,我可以在 Web 应用程序中包含 .jar,并且我可以拥有.war包含整个应用程序的单个文件。

What do you think? How is this problem typically solved ?

你怎么认为?这个问题通常是如何解决的?

Note: I don't want to convert the Java Project into a Web project.

注意:我不想将 Java 项目转换为 Web 项目。

回答by BalusC

In Eclipse project properties, add the project to the Java EE Module Dependencies(Eclipse 3.5 or older)

在 Eclipse 项目属性中,将项目添加到Java EE 模块依赖项(Eclipse 3.5 或更早版本)

Java EE Module Dependencies

Java EE 模块依赖项

or Deployment Assembly(Eclipse 3.6 or newer) entry in the project properties.

或项目属性中的部署程序集(Eclipse 3.6 或更高版本)条目。

Deployment Assembly

Deployment Assembly

This way Eclipse will take care about doing the right thing to create a WAR out of this all (it will end in /WEB-INF/lib). No other configuration is necessary, even not some fiddling in Build Path.

这样 Eclipse 将负责做正确的事情来创建一个 WAR(它将以 结束/WEB-INF/lib)。不需要其他配置,甚至不需要在Build Path 中进行一些摆弄。

回答by Pascal Thivent

Under Eclipse, you can declare Project Referencesfor a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project Referencesand select the JavaApplicationproject. This should allow you to call code from the JavaApplicationproject from the WebApplicationwithout having to build a WAR. This is a solution for development.

在 Eclipse 下,您可以为给定的项目(您的案例中的 Web 应用程序)声明项目引用。为此,右键单击您的 Web 应用程序项目,然后转到“属性”>“项目引用”并选择该JavaApplication项目。这应该允许您从JavaApplication项目中调用代码WebApplication而无需构建 WAR。这是一个开发解决方案。

For standard deployment(outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplicationas a JAR including the .classfiles and the files under /resourcesbut notthe libraries it depends on (JARs under /lib). These dependencies will actually end up in the WEB-INF/libdirectory of the WAR, beside the JAR of your JavaApplication. These steps are typically automated with tools like Antor Maven.

对于标准部署(在 IDE 之外),您确实应该创建一个标准的 WAR。为此,您必须将您JavaApplication.class文件打包为 JAR,包括文件和其下的文件,/resources包括它所依赖的库( 下的 JAR /lib)。这些依赖项实际上最终会出现在WEB-INF/libWAR 目录中,在 .jar 文件的 JAR 旁边JavaApplication。这些步骤通常使用AntMaven等工具自动执行。

回答by Megha Raina

Connecting java app to web app for development :

将 Java 应用程序连接到 Web 应用程序进行开发:

right click on web project :

右键单击 web 项目:

properties>project references> add the java project you want to refer

属性>项目引用>添加你要引用的java项目

Now in properties tab of web project go to

现在在 web 项目的属性选项卡中转到

properties>deployment assembly> add the project manually and run the app

属性>部署程序集>手动添加项目并运行应用程序

回答by Thorbj?rn Ravn Andersen

Consider moving up to EAR level, if your web container supports that.

如果您的 Web 容器支持,请考虑向上移动到 EAR 级别。

The tricky part with shared code is where should the common code be put. A copy pr web application? A copy in the web container? Overdoing the "share these classes" might end up in class loader problems.

共享代码的棘手部分是公共代码应该放在哪里。复制公关网络应用程序?Web 容器中的副本?过度“共享这些类”可能会导致类加载器问题。

If you are creating two separateweb applications refactor common java code into a separate Eclipse project and refer to it from both WAR projects.

如果您正在创建两个单独的Web 应用程序,请将公共 java 代码重构为一个单独的 Eclipse 项目,并从两个 WAR 项目中引用它。



EDIT: Apparently I have misread the problem description, and thought you asked about an existing and a new web application sharing code.

编辑:显然我误读了问题描述,并认为您询问了现有和新的 Web 应用程序共享代码。

If you have an Eclipse project with your application, and another with your web frontend, then you can let your application export the necessary resources which the "Export WAR" in Eclipse Java EE can wrap up in a jar file and put in WEB-INF/lib for you. You need to say this explicitly with a checkmark in Properties -> Java EE Module Dependencies for your web project. Expect you have to experiment a bit - this took me a while to learn.

如果您的应用程序有一个 Eclipse 项目,而您的 Web 前端有另一个项目,那么您可以让您的应用程序导出必要的资源,Eclipse Java EE 中的“导出 WAR”可以将这些资源包装在一个 jar 文件中并放入 WEB-INF /lib 给你。您需要在 Web 项目的 Properties -> Java EE Module Dependencies 中使用复选标记明确说明这一点。预计您必须进行一些实验 - 这花了我一段时间来学习。

回答by Antz

Typically you would create an API interface using remote service beans from the Java application that expose the methods that you want to invoke in the web application. You would include a proxy of the API interface with your web application that calls the remote service bean in the Java application. Remember that you will need to register the remote bean in the web.xml file.

通常,您将使用来自 Java 应用程序的远程服务 bean 创建 API 接口,这些 bean 公开您要在 Web 应用程序中调用的方法。您将在 Web 应用程序中包含 API 接口的代理,该应用程序调用 Java 应用程序中的远程服务 bean。请记住,您需要在 web.xml 文件中注册远程 bean。