You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started-first-project.asciidoc 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ---
  2. title: Creating a Project in Eclipse
  3. order: 100
  4. layout: page
  5. ---
  6. [[getting-started.first-project]]
  7. = Creating and Running a Project in Eclipse
  8. This section gives instructions for creating a new Eclipse project using the
  9. Vaadin Plugin. The task will include the following steps:
  10. . Create a new project
  11. . Write the source code
  12. . Configure and start web server
  13. . Open a web browser to use the web application
  14. We also show how you can debug the application in the debug mode in Eclipse.
  15. This walkthrough assumes that you have already installed the Eclipse IDE, the Vaadin Plugin, and a development server, as instructed in
  16. <<dummy/../../../framework/installing/installing-eclipse#installing.eclipse, "Installing the Eclipse IDE and Plugin">>.
  17. [[getting-started.first-project.creation]]
  18. == Creating a Maven Project
  19. Let us create the first application project with the tools installed in the previous section.
  20. First, launch Eclipse and follow the following steps:
  21. . Start creating a new project by selecting from the menu "File > New > Project...".
  22. . In the [guilabel]#New Project# window that opens, select "Vaadin > Vaadin 8
  23. Project (Maven)" and click [guibutton]#Next#.
  24. +
  25. image::img/myproject-new-vaadin.png[width=70%, scaledwidth=90%]
  26. . In the [guilabel]#Select a Maven archetype# step, you need to select the project type.
  27. To create a simple test project, select the [guilabel]#Single-module Application Project#.
  28. +
  29. image::img/myproject-archetype-selection.png[width=70%, scaledwidth=90%]
  30. . In the [guilabel]#Specify archetype parameters# step, you need to give at least the [guilabel]#Group Id# and the [guilabel]#Artifact Id#.
  31. The default values should be good for the other settings.
  32. +
  33. image::img/myproject-settings.png[width=70%, scaledwidth=90%]
  34. [guilabel]#Group Id#::
  35. Give the project an organization-level identifier, for example, [packagename]#com.example#.
  36. It is used as a prefix for your Java package names, and hence must be a valid Java package name itself.
  37. [guilabel]#Artifact Id#:: Give the project a name, for example, `myproject`.
  38. The artifact ID must be a valid Java sub-package name.
  39. [guilabel]#Version#:: Give the project a Maven compatible version number, for example, `1.0-SNAPSHOT`.
  40. The version number should typically start with two or more integers separated with dots, and
  41. should not contain spaces.
  42. [guilabel]#Package#:: Give the base package name for the project, for example,
  43. [packagename]#com.example.myproject#.
  44. It is by default generated from the group ID and the artifact ID.
  45. [guilabel]#Properties#:: Enter values for archetype-specific properties that control naming of various elements in the created project, such as the UI class name.
  46. +
  47. You can change the version later in the [filename]#pom.xml#.
  48. +
  49. Finally, click [guibutton]#Finish# to create the project.
  50. [[getting-started.first-project.exploring]]
  51. == Exploring the Project
  52. After the [guilabel]#New Project# wizard exits, it has done all the work for you.
  53. A UI class skeleton has been written to the [filename]#src# directory.
  54. The project hierarchy shown in the Project Explorer is shown in <<figure.getting-started.first-project.exploring>>.
  55. [[figure.getting-started.first-project.exploring]]
  56. .A new Vaadin project
  57. image::img/myproject-created-annotated-hi.png[width=80%, scaledwidth=100%]
  58. The Vaadin libraries and other dependencies are managed by Maven.
  59. Notice that the libraries are not stored under the project folder, even though they are listed in the "Java Resources > Libraries > Maven Dependencies" virtual folder.
  60. [[getting-started.first-project.exploring.ui]]
  61. === The UI Class
  62. The UI class created by the plug-in contains the following code:
  63. [source, java]
  64. ----
  65. package com.example.myproject;
  66. import com.vaadin.ui.UI;
  67. ...
  68. @Theme("mytheme")
  69. public class MyUI extends UI {
  70. @Override
  71. protected void init(VaadinRequest vaadinRequest) {
  72. final VerticalLayout layout = new VerticalLayout();
  73. final TextField name = new TextField();
  74. name.setCaption("Type your name here:");
  75. Button button = new Button("Click Me");
  76. button.addClickListener(e ->
  77. layout.addComponent(new Label("Thanks " + name.getValue()
  78. + ", it works!")));
  79. layout.addComponents(name, button);
  80. setContent(layout);
  81. }
  82. @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  83. @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  84. public static class MyUIServlet extends VaadinServlet {
  85. }
  86. }
  87. ----
  88. [[getting-started.first-project.theme]]
  89. == Compiling the Theme
  90. Before running the project for the first time, click the [guilabel]#Compile Vaadin Theme# button in the toolbar, as shown in <<figure.getting-started.first-project.compiletheme>>.
  91. [[figure.getting-started.first-project.compiletheme]]
  92. .Compile Vaadin Theme
  93. image::img/myproject-compiletheme.png[width=40%, scaledwidth=60%]
  94. [[getting-started.first-project.coding]]
  95. == Coding Tips for Eclipse
  96. === Code Completion
  97. One of the most useful features in Eclipse is __code completion__. Pressing
  98. kbd:[Ctrl+Space] in the editor will display a pop-up list of possible class name and
  99. method name completions, as shown in
  100. <<figure.getting-started.first-project.coding.codecompletion>>, depending on the
  101. context of the cursor position.
  102. [[figure.getting-started.first-project.coding.codecompletion]]
  103. .Java Code Completion in Eclipse
  104. image::img/codingtips-codecompletion.png[scaledwidth=100%]
  105. === Generating Imports
  106. To automatically add an [literal]#++import++# statement for a class, such as
  107. [classname]#Button#, simply press kbd:[Ctrl+Shift+O] or click the red error indicator on the left side of the editor window.
  108. If the class is available in multiple packages, a list of the alternatives is displayed, as shown in <<figure.getting-started.first-project.coding.import>>.
  109. [[figure.getting-started.first-project.coding.import]]
  110. .Importing classes automatically
  111. image::img/codingtips-automaticimports.png[scaledwidth=70%]
  112. For server-side Vaadin development, you should generally use the classes under the [package]#com.vaadin.ui# or [package]#com.vaadin.server# packages.
  113. _You can not use client-side classes (under [package]#com.vaadin.client#) or GWT classes for server-side development._
  114. [[getting-started.first-project.server]]
  115. == Setting Up and Starting the Web Server
  116. Eclipse IDE for Java EE Developers has the Web Standard Tools package installed,
  117. which supports control of various web servers and automatic deployment of web
  118. content to the server when changes are made to a project.
  119. Make sure that Tomcat was installed with user permissions. Configuration of the
  120. web server in Eclipse will fail if the user does not have write permissions to
  121. the configuration and deployment directories under the Tomcat installation
  122. directory.
  123. Follow the following steps:
  124. . Switch to the [guilabel]#Servers# tab in the lower panel in Eclipse.
  125. List of servers should be empty after Eclipse is installed.
  126. Right-click on the empty area in the panel and select "New > Server".
  127. . Select "Apache > Tomcat v8.0 Server" and set [guilabel]#Server's host name# as [literal]#++localhost++#, which should be the default. If you have only one Tomcat installed, [guilabel]#Server runtime# has only one choice. Click [guibutton]#Next#.
  128. . Add your project to the server by selecting it on the left and clicking [guibutton]#Add# to add it to the configured projects on the right. Click [guibutton]#Finish#.
  129. . The server and the project are now installed in Eclipse and are shown in the [guilabel]#Servers# tab.
  130. To start the server, right-click on the server and select [guilabel]#Debug#.
  131. To start the server in non-debug mode, select [guilabel]#Start#.
  132. . The server starts and the WebContent directory of the project is published to the server on http://localhost:8080/myproject/.
  133. [[getting-started.first-project.run]]
  134. == Running and Debugging
  135. Starting your application is as easy as selecting [guilabel]#myproject# from the
  136. [guilabel]#Project Explorer# and then "Run > Debug As > Debug on Server".
  137. Eclipse then opens the application in built-in web browser.
  138. You can insert break points in the Java code by double-clicking on the left
  139. margin bar of the source code window. For example, if you insert a breakpoint in
  140. the [methodname]#buttonClick()# method and click the [guibutton]#What is the
  141. time?# button, Eclipse will ask to switch to the Debug perspective. Debug
  142. perspective will show where the execution stopped at the breakpoint. You can
  143. examine and change the state of the application.
  144. To continue execution, select [guilabel]#Resume# from [guilabel]#Run# menu.
  145. .Debugging a Vaadin Application
  146. image::img/debuggingMyProject.png[scaledwidth=100%]
  147. Above, we described how to debug a server-side application.
  148. Debugging client-side applications and widgets is described in
  149. <<dummy/../../../framework/clientside/clientside-debugging#clientside.debugging,"Debugging Client-Side Code">>.
  150. [[getting-started.eclipse.mavenlibraryupdate]]
  151. == Updating the Vaadin Framework Libraries
  152. Updating the Vaadin plugin does not update Vaadin Framework libraries. The libraries are
  153. project specific, as a different version might be required for different
  154. projects, so you have to update them separately for each project.
  155. . Open the [filename]#pom.xml# in an editor in Eclipse.
  156. . Edit the [propertyname]#vaadin.version# property to set the Vaadin version.
  157. +
  158. Updating the libraries can take several minutes. You can see the progress in the
  159. Eclipse status bar. You can get more details about the progress by clicking the
  160. indicator.
  161. . _In Vaadin 7.6 and older_: if you have compiled the widget set for your project, recompile it by clicking the *Compile Vaadin Widgetset* button in the Eclipse toolbar.
  162. +
  163. image::img/myproject-compilewidgetset.png[width=50%, scaledwidth=60%]
  164. . Stop the integrated Tomcat (or other server) in Eclipse, clear its caches by
  165. right-clicking the server and selecting [guilabel]#Clean# as well as
  166. [guilabel]#Clean Tomcat Work Directory#, and restart it.
  167. If you experience problems after updating the libraries, you can try using
  168. "Maven > Update Project".