如何使用Maven设置一个有效的Vaadin项目并使用Jetty运行它?

ton*_*nix 4 jetty vaadin maven

我在os x机器上安装了maven:

$ 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"
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几个教程,在SO中查看:

但是我无法在jetty maven插件上设置可运行的Vaadin项目.我的问题是在jetty Web服务器上找不到该项目,我总是收到404 Not Found错误.

以下是我做的步骤:

$ 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] ------------------------------------------------------------------------
$
Run Code Online (Sandbox Code Playgroud)

然后我输入mvn jetty:run命令:

$ mvn jetty:run
Run Code Online (Sandbox Code Playgroud)

这给出了这个错误:

Maven:在当前项目中找不到前缀'jetty'的插件

所以我在〜/ .m2目录中添加一个settings.xml文件:

〜/ .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>
Run Code Online (Sandbox Code Playgroud)

并输入mvn jetty:再次运行:

$ 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 SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
Run Code Online (Sandbox Code Playgroud)

现在您可以看到jetty启动,但是当我在http:// localhost:8080 /打开浏览器时,我得到404未找到:Problem accessing /. Reason: Not Found.我尝试过以下网址之一:

没有人为我工作(我总是找不到404).

现在,为什么他们在文档中说如果你按照我上面所做的步骤:

瓦丁书:

然后,您可以在Web浏览器中以http:// localhost:8080/project-name打开它 .

project-name是赋予maven生成目标的artifactId参数,而我的是artifactId: hello-world,但正如我所说的http:// localhost:8080/hello-world对我不起作用(404未找到).

我也在阅读Vaadin 7 CookBook,他们在这里说:

我们完成了,我们可以运行我们的新Web应用程序.转到pom.xml文件所在项目的根目录,然后运行以下命令.mvn jetty:start应用程序将在 http:// localhost:8080上运行

什么???http://localhost:8080没有"项目名称"后缀??? 我试过了,但仍然没有找到404,无论如何......

那么,有没有人可以解释我发生了什么,我怎样才能解决这个令人沮丧的问题,以便最终开始与Vaadin一起开发?

谢谢你的关注.

Jen*_*son 8

我猜你遇到了一个问题,那里的指令是针对一个旧的原型版本,原型大约在一个月左右的时间里被更新为一个新版本,但说明书谈的是旧版本.最大的区别在于原型被制作成一个多模块项目,其中旧的原型是单个模块项目.

许多人在使用多模块项目时遇到了问题,因此当前的原型可能会在不久的将来重新命名为其他东西,而在当前的命名空间中,同一个项目将缩减回单个模块项目,以便更容易入门用.但是,到那时为止,您将不得不学习使用多模块项目.

如果你查看文件夹内部,你应该在hello-world文件夹中有一组文件夹.每个文件夹都有一个模块:

  • hello-world-ui - 实际UI项目所在的代码
  • hello-world-widgetset - 将使用GWT从Java编译为JavaScript的代码.-ui取决于这个项目
  • hello-world-production - 缩小版的-ui,用于将应用程序部署到生产服务器时.这纯粹是对性能提升的小优化.
  • 和根文件夹本身就是一个模块,hello-world-parent.

首先,您应该删除添加到settings.xml的内容.应该在应该使用它的项目的pom.xml中定义Jetty.现在,您已全局定义它,以便它适用于所有项目,也适用于无法在Jetty上运行的项目.

为了能够运行软件,您必须做两件事,构建整个软件并启动-ui模块.

  1. mvn install在根文件夹中调用时会发生构建.您也可以在-widgetset文件夹中执行此操作,但重要的是-widgetset会构建并安装到您的本地maven存储库中,以便-ui可以在需要时找到它.调用mvn installroot将导致以正确的顺序分别调用所有子模块.
  2. 启动UI是通过转到hello-world-ui并进行调用来完成的mvn jetty:run.-ui项目中的pom.xml定义了一个jetty依赖项.

关于URL中的项目名称后缀.它取决于servlet容器(Jetty是其中之一,Tomcat,Wildfly等是其他人)来决定它是否将其部署在特定URL下.Jetty是一个简单的servlet容器,只要你需要它就可以启动它,因此http://localhost:8080/很好.Tomcat和其他更大的更适合生产,你可以在一个Tomcat实例中同时部署许多项目(war),所以它需要通过将项目名称添加到url中来分离它们,hello-world-ui

  • 此外,如果你想要一个简单的单个模块Vaadin项目,你可以在github中克隆我的hello-world项目:https://github.com/Peppe-/hello-world.请参阅README中的用法部分进行设置.将三行复制粘贴到控制台中,软件应该正在运行.请注意,我从pom中删除了非必需的所有内容,因此如果您需要更多功能,例如widgetset编译,则必须重新添加它们.我喜欢简约而简单的解决方案.:) (2认同)