Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

getting-started-first-project.asciidoc 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. ifdef::web[]
  18. [[getting-started.first-project.ivy]]
  19. == Creating an Ivy Project
  20. endif::web[]
  21. ifdef::web[]
  22. NOTE: The following describes the creation of an Ivy project. The upcoming version of the Eclipse plug-in creates Maven projects. For that, see <<getting-started.first-project.creation>>.
  23. endif::web[]
  24. ifdef::web[]
  25. Let us create the first application project with the tools installed in the
  26. previous section. First, launch Eclipse and follow the following steps:
  27. . Start creating a new project by selecting from the menu "File > New > Project...".
  28. . In the [guilabel]#New Project# window that opens, select "Vaadin > Vaadin 7
  29. Project" and click [guibutton]#Next#.
  30. +
  31. image::img/myproject-ivy-new-vaadin.png[width=70%, scaledwidth=100%]
  32. . In the [guilabel]#Vaadin Project# step, you need to set the basic web project
  33. settings. You need to give at least the __project name__ and the runtime; the
  34. default values should be good for the other settings.
  35. +
  36. image::img/myproject-ivy-settings.png[width=70%, scaledwidth=100%]
  37. [guilabel]#Project name#:: Give the project a name. The name should be a valid identifier usable
  38. cross-platform as a filename and inside a URL, so using only lower-case
  39. alphanumerics, underscore, and minus sign is recommended.
  40. [guilabel]#Use default location#:: Define the directory under which the project is created. The default is under
  41. your workspace folder, and you should normally leave it as it is. You may need
  42. to set the directory, for example, if you are creating an Eclipse project on top
  43. of a version-controlled source tree.
  44. [guilabel]#Target runtime#:: Define the application server to use for deploying the application. The server
  45. that you have installed, for example Apache Tomcat, should be selected
  46. automatically. If not, click [guibutton]#New# to configure a new server under
  47. Eclipse.
  48. [guilabel]#Configuration#:: Select the configuration to use; you should normally use the default
  49. configuration for the application server. If you need to modify the project
  50. facets, click [guibutton]#Modify#. The recommended Servlet 3.0 configuration
  51. uses the @WebServlet deployment, while Servlet 2.4 uses the old
  52. [filename]#web.xml# deployment.
  53. [guilabel]#Deployment configuration#:: This setting defines the environment to which the application will be deployed,
  54. to generate the appropriate project directory layout and configuration files.
  55. The choises are:
  56. *** [guilabel]#Servlet# (default)
  57. *** [guilabel]#Google App Engine Servlet#
  58. *** [guilabel]#Generic Portlet (Portlet 2.0)#
  59. +
  60. The further steps in the New Project Wizard depend on the selected deployment
  61. configuration; the steps listed in this section are for the default servlet
  62. configuration.
  63. ifdef::web[]
  64. See <<dummy/../../../framework/advanced/advanced-gae#advanced.gae,"Google App
  65. Engine Integration">> and <<dummy/../../../framework/portal/portal-overview.asciidoc#portal.overview,"Portal Integration">> for instructions regarding the use of Vaadin in the alternative
  66. environments.
  67. endif::web[]
  68. [guilabel]#Vaadin version#:: Select the Vaadin version to use. The drop-down list shows, by default, the
  69. latest available version of Vaadin. The selection includes nightly
  70. [literal]#++SNAPSHOT++# builds, if you want to keep up with the absolutely
  71. latest unstable versions.
  72. +
  73. You can change the version later in the [filename]#ivy.xml#.
  74. [guilabel]#Create TestBench test#:: When enabled, the application stub will include a test case for testing the UI
  75. with Vaadin TestBench, as described in
  76. <<dummy/../../../testbench/testbench-overview.asciidoc#testbench.overview,"Vaadin TestBench">>.
  77. Vaadin TestBench API library will be included in [filename]#ivy.xml# as a dependency.
  78. Vaadin version 7.3 or later is required to create the stub.
  79. +
  80. You can click [guibutton]#Finish# here to use the defaults for the rest of the
  81. settings, or click [guibutton]#Next#.
  82. . The settings in the [guilabel]#Web Module# step define the basic web application
  83. (WAR) deployment settings and the structure of the web application project. All
  84. the settings are pre-filled, and you should normally accept them as they are.
  85. +
  86. image::img/myproject-ivy-web.png[scaledwidth=100%]
  87. [guilabel]#Context Root#:: The context root (of the application) identifies the application in the URL used
  88. for accessing it. For example, if the project has a [literal]#++myproject++#
  89. context and a single UI at the context root, the URL would be
  90. http://example.com/myproject. The wizard will suggest the project name given in
  91. the first step as the context name. You can change the context root later in the
  92. Eclipse project properties.
  93. [guilabel]#Content Directory#:: The directory containing all the content to be included in the web application
  94. (WAR) that is deployed to the web server. The directory is relative to the root
  95. directory of the project.
  96. +
  97. You can just accept the defaults and click [guibutton]#Next#.
  98. . The [guilabel]#Vaadin project# step page has various Vaadin-specific application settings.
  99. If you are trying out Vaadin for the first time, you should not need
  100. to change anything. You can set most of the settings afterwards, except the
  101. creation of the portlet configuration.
  102. +
  103. image::img/myproject-vaadin.png[scaledwidth=100%]
  104. [guilabel]#Create project template#:: Make the wizard create an UI class stub.
  105. [guilabel]#Application Name#:: A name for the application UI, shown in the title bar of the browser window.
  106. [guilabel]#Base package name#:: The name of the Java package under which the UI class of the application is to
  107. be placed.
  108. [guilabel]#Application/UI class name#:: The name of the UI class for the application, in which the user interface is
  109. developed.
  110. [guilabel]#Portlet version#:: When a portlet version is selected (only Portlet 2.0 is supported), the wizard
  111. will create the files needed for running the application in a portal. See
  112. <<dummy/../../../framework/portal/portal-overview.asciidoc#portal.overview,"Portal
  113. Integration">> for more information on portlets.
  114. +
  115. Finally, click [guibutton]#Finish# to create the project.
  116. [[getting-started.first-project.exploring]]
  117. == Exploring the Project
  118. After the [guilabel]#New Project# wizard exits, it has done all the work for
  119. you: an UI class skeleton has been written to [filename]#src# directory and the
  120. [filename]#WebContent/WEB-INF/web.xml# contains a deployment descriptor. The
  121. project hierarchy shown in the Project Explorer is shown in
  122. <<figure.getting-started.first-project.exploring>>.
  123. [[figure.getting-started.first-project.exploring]]
  124. .A New Vaadin Project
  125. image::img/myproject-ivy-created.png[width=40%, scaledwidth=60%]
  126. The Vaadin libraries and other dependencies are managed by Ivy. Notice that the
  127. libraries are not stored under the project folder, even though they are listed
  128. in the "Java Resources > Libraries > ivy.xml" virtual folder.
  129. [[getting-started.first-project.exploring.ui]]
  130. === The UI Class
  131. The UI class created by the plugin contains the following code:
  132. [source, java]
  133. ----
  134. package com.example.myproject;
  135. import com.vaadin.ui.UI;
  136. ...
  137. @SuppressWarnings("serial")
  138. @Theme("myproject")
  139. public class MyprojectUI extends UI {
  140. @WebServlet(value = "/*", asyncSupported = true)
  141. @VaadinServletConfiguration(
  142. productionMode = false,
  143. ui = MyprojectUI.class)
  144. public static class Servlet extends VaadinServlet {
  145. }
  146. @Override
  147. protected void init(VaadinRequest request) {
  148. final VerticalLayout layout = new VerticalLayout();
  149. layout.setMargin(true);
  150. setContent(layout);
  151. Button button = new Button("Click Me");
  152. button.addClickListener(new Button.ClickListener() {
  153. public void buttonClick(ClickEvent event) {
  154. layout.addComponent(
  155. new Label("Thank you for clicking"));
  156. }
  157. });
  158. layout.addComponent(button);
  159. }
  160. }
  161. ----
  162. In a Servlet 3.0 project, the deployment is configured with servlet class and a
  163. [literal]#++@WebServlet++# annotation. The stub includes the servlet class as a
  164. static inner class. You may want to refactor it to a separate normal class.
  165. In a Servlet 2.3 project, you would have a [filename]#web.xml# deployment
  166. descriptor.
  167. For a more detailed treatment of the deployment, see
  168. <<dummy/../../../framework/application/application-environment#application.environment.web-xml,"Using a web.xml Deployment Descriptor">>.
  169. endif::web[]
  170. [[getting-started.first-project.creation]]
  171. == Creating a Maven Project
  172. ifdef::web[]
  173. NOTE: The following describes project creation in the upcoming version of the Eclipse plug-in, which creates Maven rather than Ivy projects.
  174. To use it, you must have installed the experimental version of the plug-in.
  175. endif::web[]
  176. Let us create the first application project with the tools installed in the previous section.
  177. First, launch Eclipse and follow the following steps:
  178. . Start creating a new project by selecting from the menu "File > New > Project...".
  179. . In the [guilabel]#New Project# window that opens, select "Vaadin > Vaadin 7
  180. Project (Maven)" and click [guibutton]#Next#.
  181. +
  182. image::img/myproject-new-vaadin.png[width=70%, scaledwidth=90%]
  183. . In the [guilabel]#Select a Maven archetype# step, you need to select the project type.
  184. To create a simple test project, select the [guilabel]#Single-module Application Project#.
  185. +
  186. image::img/myproject-archetype-selection.png[width=70%, scaledwidth=90%]
  187. . In the [guilabel]#Specify archetype parameters# step, you need to give at least the [guilabel]#Group Id# and the [guilabel]#Artifact Id#.
  188. The default values should be good for the other settings.
  189. +
  190. image::img/myproject-settings.png[width=70%, scaledwidth=90%]
  191. [guilabel]#Group Id#::
  192. Give the project an organization-level identifier, for example, [packagename]#com.example#.
  193. It is used as a prefix for your Java package names, and hence must be a valid Java package name itself.
  194. [guilabel]#Artifact Id#:: Give the project a name, for example, `myproject`.
  195. The artifact ID must be a valid Java sub-package name.
  196. [guilabel]#Version#:: Give the project a Maven compatible version number, for example, `1.0-SNAPSHOT`.
  197. The version number should typically start with two or more integers separated with dots, and
  198. should not contain spaces.
  199. [guilabel]#Package#:: Give the base package name for the project, for example,
  200. [packagename]#com.example.myproject#.
  201. It is by default generated from the group ID and the artifact ID.
  202. [guilabel]#Properties#:: Enter values for archetype-specific properties that control naming of various elements in the created project, such as the UI class name.
  203. +
  204. You can change the version later in the [filename]#pom.xml#.
  205. +
  206. Finally, click [guibutton]#Finish# to create the project.
  207. [[getting-started.first-project.exploring]]
  208. == Exploring the Project
  209. After the [guilabel]#New Project# wizard exits, it has done all the work for you.
  210. A UI class skeleton has been written to the [filename]#src# directory.
  211. The project hierarchy shown in the Project Explorer is shown in <<figure.getting-started.first-project.exploring>>.
  212. [[figure.getting-started.first-project.exploring]]
  213. .A new Vaadin project
  214. image::img/myproject-created-annotated-hi.png[width=80%, scaledwidth=100%]
  215. The Vaadin libraries and other dependencies are managed by Maven.
  216. 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.
  217. [[getting-started.first-project.exploring.ui]]
  218. === The UI Class
  219. The UI class created by the plug-in contains the following code:
  220. [source, java]
  221. ----
  222. package com.example.myproject;
  223. import com.vaadin.ui.UI;
  224. ...
  225. @Theme("mytheme")
  226. public class MyUI extends UI {
  227. @Override
  228. protected void init(VaadinRequest vaadinRequest) {
  229. final VerticalLayout layout = new VerticalLayout();
  230. final TextField name = new TextField();
  231. name.setCaption("Type your name here:");
  232. Button button = new Button("Click Me");
  233. button.addClickListener( e -> {
  234. layout.addComponent(new Label("Thanks " + name.getValue()
  235. + ", it works!"));
  236. });
  237. layout.addComponents(name, button);
  238. layout.setMargin(true);
  239. layout.setSpacing(true);
  240. setContent(layout);
  241. }
  242. @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  243. @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  244. public static class MyUIServlet extends VaadinServlet {
  245. }
  246. }
  247. ----
  248. [[getting-started.first-project.theme]]
  249. == Compiling the Theme
  250. 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>>.
  251. [[figure.getting-started.first-project.compiletheme]]
  252. .Compile Vaadin Theme
  253. image::img/myproject-compiletheme.png[width=40%, scaledwidth=60%]
  254. [[getting-started.first-project.coding]]
  255. == Coding Tips for Eclipse
  256. === Code Completion
  257. One of the most useful features in Eclipse is __code completion__. Pressing
  258. kbd:[Ctrl+Space] in the editor will display a pop-up list of possible class name and
  259. method name completions, as shown in
  260. <<figure.getting-started.first-project.coding.codecompletion>>, depending on the
  261. context of the cursor position.
  262. [[figure.getting-started.first-project.coding.codecompletion]]
  263. .Java Code Completion in Eclipse
  264. image::img/codingtips-codecompletion.png[scaledwidth=100%]
  265. === Generating Imports
  266. To automatically add an [literal]#++import++# statement for a class, such as
  267. [classname]#Button#, simply press kbd:[Ctrl+Shift+O] or click the red error indicator on the left side of the editor window.
  268. 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>>.
  269. [[figure.getting-started.first-project.coding.import]]
  270. .Importing classes automatically
  271. image::img/codingtips-automaticimports.png[scaledwidth=70%]
  272. For server-side Vaadin development, you should generally use the classes under the [package]#com.vaadin.ui# or [package]#com.vaadin.server# packages.
  273. _You can not use client-side classes (under [package]#com.vaadin.client#) or GWT classes for server-side development._
  274. [[getting-started.first-project.server]]
  275. == Setting Up and Starting the Web Server
  276. Eclipse IDE for Java EE Developers has the Web Standard Tools package installed,
  277. which supports control of various web servers and automatic deployment of web
  278. content to the server when changes are made to a project.
  279. Make sure that Tomcat was installed with user permissions. Configuration of the
  280. web server in Eclipse will fail if the user does not have write permissions to
  281. the configuration and deployment directories under the Tomcat installation
  282. directory.
  283. Follow the following steps:
  284. . Switch to the [guilabel]#Servers# tab in the lower panel in Eclipse.
  285. List of servers should be empty after Eclipse is installed.
  286. Right-click on the empty area in the panel and select "New > Server".
  287. ifdef::web[]
  288. +
  289. image::img/tomcat-startserver-1.png[width=60%, scaledwidth=100%]
  290. endif::web[]
  291. . Select "Apache > Tomcat v7.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#.
  292. ifdef::web[]
  293. +
  294. image::img/tomcat-startserver-2.png[width=60%, scaledwidth=100%]
  295. endif::web[]
  296. . 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#.
  297. ifdef::web[]
  298. +
  299. image::img/tomcat-startserver-3.png[width=60%, scaledwidth=100%]
  300. endif::web[]
  301. . The server and the project are now installed in Eclipse and are shown in the [guilabel]#Servers# tab.
  302. To start the server, right-click on the server and select [guilabel]#Debug#.
  303. To start the server in non-debug mode, select [guilabel]#Start#.
  304. ifdef::web[]
  305. +
  306. image::img/tomcat-startserver-4.png[width=60%, scaledwidth=100%]
  307. endif::web[]
  308. . The server starts and the WebContent directory of the project is published to the server on http://localhost:8080/myproject/.
  309. ifdef::web[]
  310. +
  311. image::img/tomcat-startserver-5.png[width=60%, scaledwidth=100%]
  312. endif::web[]
  313. [[getting-started.first-project.run]]
  314. == Running and Debugging
  315. Starting your application is as easy as selecting [guilabel]#myproject# from the
  316. [guilabel]#Project Explorer# and then "Run > Debug As > Debug on Server".
  317. Eclipse then opens the application in built-in web browser.
  318. ////
  319. // This is rather irrelevant
  320. //
  321. .Running a Vaadin Application
  322. image::img/runningMyProject.png[width=60%, scaledwidth=80%]
  323. ////
  324. You can insert break points in the Java code by double-clicking on the left
  325. margin bar of the source code window. For example, if you insert a breakpoint in
  326. the [methodname]#buttonClick()# method and click the [guibutton]#What is the
  327. time?# button, Eclipse will ask to switch to the Debug perspective. Debug
  328. perspective will show where the execution stopped at the breakpoint. You can
  329. examine and change the state of the application.
  330. To continue execution, select [guilabel]#Resume# from [guilabel]#Run# menu.
  331. .Debugging a Vaadin Application
  332. image::img/debuggingMyProject.png[scaledwidth=100%]
  333. Above, we described how to debug a server-side application.
  334. Debugging client-side applications and widgets is described in
  335. <<dummy/../../../framework/clientside/clientside-debugging#clientside.debugging,"Debugging Client-Side Code">>.
  336. [[getting-started.eclipse.mavenlibraryupdate]]
  337. ifdef::web[]
  338. == Updating the Vaadin Libraries in Maven Projects
  339. endif::web[]
  340. // The book only describes Maven projects
  341. ifndef::web[]
  342. == Updating the Vaadin Libraries
  343. endif::web[]
  344. Updating the Vaadin plugin does not update Vaadin libraries. The libraries are
  345. project specific, as a different version might be required for different
  346. projects, so you have to update them separately for each project.
  347. . Open the [filename]#pom.xml# in an editor in Eclipse.
  348. . Edit the [propertyname]#vaadin.version# property to set the Vaadin version.
  349. +
  350. Updating the libraries can take several minutes. You can see the progress in the
  351. Eclipse status bar. You can get more details about the progress by clicking the
  352. indicator.
  353. . _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.
  354. +
  355. image::img/myproject-compilewidgetset.png[width=50%, scaledwidth=60%]
  356. . Stop the integrated Tomcat (or other server) in Eclipse, clear its caches by
  357. right-clicking the server and selecting [guilabel]#Clean# as well as
  358. [guilabel]#Clean Tomcat Work Directory#, and restart it.
  359. If you experience problems after updating the libraries, you can try using
  360. "Maven > Update Project".
  361. ifdef::web[]
  362. [[getting-started.eclipse.libraryupdate]]
  363. == Updating the Vaadin Libraries in Ivy Projects
  364. Updating the Vaadin plugin does not update Vaadin libraries.
  365. The libraries are project specific, as a different version might be required for different projects, so you have to update them separately for each project.
  366. . Open the [filename]#ivy.xml# in an editor in Eclipse.
  367. . Edit the entity definition at the beginning of the file to set the Vaadin
  368. version.
  369. +
  370. [subs="normal"]
  371. ----
  372. &lt;!ENTITY vaadin.version "**7.x.x**"&gt;
  373. ----
  374. +
  375. You can specify either a fixed version number, as shown in the above example, or
  376. a dynamic revision tag such as [literal]#++latest.release++#.
  377. You can find more information about the dependency declarations in Ivy documentation.
  378. . Right-click the project and select "Ivy > Resolve".
  379. +
  380. Updating the libraries can take several minutes. You can see the progress in the
  381. Eclipse status bar. You can get more details about the progress by clicking the
  382. indicator.
  383. . If you have compiled the widget set for your project, recompile it by clicking the *Compile Vaadin Widgetset* button in Eclipse toolbar.
  384. +
  385. image::img/myproject-compilewidgetset.png[width=50%, scaledwidth=60%]
  386. . Stop the integrated Tomcat (or other server) in Eclipse, clear its caches by
  387. right-clicking the server and selecting Clean as well as Clean Tomcat Work
  388. Directory, and restart it.
  389. If you experience problems after updating the libraries, you can try clearing
  390. the Ivy resolution caches by right-clicking the project and selecting "Ivy >
  391. Clean all caches".
  392. Then, do the "Ivy > Resolve" and other tasks again.
  393. endif::web[]