🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 概述 Webapp Runner可以在任何安装有JRE环境的系统中利用Tomcat将应用程序启动。使用Webapp Runner不需要安装Tomcat。它只是一个jar文件可以用`java`命令运行和配置。 这篇文档将会教您如何构建应用程序,并通过Wepapp Runner启动,最终部署到好雨云平台。 您可以按照下面的每一步从头开始构建一个应用程序,或者直接跳到最后浏览文章源码。当然你也可以使用大多数现有的Maven webapp项目。 #### **Webapp Runner 是如何工作的?** 在本地或好雨云平台使用Webapp Runner启动应用程序时的命令类似如下格式: ~~~ $ java -jar webapp-runner.jar application.war deploying app from: /Users/zhouyq/dev/gitrepos/java-webapp-runner/target/webappRunnerSample.war Feb 14, 2015 5:21:44 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Feb 14, 2015 5:21:44 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Tomcat Feb 14, 2015 5:21:44 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.57 Feb 14, 2015 5:21:44 PM org.apache.catalina.startup.ContextConfig webConfig INFO: No global web.xml found Feb 14, 2015 5:21:44 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] ~~~ Webapp Runner 会利用给定的war文件启动一个Tomcat实例。它会使用Tomcat提供的内嵌API来把程序跑起来,与[Jetty Runner](https://webtide.com/)提供的选项有些类似。 Webapp Runner 是 [开源](https://github.com/jsimone/webapp-runner) 软件,你可以随时查阅项目源码。 #### **创建应用程序** ~~~ $ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp ... [INFO] Generating project in Interactive mode Define value for property 'groupId': : com.example Define value for property 'artifactId': : helloworld ~~~ (你可以使用任意的 groupId 或 artifactId)。执行完上面的命令后,`helloworld`目录会生成一个完整的Java web 应用。 #### **配置Maven下载Webapp Runner** 通过构建配置文件(pom.xml)可以下载Webapp Runner,通过这种方式可以自动解决依赖关系,提高程序的灵活性与可移植性。在这里我们使用 Maven 因此需要使用依赖插件(maven-dependency-plugin)来下载jar包。将如下的配置添加到你的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>com.github.jsimone</groupId> <artifactId>webapp-runner</artifactId> <version>7.0.57.2</version> <destFileName>webapp-runner.jar</destFileName> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> ~~~ Webapp Runner 是基于 Tomcat server的,因此 7.0.57.2 版本的 Webapp Runner 使用的是 7.0.57 版本的Tomcat。Tomcat 8 Beta测试版本对应 8.0.18.0-M1 版本的 Webapp Runner. #### **运行程序** 先运行如下简单的命令构建应用程序: `$ mvn clean package` 然后通过java命令运行起来: `$ java -jar target/dependency/webapp-runner.jar target/*.war` 就这么简单,你的应用程序已经运行并监听8080端口 **说明** `如果你需要在启动应用之前展开WAR文件,需要在启动命令的 target/*.war选项之前添加 --expand-war ` ### **将应用部署到好雨云平台** #### **创建Procfile文件** 您需要在项目跟目录创建 Procfile 文件来描述如何启动您的应用程序,该文件只是一个简单的文本文件,通过如下方式将启动命令写到Procfile文件中: ~~~ cd helloworld echo "web: java \$JAVA_OPTS -jar target/dependency/webapp-runner.jar --port \$PORT target/*.war" > Procfile ~~~ **注意** ~~~ $JAVA_OPTS 与 $PORT 变量必须添加,否则程序无法在平台上跑起来。 ~~~ #### **可选的 JDK 版本** 平台默认会使用 OpenJDK 1.8 如果您想使用1.7版本,可以通过system.properties文件设置`java.runtime.version=1.7`选项, system.properties文件的内容类似如下格式: `java.runtime.version=1.7` 您可以指定 `1.6`, `1.7`, 或 `1.8` (1.8 beta版本) 分别对应 Java 6, 7, 或 8 #### **将更改提交到git** #### **创建应用** `参见: 新建应用文档` #### **提交代码** ~~~ $ git init $ git add . $ git commit -m "Ready to deploy" # 下面的仓库地址根据用户的情况自行更改 $ git remote add origin http://code.goodrain.com/app/项目名称.git $ git push -u origin master ~~~ 更多关于提交代码的文档参见: 新建应用-代码提交 #### **一键部署** 参见: 一键部署文档 #### **访问应用** 直接点击 应用概览页的 “访问” 按钮,打开应用首页