简体   繁体   English

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

[英]How to start a Java EE 6 project in Eclipse

I have recently begun learning Java EE 6 after doing Java SE for some years now (together with other languages). 我最近在做了Java SE几年后开始学习Java EE 6(和其他语言一起)。 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. 我已经购买了关于Maven,EJB,JSF,Glassfish等的不同书籍(虽然还没有读过它们),我觉得完全不知道只是制作一个项目的复杂性。

How would I start a project from "scratch" (Not copying a from an example in the book) in eclipse? 我如何从eclipse中的“scratch”(不是从书中的一个例子中复制一个)开始一个项目? 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应用程序),我应该使用哪个?

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. 我知道标准Maven目录布局和原型,但我不知道如果这是这样做的选择。 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. 当然,我知道有很多方法可以做到这一点,这取决于我将用它做什么,但我正在学习Java EE 6,所以我想至少掌握一些如何设置基本项目的知识。好。

I am also rather overwhelmed by the POM file. 我也对POM文件感到不知所措。 How do I know which dependencies I need just to get started with a web application? 我如何知道开始使用Web应用程序时需要哪些依赖项? Or is this included in the correct archetype (if I find one?) 或者这是否包含在正确的原型中(如果我找到一个?)

I am using Glassfish as application server, Eclipse, EJB, JPA and JSF. 我使用Glassfish作为应用程序服务器,Eclipse,EJB,JPA和JSF。 Currently learning from book examples only. 目前仅从书籍示例中学习。

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 EE应用程序的概念表示使用一个或多个Java EE API实现并需要运行Java EE应用程序服务器的任何类型的Java应用程序。 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). Java API就是这样的:Servlet,JSP,EJB,JPA等。这些API的实现通常由应用程序服务器提供(类似于Tomcat具有servlet和JSP实现,Glassfish也具有这些API以及EJB等)。 There are, let's say non-application server Java EE APIs implementations like Hibernate's implementation of JPA. 有一些非应用服务器Java EE API实现,比如Hibernate的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. 简而言之,如果您在应用程序中使用其中一些API实现并将其部署到某些应用程序服务器(如Tomcat,Glassfish,JBoss AS等)中,那么您就拥有了一个Java EE应用程序。

    • 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. 我告诉过你所有这些,因为实际上,大多数项目管理工具(如Eclipse,Maven等)都使用Web-Application,EJB Module,Dynamic Web Project等这些细分作为自定义面额来建立球场。您的应用程序将使用哪些Java EE API。 IE 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. IE Web应用程序或动态Web项目更可能使用Servlet和JSP API,EJB模块将(duh)使用EJB API等。 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. 动态Web应用程序也可以使用EJB或JPA,它不像是一成不变的。

    • 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. 当您使用这些工具(如Eclipse或Maven)中的任何一个时,要启动上述命名项目之一,例如Elcipse中的Dynamic Web Project或使用Maven的标准Web应用程序,相应的工具将添加常识依赖项您的模板项目(在本例中类似于Servlet和JSP),然后让您从这里处理它。

    • 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). 就像你决定使用Eclipse并从动态Web项目开始一样,你应该看看Eclipse对你的要求以及做什么时它会做什么(它会询问你本地安装的Java EE应用服务器的位置。将引入诸如Servlet Implemnentation和JSP实现之类的依赖关系。 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. 或者如果您选择Maven并启动标准Web项目,您应该通过pom.xml文件查看它添加到项目中的依赖项,THAT 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. 我的简要建议是从最基本的示例开始:安装Tomcat(建议使用版本7),启动Eclipse for Java EE Development,将Tomcat安装添加到Eclipse配置中,启动一个使用该Tomcat作为Application服务器的Dynamic Web项目,然后只做一些基本的servlet和JSP页面,然后使用Eclipse将该sucker部署到tomcat并运行它。 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. 然后你可以分支到更高级的东西,比如用Maven做同样的事情,用Eclipse集成Maven项目,添加一些Java EE Stuff(比如JPA Implenentation)等等。

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. 在EJB 3.1规范之前,无法在war文件中部署EJB,但现在,您可以。 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. 因此,对于快速原型,您可以使用war文件而不是ear文件,如果这是您所需要的,并且您对它更熟悉。 Of course you can also deploy it in an .ear file. 当然,您也可以将其部署在.ear文件中。

I believe the Dynamic Web project in Eclipse generates a .war file that you can deploy in your AS. 我相信Eclipse中的Dynamic 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). 如果您正在使用Maven,您也可以事先使用Maven创建项目,然后使用Maven导入(通过M2Eclipse插件)。 This works pretty well for me. 这对我很有用。 Then, with the Glassfish Maven plugin , you can deploy your app to Glassfish via command line. 然后,使用Glassfish Maven插件 ,您可以通过命令行将应用程序部署到Glassfish。

I am also rather overwhelmed by the POM file. 我也对POM文件感到不知所措。 How do I know which dependencies I need just to get started with a web application? 我如何知道开始使用Web应用程序时需要哪些依赖项? Or is this included in the correct archetype (if I find one?) 或者这是否包含在正确的原型中(如果我找到一个?)

You need the Java EE dependencies at least in a "provided" scope as the server will probably provide these for you. 您至少在“提供”范围内需要Java EE依赖项,因为服务器可能会为您提供这些依赖项。 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): 例如,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 this is a good resource on how to setup a Glassfish / Java EE project. 补充:正如你所说,这在开始时可能是压倒性的,我认为是如何设置Glassfish / Java EE项目的一个很好的资源。 Pick the bits you are interested on and good luck. 选择你感兴趣的位并祝你好运。

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. 如果你的目的是找到工作,根据我的经验,公司有一种趋势,即远离Java EE并转向更简单的解决方案。 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. 例如,对于大多数组织而言,应用程序服务器都是过度的,而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 (eg a hello Tomcat app with a simple Ant build) and gradually add each technology in as you find the need. 我认为,当你开始并远离IDE,复杂的构建,EAR等时,你会更好地专注于核心概念和更简单的技术。相反,从一两种技术开始(例如一个带有简单Ant的hello Tomcat应用程序)构建)并在您发现需要时逐步添加每种技术。 Better yet if you have a real world problem to solve, even if it doesn't pay. 更好的是,如果你有一个现实世界的问题需要解决,即使它没有付出代价。

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这样的技术所带来的价值,以及复杂的成本,而不是简历上有很多首字母缩略词的人。

http://blogs.oracle.com/arungupta/entry/web_app_development_using_java provides 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

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. 只需键入“archetype:generate”并选择JSF + EJB / JPA(如果可用)。

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)) 这个看起来很好:360:远程 - > org.jboss.weld.archetypes:weld-jsf-jee(使用JSF 2.0创建Java EE 6应用程序的Weld原型,CDI 1.0,EJB 3.1和JPA 2.0(包括持久性单元) )

The Appfuse are quite more complete and may embed stuff you will never use and that you will have to remove. Appfuse非常完整,可以嵌入你永远不会使用的东西,你必须删除。

After that mvn eclipse:eclipse to be able to load the project in eclipse. 之后mvn eclipse:eclipse能够在eclipse中加载项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM