Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a working Vaadin project with Maven and run it with Jetty?

I have maven installed on an os x machine:

$ mvn --version 
Apache Maven 3.2.3 
Java version: 1.8.0_25, 
vendor: Oracle Corporation 
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre 
Default locale: en_US, platform encoding: UTF-8 
OS name: "mac os x", version: "10.10.1", arch: "x86_64", family: "mac"

I have tried several tutorials, looked around in SO:

  • https://vaadin.com/book/-/page/getting-started.maven.html
  • https://vaadin.com/wiki/-/wiki/Main/Creating+a+Maven+project
  • http://www.maxpagels.com/tutorials/vaadin-tutorial/
  • 404 Not Found Error in a simple Jetty/Maven Hello World webapp

But yet I can't setup a working Vaadin project runnable on the jetty maven plugin. My problem is that the project is not found on the jetty web server, I always get a 404 Not Found error.

Here are the steps I made:

$ cd ~
$ mkdir Maven
$ cd Maven
$ mvn archetype:generate \
   -DarchetypeGroupId=com.vaadin \
   -DarchetypeArtifactId=vaadin-archetype-application \
   -DarchetypeVersion=7.3.9 \
   -Dpackaging=war
... 
[INFO] Archetype repository missing. Using the one from [com.vaadin:vaadin-archetype-application:7.3.9] found in catalog remote
    Define value for property 'groupId': : com.example
    Define value for property 'artifactId': : hello-world
    Define value for property 'version':  1.0-SNAPSHOT: : 1.0
    Define value for property 'package':  com.example: : 
    [INFO] Using property: themeName = mytheme
    [INFO] Using property: uiName = MyUI
    [INFO] Using property: widgetsetName = MyAppWidgetset
    Confirm properties configuration:
    groupId: com.example
    artifactId: hello-world
    version: 1.0
    package: com.example
    themeName: mytheme
    uiName: MyUI
    widgetsetName: MyAppWidgetset
     Y: : Y
     ...
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 56.332 s
    [INFO] Finished at: 2015-01-27T12:03:42+03:00
    [INFO] Final Memory: 15M/245M
    [INFO] ------------------------------------------------------------------------
$ cd hello-world
$ mvn package
    ...
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:00 min
[INFO] Finished at: 2015-01-27T12:08:56+03:00
[INFO] Final Memory: 21M/247M
[INFO] ------------------------------------------------------------------------
$

Then I type the mvn jetty:run command:

$ mvn jetty:run

which gives this error:

Maven: No plugin found for prefix 'jetty' in the current project

So I add a settings.xml file inside ~/.m2 directory:

~/.m2/settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
</settings>

And type mvn jetty:run again:

$ mvn jetty:run
...
[INFO] Configuring Jetty for project: hello-world-parent
[INFO] webAppSourceDirectory not set. Defaulting to /Users/me/Maven/hello-world/src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes directory /Users/me/Maven/hello-world/target/classes does not exist
[INFO] Context path = /
[INFO] Tmp directory = /Users/me/Maven/hello-world/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = null
[INFO] Webapp directory = /Users/me/Maven/hello-world/src/main/webapp
2015-01-27 12:11:28.610:INFO:oejs.Server:jetty-8.1.16.v20140903
2015-01-27 12:11:29.121:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2015-01-27 12:11:31.038:WARN:oejsh.RequestLogHandler:!RequestLog
2015-01-27 12:11:31.155:INFO:oejs.AbstractConnector:Started [email protected]:8080
[INFO] Started Jetty Server

Now as you can see jetty starts, but when I open the browser at http://localhost:8080/, I get a 404 not found: Problem accessing /. Reason: Not Found. I have either tried one of the following URLs:

  • http://localhost:8080/
  • http://localhost:8080
  • http://localhost:8080/hello-world
  • http://localhost:8080/hello-world/
  • http://localhost:8080/hello-world-parent/
  • http://localhost:8080/hello-world-parent
  • http://localhost:8080/webapp
  • http://localhost:8080/webapp/

No one worked for me (I always get a 404 not found).

Now, why they say in the documentation that if you follow the steps I have done above:

Book of Vaadin:

You can then open it in a web browser at http://localhost:8080/project-name.

project-name is the artifactId param given to the maven generate goal, and mine was artifactId: hello-world, but as I said http://localhost:8080/hello-world didn't work for me (404 not found).

I am also reading the Vaadin 7 CookBook and here they say:

We are done and we can run our new web application. Go to the root of the project where pom.xml file is located and run the following command. mvn jetty:start The application will be running on http://localhost:8080

What??? http://localhost:8080 without the "project-name" suffix??? I have tried it, but still I get the 404 not found, anyway...

So please, is there someone that can explain me what is going on and how can I resolve this frustrating issue in order finally start developing with Vaadin?

Thank you for the attention.

like image 944
tonix Avatar asked Feb 11 '23 01:02

tonix


1 Answers

I guess you have ran into a problem where instructions were for an old archetype version, where of the archetype was updated to a new version a month or so ago, but the instructions talk about the old one. The big difference is that the archetype was made into a multi-module project, where as the old archetype was a single module project.

Many have had issues working with multi module projects, so the current archetype will probably be renamed in the near future to something else and in the current namespace there will be the same project reduced back to a single module project to make it easier to get started with. Up until that point, however, you'll have to learn to use the multi-module project.

If you look inside the folder, you should have a set of folders in the hello-world folder. Every folder there is a module:

  • hello-world-ui - Code where the actual UI project lives in
  • hello-world-widgetset - The code that will be compiled from Java to JavaScript with GWT. -ui depends on this project
  • hello-world-production - A minified version of -ui, meant for when you deploy your application to a production server. This is purely small optimisations for performance gains.
  • and the root folder which is a module by itself, hello-world-parent.

First of you should remove the things you added to settings.xml. Jetty should be defined in the pom.xml of the project that should use it. Now you defined it globally so that it applies to all project, also those that can't be run on Jetty.

To be able to run the software you have to do two things, build the whole software and start up the -ui module.

  1. Building happens when you call mvn install in the root folder. You could also do it just in the -widgetset folder but the important thing is that -widgetset gets built and installed into your local maven repository so that -ui can find it when it needs it. Calling mvn install on root will lead to it being called separately to all the submodules, in the correct order.
  2. Starting the UI is done by going to hello-world-ui and there calling mvn jetty:run. The pom.xml in -ui project has defined a jetty dependency.

About the project name suffix in URL. It is up to the servlet container (where of Jetty is one, Tomcat, Wildfly etc. being others) to decide if it deploys it under and specific URL. Jetty is a simple servlet container which you just start for a project when ever you need it and thus http://localhost:8080/is fine. Tomcat and others are bigger ones that are more suitable for production, and you can deploy many projects (wars) in one Tomcat instance at once, so it needs to separate them by adding the project name into the url, the hello-world-ui

like image 63
Jens Jansson Avatar answered Apr 26 '23 17:04

Jens Jansson