如何在 Eclipse 中启动 Java EE 6 项目

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

How to start a Java EE 6 project in Eclipse

javaeclipsejakarta-eemaven

提问by John Nash

I have recently begun learning Java EE 6 after doing Java SE for some years now (together with other languages). I have bought different books about Maven, EJB, JSF, Glassfish and so on (not read them all yet though), and I feel absolutely overwhelmed of the complexity of just making a project.

在使用 Java SE 几年后(连同其他语言),我最近开始学习 Java EE 6。我已经购买了关于 Maven、EJB、JSF、Glassfish 等的不同书籍(虽然还没有全部读完),我对制作项目的复杂性感到绝对不知所措。

How would I start a project from "scratch" (Not copying a from an example in the book) in eclipse? I can choose from Dynamic Web project, Maven project, EJB project, EAR application etc. Which should I use if I want to make an application that has EJB/JPA and JSF inside (web application)?

我将如何在 eclipse 中从“从头开始”(不是从书中的一个例子中复制一个)开始一个项目?我可以从动态 Web 项目、Maven 项目、EJB 项目、EAR 应用程序等中进行选择。如果我想制作一个包含 EJB/JPA 和 JSF 的应用程序(Web 应用程序),我应该使用哪个?

I know about the Standard Maven Directory Layout and archetypes, however I do not know which to choose if this is the way to do it. Of course, I understand that there are many ways of doing it and it is dependent on what I will use it for, but I am learning Java EE 6 so I want to at least have some knowledge of how to set up a basic project as well.

我知道标准 Maven 目录布局和原型,但是如果这是这样做的方式,我不知道选择哪个。当然,我知道有很多方法可以做到这一点,这取决于我将使用它的目的,但我正在学习 Java EE 6,所以我想至少了解一些如何设置基本项目的知识好。

I am also rather overwhelmed by the POM file. How do I know which dependencies I need just to get started with a web application? Or is this included in the correct archetype (if I find one?)

我也对 POM 文件感到不知所措。我如何知道刚开始使用 Web 应用程序需要哪些依赖项?或者这是否包含在正确的原型中(如果我找到了?)

I am using Glassfish as application server, Eclipse, EJB, JPA and JSF. Currently learning from book examples only.

我使用 Glassfish 作为应用服务器、Eclipse、EJB、JPA 和 JSF。目前仅从书籍示例中学习。

回答by Shivan Dragon

Well, a complete answer to your question would pretty much need an entire book on it's own, but here's some pointers.

好吧,您的问题的完整答案几乎需要一整本书,但这里有一些提示。

  • The notion of Java EE applications represents any kind of Java application that uses one or more of the Java EE APIs implementation and needs a Java EE application server to run. Java APIs are those such: Servlet,JSP, EJB, JPA, etc. Implementations of these APIs are usually provided by the application Server (like Tomcat has servlet and JSP implementations, Glassfish has those as well plus EJB, etc). There are, let's say non-application server Java EE APIs implementations like Hibernate's implementation of JPA. So in short if you use some of these APIs implementations in your app and deploy it into some application server like Tomcat, Glassfish, JBoss AS, etc, you have a Java EE application.

    • I've told you all this because in reality, most of the project management tools (like Eclipse, Maven, etc) use these subdivisions like Web-Application, EJB Module, Dynamic Web Project etc, as a custom denomination that establishes the ball park of what set of Java EE APIs your application will use. I.E. a Web Application or Dynamic Web Project will more likely use Servlet and JSP APIs, an EJB Module will (duh) use the EJB APIs and so on. These are, as I said, ball park denominations. A Dynamic Web Application can use EJB or JPA as well, it's not like it's written in stone.

    • When you use either of these tools such as Eclipse or Maven, to start one of these above mentioned, denominated projects, such as a Dynamic Web Project in Elcipse or a Standard Web Application with Maven, that respective tool will add the common sense dependencies to your template project (like Servlet and JSP in this case), and then let you handle it from here.

    • The idea here is to understand each type of template of your chosen tool before you use it at the start of your project. Like if you decide to use Eclipse and start with a Dynamic Web Project, you should check out what exactly Eclipse asks of you and does when you do that (it will ask you for the location of a locally installed Java EE application server from where it will pull in such dependencies as a Servlet Implemnentation and a JSP implementation amongs others). Or if you chose Maven, and start a standard web project, you should check out what dependencies it adds to your project via the pom.xml file, what the basic functions of THAT pom.xml file are, etc. You should also check out how each of these tools deploys your application when you tell it to do that, does it use some hot deploy feature of your Java EE server, does it need credentials to access that server first etc.

  • Java EE 应用程序的概念代表任何类型的 Java 应用程序,它使用一个或多个 Java EE API 实现并且需要 Java EE 应用程序服务器来运行。Java API 是那些:Servlet、JSP、EJB、JPA 等。这些 API 的实现通常由应用服务器提供(如 Tomcat 有 servlet 和 JSP 实现,Glassfish 也有这些以及 EJB 等)。假设有非应用服务器 Java EE API 实现,例如 Hibernate 的 JPA 实现。简而言之,如果您在应用程序中使用其中一些 API 实现并将其部署到某些应用程序服务器(如 Tomcat、Glassfish、JBoss AS 等)中,您就有了一个 Java EE 应用程序。

    • 我告诉你这一切是因为实际上,大多数项目管理工具(如 Eclipse、Maven 等)都使用这些细分,如 Web 应用程序、EJB 模块、动态 Web 项目等,作为建立球场的自定义面额您的应用程序将使用哪一组 Java EE API。IE Web 应用程序或动态 Web 项目更可能使用 Servlet 和 JSP API,EJB 模块将(废话)使用 EJB API 等等。正如我所说,这些是棒球场面额。动态 Web 应用程序也可以使用 EJB 或 JPA,这不是一成不变的。

    • 当您使用这些工具(例如 Eclipse 或 Maven)中的任何一个来启动上述指定项目之一(例如 Elcipse 中的动态 Web 项目或使用 Maven 的标准 Web 应用程序)时,相应的工具会将常识依赖项添加到您的模板项目(如本例中的 Servlet 和 JSP),然后让您从这里处理它。

    • 这里的想法是在您在项目开始时使用它之前了解您选择的工具的每种类型的模板。就像您决定使用 Eclipse 并从动态 Web 项目开始一样,您应该检查 Eclipse 究竟要求您做什么以及在您这样做时做了什么(它会询问您本地安装的 Java EE 应用程序服务器的位置)将引入诸如 Servlet 实现和 JSP 实现等依赖项)。或者如果你选择Maven,并启动一个标准的web项目,你应该通过pom.xml文件查看它给你的项目添加了哪些依赖,那个pom.xml文件的基本功能是什么等等。你也应该查看当您告诉这些工具如何部署您的应用程序时,它是否使用了 Java EE 服务器的一些热部署功能,

My brief advice is to start with the very basic example: Install a Tomcat (version 7 is recommended), start Eclipse for Java EE Development, add that Tomcat installation to your Eclipse config, start a Dynamic Web project that uses that Tomcat as Application server, and just do some basic servlets and JSP pages, then use Eclipse to deploy that sucker to tomcat and run it. Then you can branch off to more advanced stuff, like doing the same thing with Maven, integrating a Maven project with Eclipse, adding some more Java EE Stuff (like a JPA Implenentation), etc etc.

我的简要建议是从非常基本的示例开始:安装 Tomcat(推荐版本 7),启动 Eclipse for Java EE Development,将该 Tomcat 安装添加到您的 Eclipse 配置中,启动一个使用该 Tomcat 作为应用程序服务器的动态 Web 项目,然后只做一些基本的 servlet 和 JSP 页面,然后使用 Eclipse 将那个傻瓜部署到 tomcat 并运行它。然后你可以分支到更高级的东西,比如用 Maven 做同样的事情,将 Maven 项目与 Eclipse 集成,添加更多 Java EE 东西(如 JPA 实现)等。

回答by Gonzalo Garcia Lasurtegui

I can choose from Dynamic Web project, Maven project, EJB project, EAR application etc. Which should I use if I want to make an application that has EJB/JPA and JSF inside (web application)?

我可以从动态 Web 项目、Maven 项目、EJB 项目、EAR 应用程序等中进行选择。如果我想制作一个包含 EJB/JPA 和 JSF 的应用程序(Web 应用程序),我应该使用哪个?

Before EJB 3.1 spec it was not possible to deploy EJBs in war files, but now, you can. So for a quick prototype you can use a war file rather than an ear file if that's all you require and you are more familiar with it. Of course you can also deploy it in an .ear file.

在 EJB 3.1 规范之前,不可能在 war 文件中部署 EJB,但现在可以了。因此,对于快速原型,您可以使用 war 文件而不是 ear 文件,如果这就是您所需要的并且您更熟悉它。当然,您也可以将其部署在 .ear 文件中。

I believe the Dynamic Web project in Eclipse generates a .war file that you can deploy in your AS.

我相信 Eclipse 中的动态 Web 项目会生成一个 .war 文件,您可以将其部署在您的 AS 中。

If you are using Maven, you can also create your project with Maven beforehand, and then import using Maven (via M2Eclipse plugin). This works pretty well for me. Then, with the Glassfish Maven plugin, you can deploy your app to Glassfish via command line.

如果您使用的是 Maven,您也可以预先使用 Maven 创建您的项目,然后使用 Maven(通过 M2Eclipse 插件)导入。这对我来说效果很好。然后,使用Glassfish Maven 插件,您可以通过命令行将您的应用程序部署到 Glassfish。

I am also rather overwhelmed by the POM file. How do I know which dependencies I need just to get started with a web application? Or is this included in the correct archetype (if I find one?)

我也对 POM 文件感到不知所措。我如何知道刚开始使用 Web 应用程序需要哪些依赖项?或者这是否包含在正确的原型中(如果我找到了?)

You need the Java EE dependencies at least in a "provided" scope as the server will probably provide these for you. This way they will be available for you in compilation mode. For instance, the one for the JBoss Java EE 5 stack is as follows (not sure about the GlassFissh dependency, but the important stuff is not to include the library in your .ear / .war in order to avoid library conflicts):

您至少需要“提供”范围内的 Java EE 依赖项,因为服务器可能会为您提供这些。这样,它们将在编译模式下可供您使用。例如,JBoss Java EE 5 堆栈如下(不确定 GlassFissh 依赖项,但重要的是不要将库包含在 .ear / .war 中以避免库冲突):

<dependency>
    <groupId>org.apache.openejb</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5.0-1</version>
    <scope>provided</scope>
</dependency>

Added: As you said, this can be overwhelming at the start, I think thisis a good resource on how to setup a Glassfish / Java EE project. Pick the bits you are interested on and good luck.

补充:正如你所说,这在开始时可能会让人不知所措,我认为是关于如何设置 Glassfish / Java EE 项目的一个很好的资源。选择您感兴趣的部分并祝您好运。

回答by Chip McCormick

The first thing I'd ask you is why you're learning so many technologies. If your purpose is getting a job, in my experience, there's a trend in companies away from Java EE and towards simpler solutions. For example, application servers are overkill for most organizations and Tomcat (or another web server) with helper technologies like Spring and standalone JMS servers replacing full-featured and much more complex Java EE servers.

我要问的第一件事是为什么要学习这么多技术。如果你的目的是找一份工作,根据我的经验,公司有一种趋势,即远离 Java EE,转向更简单的解决方案。例如,对于大多数组织和 Tomcat(或其他 Web 服务器)而言,应用程序服务器是过度使用 Spring 和独立 JMS 服务器等辅助技术来取代功能齐全且复杂得多的 Java EE 服务器。

I think you'd be better served focusing on core concepts and simpler technologies as you get started and staying away from IDEs, complex builds, EARs, etc. Instead, start with one or two technologies (e.g. a hello Tomcat app with a simple Ant build) and gradually add each technology in as you find the need. Better yet if you have a real world problem to solve, even if it doesn't pay.

我认为你最好在开始时专注于核心概念和更简单的技术,并远离 IDE、复杂的构建、EAR 等。相反,从一两种技术开始(例如,一个带有简单 Ant 的 hello Tomcat 应用程序) build) 并根据需要逐渐添加每种技术。如果你有一个现实世界的问题要解决,那就更好了,即使它不付钱。

Most companies (at least those with a technical clue) would rather hire a programmer who understands what value a technology like JSF adds and at what cost of complexity than someone with a lot of acronyms on their resume.

大多数公司(至少是那些有技术线索的公司)宁愿聘请一位了解 JSF 等技术的价值和复杂性成本的程序员,而不是简历上有很多首字母缩略词的人。

回答by Arun Gupta

http://blogs.oracle.com/arungupta/entry/web_app_development_using_javaprovides a complete explanation of how to create a Java EE 6 project using Eclipse and GlassFish.

http://blogs.oracle.com/arungupta/entry/web_app_development_using_java提供了如何使用 Eclipse 和 GlassFish 创建 Java EE 6 项目的完整说明。

Alternatively, you can also NetBeans to create a Java EE 6 project as explained at:

或者,您也可以使用 NetBeans 创建 Java EE 6 项目,如下所述:

http://blogs.oracle.com/arungupta/entry/screencast_37_java_ee_6

http://blogs.oracle.com/arungupta/entry/screencast_37_java_ee_6

回答by Sebastien Lorber

How to start a new web app project in Eclipse with Maven?

如何使用 Maven 在 Eclipse 中启动一个新的 Web 应用程序项目?

Just type "archetype:generate" and select JSF + EJB/JPA if available.

This one seems fine: 360: remote -> org.jboss.weld.archetypes:weld-jsf-jee (Weld archetype for creating a Java EE 6 application using JSF 2.0, CDI 1.0, EJB 3.1 and JPA 2.0 (persistence unit included))

The Appfuse are quite more complete and may embed stuff you will never use and that you will have to remove.

只需输入“archetype:generate”并选择 JSF + EJB/JPA(如果可用)。

这个看起来不错:360: remote -> org.jboss.weld.archetypes:weld-jsf-jee(使用 JSF 2.0、CDI 1.0、EJB 3.1 和 JPA 2.0(包括持久性单元)创建 Java EE 6 应用程序的焊接原型)

Appfuse 更加完整,可能会嵌入您永远不会使用的内容,而您必须删除这些内容。

After that mvn eclipse:eclipse to be able to load the project in eclipse.

之后 mvn eclipse:eclipse 能够在 eclipse 中加载项目。