Creating a new project from scratch
Usually, you would use the vaadin-archetype-application or vaadin-archetype-application-multimodule Maven archetypes to create a new Vaadin application. There's nothing wrong with using these if the generated code suits your needs. However, these archetypes generate more code than you need, partially because they try to show you how to get started with Vaadin and partially because they are general-purpose starters which are well-suited for most projects. But let's gain full control (and understanding) of the web application by creating a Vaadin project in a very different way—a more fine-grained, controlled way.
A Vaadin application is, at the end of the day, a Java application packaged as a WAR file. You can think of it as a standard web application in which you drop some JARs that allow you to build a web UI using the Java Programming Language instead of HTML and JavaScript. Is it as simple as dropping some JARs into your Java project? Let's find out!
Use the maven-archetype-webapp to generate a simple Java web application by executing the following on the command line:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
Use the following properties when prompted:
- groupId: packt.vaadin.datacentric.chapter01
- artifactId: chapter-01
- version: 1.0-SNAPSHOT
- package: packt.vaadin.datacentric.chapter01
Clean up the pom.xml file to make it look like the following:
<project ...>
<modelVersion>4.0.0</modelVersion>
<artifactId>chapter-01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
</project>
Remove the src/main/webapp and src/main/resources directories. This deletes the generated web.xml file which will make Maven complain. To tell it that this was intended, add the following property to your pom.xml file:
...
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
...
Also, add the following properties to configure Maven to use Java 8:
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>