首页 文章

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

提问于
浏览
4

我在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"

我已经尝试了几个教程,在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] ------------------------------------------------------------------------
$

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

$ mvn jetty:run

这给出了这个错误:

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

所以我在〜/ .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>

然后再次输入 mvn jetty:run

$ 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

现在您可以看到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 没有"project-name"后缀???我试过了,但仍然没有找到404,无论如何......

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

谢谢你的关注 .

1 回答

  • 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模块 .

    • 在根文件夹中调用 mvn install 时会发生构建 . 您也可以在-widgetset文件夹中执行此操作,但重要的是-widgetset会构建并安装到您的本地maven存储库中,以便-ui可以在需要时找到它 . 在root上调用 mvn install 将导致以正确的顺序分别调用所有子模块 .

    • 启动UI是通过转到hello-world-ui并调用 mvn jetty:run 来完成的 . -ui项目中的pom.xml定义了一个jetty依赖项 .

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

相关问题