java WAR 应用程序的开发服务器来自当前目录且没有 sudo

Development server for java WAR applications from current directory and without sudo

在 Rails 和大多数现代 Web 框架中,我可以通过 运行 使用单个简单命令 (rails server) 连接开发服务器并访问浏览器来轻松地交互式开发 Web 应用程序.

对于 Java WAR 应用程序,我能找到 运行 的唯一方法是安装 tomcat、构建应用程序并将其复制到它的 webapps 文件夹中,这需要 sudo。

有没有更简单的方法 运行 这样 WAR 无需 sudo 且无需复制目录的开发应用程序(这特别糟糕,因为 webapps 文件夹随安装而变化)?

基于保存的 inotify 构建会更好,这样我就可以编辑文件并在重新加载浏览器后立即看到结果。

我并不局限于使用 Tomcat:任何能够 运行 宁 WAR 文件的 Servlet / JSP 实现都适合我。

使用 Maven

如果您已经在使用 Maven 构建项目,那么最简单的方法可能是 Jetty Maven plugin

首先,将此添加到您的 pom.xml:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.2.7-SNAPSHOT</version>
</plugin>

然后您可以从命令行 运行 Jetty 服务器:

mvn jetty:run

这与 Rails 非常相似,因为它会在您的控制台中启动一个服务器。它监视对您的应用程序源文件或配置文件的更改,并在必要时重新加载。

sudo 没有问题,因为您没有将 war 文件打包并部署到其他地方的 tomcat 实例。相反,它 运行 将您的应用程序作为一个展开的 war 文件直接来自 Maven 创建的输出目录。

不过,我很欣赏这听起来比实际上更容易 - 大部分工作很可能是将您的项目转换为 Maven...

使用独立 Jetty 自动部署 Web 应用程序

如果您不想将项目移交给 Maven,另一种方法是使用 Jetty 的 automated web app deployment

jetty-运行ner 是我能找到的最佳选择。

安装 Maven 后,您可以执行以下操作:

java -jar target/dependency/jetty-runner.jar target/*.war

要安装,到您的 pom.xml:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.mortbay.jetty</groupId>
                                <artifactId>jetty-runner</artifactId>
                                <version>7.5.4.v20111024</version>
                                <destFileName>jetty-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

和运行:

mvn package

但是,当然,您不会被迫使用 Maven。

这是Heroku推荐的方法: https://devcenter.heroku.com/articles/deploy-a-java-web-application-that-launches-with-jetty-runner

一个问题的解决方案也解决了这个问题是: Jetty Run War Using only command line