|
| 1 | +Getting Started |
| 2 | +=== |
| 3 | + |
| 4 | +Starting with 2.11.0 of the GWT SDK the [webAppCreator](gettingstarted-outdated.html) is deprecated. Instead, we will use |
| 5 | +the [gwt-maven-archetypes](https://github.com/tbroyer/gwt-maven-archetypes) by Thomas Broyer to create a new GWT project. Using the **artifactId: modular-webapp** |
| 6 | +the plugin generates a Maven project with separate modules for |
| 7 | + |
| 8 | +* client |
| 9 | +* server |
| 10 | +* shared. |
| 11 | + |
| 12 | +**Separation of code is the way to go in modern web development.** |
| 13 | + |
| 14 | +## TOC |
| 15 | + |
| 16 | +* [Prerequisites](#prereqs) |
| 17 | +* [Create your first web application](#create) |
| 18 | +* [Run locally in super dev mode](#run) |
| 19 | +* [Make a few changes](#change) |
| 20 | +* [Debugging the browser](#bebug) |
| 21 | +* [Compile and run in production mode](#compile) |
| 22 | +* [Set up an IDE](#setup) |
| 23 | + |
| 24 | +## Prerequisites<a id="prereqs"></a> |
| 25 | + |
| 26 | +1. GWT 2.11.0 still supports Java 8 (running and building). We suggest to use at least Java 11 or 17. If necessary, download and |
| 27 | + install the Java SE Development Kit (JDK) <a href="https://adoptium.net/de/temurin/releases/" |
| 28 | + rel="nofollow">from Eclipse Temurin</a> for your platform. Install the JDK by following the |
| 29 | + installation instructions. |
| 30 | +2. Apache Maven is also necessary to run the gwt-maven-archetype. In this sample the Maven CLI is used. If |
| 31 | + you don't already have it, install <a href="https://maven.apache.org/download.cgi" rel="nofollow">Apache Maven</a>. |
| 32 | + |
| 33 | +If you have problems running the gwt-maven-archetype from the command line, try setting the |
| 34 | +$JDK_HOME environment variable with export JDK_HOME="[pathToJDK]" and $MAVEN_HOME environment variable with export MAVEN_HOME="[pathToMaven]" |
| 35 | +and add both to the path. |
| 36 | + |
| 37 | +## Create your first web application<a id="create"></a> |
| 38 | + |
| 39 | +Open a terminal and choose the directory you like to generate the project into. Generate a new project using: |
| 40 | + |
| 41 | +``` |
| 42 | +mvn archetype:generate \ |
| 43 | + -DarchetypeGroupId=net.ltgt.gwt.archetypes \ |
| 44 | + -DarchetypeVersion=LATEST \ |
| 45 | + -DarchetypeArtifactId=modular-webapp |
| 46 | +``` |
| 47 | + |
| 48 | +The archetype will request values for: |
| 49 | + |
| 50 | +* **groupId**: com.mycompany.mywebapp |
| 51 | +* **artifactId**: mywebapp |
| 52 | +* **version**: HEAD-SNAPSHOT |
| 53 | +* **package**: com.mycompany.mywebapp |
| 54 | +* **module-short-name**: mywebap |
| 55 | + |
| 56 | +After entering the parameter above, the terminal shows something like that: |
| 57 | + |
| 58 | +``` |
| 59 | +[INFO] Using property: module = App |
| 60 | +Define value for property 'groupId': com.mycompany.mywebapp |
| 61 | +Define value for property 'artifactId': mywebapp |
| 62 | +Define value for property 'version' 1.0-SNAPSHOT: : HEAD-SNAPSHOT |
| 63 | +Define value for property 'package' com.mycompany.mywebapp: : com.mycompany.mywebapp |
| 64 | +Define value for property 'module-short-name' app: : MyWebApp |
| 65 | +Confirm properties configuration: |
| 66 | +module: App |
| 67 | +groupId: com.mycompany.mywebapp |
| 68 | +artifactId: mywebapp |
| 69 | +version: HEAD-SNAPSHOT |
| 70 | +package: com.mycompany.mywebapp |
| 71 | +module-short-name: mywebapp |
| 72 | + Y: : |
| 73 | +``` |
| 74 | + |
| 75 | +Enter **Y** to continue. |
| 76 | + |
| 77 | +Now, the new project will be generated and you see the following messages: |
| 78 | + |
| 79 | +``` |
| 80 | +[INFO] ---------------------------------------------------------------------------- |
| 81 | +[INFO] Using following parameters for creating project from Archetype: modular-webapp:LATEST |
| 82 | +[INFO] ---------------------------------------------------------------------------- |
| 83 | +[INFO] Parameter: groupId, Value: com.mycompany.mywebapp |
| 84 | +[INFO] Parameter: artifactId, Value: mywebapp |
| 85 | +[INFO] Parameter: version, Value: HEAD-SNAPSHOT |
| 86 | +[INFO] Parameter: package, Value: com.mycompany.mywebapp |
| 87 | +[INFO] Parameter: packageInPathFormat, Value: com/mycompany/mywebapp |
| 88 | +[INFO] Parameter: package, Value: com.mycompany.mywebapp |
| 89 | +[INFO] Parameter: module-short-name, Value: MyWebApp |
| 90 | +[INFO] Parameter: module, Value: App |
| 91 | +[INFO] Parameter: groupId, Value: com.mycompany.mywebapp |
| 92 | +[INFO] Parameter: artifactId, Value: mywebapp |
| 93 | +[INFO] Parameter: version, Value: HEAD-SNAPSHOT |
| 94 | +[INFO] Parent element not overwritten in /Users/gwt-user/Desktop/mywebapp/mywebapp-client/pom.xml |
| 95 | +[INFO] Parent element not overwritten in /Users/gwt-user/Desktop/mywebapp/mywebapp-shared/pom.xml |
| 96 | +[INFO] Parent element not overwritten in /Users/gwt-user/Desktop/mywebapp/mywebapp-server/pom.xml |
| 97 | +[INFO] Project created from Archetype in dir: /Users/gwt-user/Desktop/mywebapp |
| 98 | +[INFO] ------------------------------------------------------------------------ |
| 99 | +[INFO] BUILD SUCCESS |
| 100 | +[INFO] ------------------------------------------------------------------------ |
| 101 | +[INFO] Total time: 01:09 min |
| 102 | +[INFO] Finished at: 2023-12-17T17:38:02+01:00 |
| 103 | +[INFO] ------------------------------------------------------------------------ |
| 104 | +``` |
| 105 | + |
| 106 | +Switch to the generated directory by entering `cd mywebapp`. |
| 107 | + |
| 108 | +Listing of the directory (**Windows**: dir, **Mac and Linux**: ls -la), shows: |
| 109 | + |
| 110 | +``` |
| 111 | +drwxr-xr-x@ 6 gwt-user staff 192 Dec 17 17:38 . |
| 112 | +drwx------@ 50 gwt-user staff 1600 Dec 17 17:38 .. |
| 113 | +drwxr-xr-x@ 4 gwt-user staff 128 Dec 17 17:38 mywebapp-client |
| 114 | +drwxr-xr-x@ 4 gwt-user staff 128 Dec 17 17:38 mywebapp-server |
| 115 | +drwxr-xr-x@ 4 gwt-user staff 128 Dec 17 17:38 mywebapp-shared |
| 116 | +-rw-r--r--@ 1 gwt-user staff 7423 Dec 17 17:38 pom.xml |
| 117 | +``` |
| 118 | + |
| 119 | +## Run locally in super dev mode<a id="run"></a> |
| 120 | + |
| 121 | +To start the code server, run the following command: |
| 122 | + |
| 123 | +``` |
| 124 | +mvn gwt:codeserver -pl *-client -am |
| 125 | +``` |
| 126 | + |
| 127 | +and entering the following command in another terminal window to start the server: |
| 128 | + |
| 129 | +``` |
| 130 | +mvn jetty:run -pl *-server -am -Denv=dev |
| 131 | +``` |
| 132 | + |
| 133 | +Now, switch to your preferred browser (we suggest using: Chrome, FireFox or Opera) and enter: |
| 134 | + |
| 135 | +``` |
| 136 | +http://lcoalhost:8080 |
| 137 | +``` |
| 138 | + |
| 139 | +Yet, the application is running in super dev mode. You see something like this in the browser: |
| 140 | + |
| 141 | +<div class="screenshot"><a href="images/mywebapp.png"><img src="images/mywebapp.png" alt="Screenshot" width="42%"/></a></div> |
| 142 | + |
| 143 | +## Make a few changes<a id="change"></a> |
| 144 | + |
| 145 | +Let's change the label of the button. Because this is something on the client side, we need to locate the source code in the client module. |
| 146 | +The source code is located in the `mywebapp-client/src/main/java/com/mycompany/mywebapp` subdirectory and the source in the `App.java` class. |
| 147 | + |
| 148 | +Look inside `App.java`. Line 42 constructs the "Send" button. |
| 149 | + |
| 150 | +``` |
| 151 | +final Button sendButton = new Button("Send"); |
| 152 | +``` |
| 153 | + |
| 154 | +Change the text from "Send" to "Send to Server". |
| 155 | + |
| 156 | +``` |
| 157 | +final Button sendButton = new Button("Send to Server"); |
| 158 | +``` |
| 159 | + |
| 160 | +Save the file and simply click "Refresh" in your browser to see your change. The button should now say "Send to Server" instead of "Send": |
| 161 | + |
| 162 | +## Debugging the browser<a id="debug"></a> |
| 163 | + |
| 164 | +In super dev mode, you can easily debug your client code in the browser. While running your application, open the dev tools |
| 165 | +in your browser. Select the `Sources`-tab and press `ctrl-P` (Windows/Linux) or `cmd-P` (macOS). A popup opens. Enter `App.java` and press return. |
| 166 | + |
| 167 | +<div class="screenshot"><a href="images/sdm-debugger.png"><img src="images/sdm-debugger.png" alt="Screenshot" width="42%"/></a></div> |
| 168 | + |
| 169 | +The source code of the `App.java` class is displayed in the debugger. Add a breakpoint by clicking of Line number 94. Press the send button. |
| 170 | +The execution will stop at line 94. The stacktrace is visible, the content of the variable, etc. |
| 171 | + |
| 172 | +<div class="screenshot"><a href="images/sdm-debugger-breakpoint.png"><img src="images/sdm-debugger-breakpoint.png" alt="Screenshot" width="42%"/></a></div> |
| 173 | + |
| 174 | +## Compile and run in production mode<a id="compile"></a> |
| 175 | + |
| 176 | +To run the application as JavaScript in what GWT calls "production mode", create the application by executing: |
| 177 | + |
| 178 | +``` |
| 179 | +mvn clean package |
| 180 | +``` |
| 181 | + |
| 182 | +The Maven "package" goal invokes the GWT compiler which generates a number of JavaScript and HTML files from the |
| 183 | +MyWebApp Java source code in the `target/` subdirectory. There you will find a `mywebapp-server-HEAD-SNAPSHOT.war`. |
| 184 | +You can deploy this war file to every servlet engine and run it. Once running, enter `mywebapp-server-HEAD-SNAPSHOT/index.html` |
| 185 | +in your web browser. The application should look identical to the super dev mode above. You can change the name of the war using |
| 186 | +Maven 'buildname`. |
| 187 | + |
| 188 | +Congratulations! You've created your first web application using GWT. |
| 189 | +Since you've compiled the project, you're now running pure JavaScript and |
| 190 | +HTML that works in Edge, Chrome, Firefox, Safari, and Opera. You could now deploy |
| 191 | +your application to production by serving the HTML and JavaScript files from your servlet engine. |
| 192 | + |
| 193 | +In case you prefer a Spring Boot server, check the |
| 194 | +[gwt-maven-springboot-archetype](https://github.com/NaluKit/gwt-maven-springboot-archetype) plugin. It creates a |
| 195 | +project in a similar way, but uses Spring Boot instead of Jetty on the server side. |
| 196 | + |
| 197 | +## Set up an IDE<a id="setup"></a> |
| 198 | + |
| 199 | +Now that you've created your first app, you probably want to do something a |
| 200 | +bit more interesting. But first, if you normally work with an IDE you'll want to |
| 201 | +set up your IDE to use the GWT SDK: |
| 202 | + |
| 203 | +[Set up Eclipse](usingeclipse.html) |
| 204 | + |
| 205 | +[Set up IntelliJ](usingintellij.html) |
| 206 | + |
| 207 | +Now, with your sample project set up, head over to [Build a Sample GWT App](doc/latest/tutorial/gettingstarted.html). |
0 commit comments