A demo project showing the possibility of running a Vaadin 14 app from an
embedded Jetty, as a simple main()
method.
Both the development and production modes are supported. Also, the project demoes packaging itself both into a flatten uberjar and a zip file containing a list of jars and a runner script. See "Packaging for production" below for more details.
Looking for Gradle version? See vaadin14-embedded-jetty-gradle
Clone this github repository and import the project to the IDE of your choice as a Maven project. You need to have Java 8 or 11 installed.
To run quickly from the command-line in development mode:
- Run
./mvnw -C clean package exec:java
- Your app will be running on http://localhost:8080.
To run the app from your IDE:
- Import the project into your IDE
- Run
mvn -C clean package
in the project, to configure Vaadin for npm mode. - Run/Debug the
Main
class as an application (run themain()
method). The app will use npm to download all javascript libraries (will take a long time) and will start in development mode. - Your app will be running on http://localhost:8080.
See Main.java for details on how Jetty is configured for embedded mode.
Yeah, since we're not packaging to WAR but to uberjar/zip+jar, the webapp
folder needs to be
served from the jar itself, and therefore it needs to reside in src/main/resources/webapp
.
To package in production mode:
mvn -C clean package -Pproduction
The project packages itself in two ways:
- As a flatten uberjar (a jar with all dependencies unpacked inside, which you can simply launch with
java -jar
). Please read below regarding inherent issues with flat uberjars. The deployable file is intarget/vaadin14-embedded-jetty-1.0-SNAPSHOT-uberjar.jar
- As a zip file with dependencies. The file is in
target/vaadin14-embedded-jetty-1.0-SNAPSHOT-zip.zip
To build&run the flat uberjar:
mvn -C clean package -Pproduction
cd target
java -jar vaadin14-embedded-jetty-1.0-SNAPSHOT-uberjar.jar
To build&run the zip file:
mvn -C clean package -Pproduction
cd target
unzip vaadin14-embedded-jetty-1.0-SNAPSHOT-zip.zip
./run
Head to localhost:8080/.
There is an inherent problem with flat uberjar (everything unpacked, then packed as a single jar):
it disallows repeated resources or duplicate files. That can be problematic especially for Java Service API
property files located under META-INF/services/
, since the flat uberjar will simply
throw away any duplicate property files, which can cause certain libraries to remain unconfigured.
You should therefore always prefer the zip+jar distribution; if you keep using
flat uberjar then please keep these limitations in mind.
Another inherent issue is that it's impossible to see the dependencies of the app
as a list of jars in the lib/
folder, since everything is unpacked into one huge jar file.
Let's look at all files that this project is composed of, and what are the points where you'll add functionality:
Files | Meaning |
---|---|
pom.xml | Maven 3 build tool configuration files. Maven is used to compile your app, download all dependency jars and build the zip+uberjar file |
.travis.yml | Configuration file for Travis-CI which tells Travis how to build the app. Travis watches your repo; it automatically builds your app and runs all the tests after every commit. |
.gitignore | Tells Git to ignore files that can be produced from your app's sources - be it files produced by Gradle, Intellij project files etc. |
Procfile | Configures Heroku on how your application is launched in the cloud. |
webpack.config.js | TODO |
src/main/java | Place the sources of your app here. |
MainView.java | The main view, shown when you browse for http://localhost:8080/ |
Main.java | Launches the Embedded Jetty; just run the main() method. |
src/main/resources/ | A bunch of static files not compiled by Java in any way; see below for explanation. |
simplelogger.properties | Configures the logging engine; this demo uses the SLF4J logging library with slf4j-simple logger. |
src/main/webapp/ | Static web files served as-is by the web container. |
src/test/java/ | Your unit & integration tests go here. |
MainViewTest.java | Tests the Vaadin UI; uses the Karibu-Testing UI test library. |
frontend/ | TODO |
node_modules |
populated by npm - contains sources of all JavaScript web components. |
See the Live Demo of the app running on Heroku.
To integrate with Heroku, you need to activate the production
Maven profile
which packages Vaadin in production mode
The profile is activated by heroku-settings.xml Maven Settings file. To use the settings
file during Heroku build, set the MAVEN_SETTINGS_PATH
config var to heroku-settings.xml
in Heroku project settings tab.
See Using a Custom Maven Settings File and
Stack Overflow: Activate Maven Profile On Heroku for more details.
For a full Vaadin application example, there are more choices available also from vaadin.com/start page.