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 10KB

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