应用groovy开发jfinal-4.9

1.搭建groovy开发环境

https://segmentfault.com/a/11...

2.应用groovy开发jfinal-4.9

2.1.开发

2.1.1.目录构造如下

2.1.2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <parent>    <groupId>com.litongjava</groupId>    <artifactId>groovy-ee-jfinal-4.9-study</artifactId>    <version>1.0</version>  </parent>  <artifactId>groovy-ee-jfinal-4.9-hello</artifactId>  <build>    <plugins>      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <version>3.1</version>        <configuration>          <compilerId>groovy-eclipse-compiler</compilerId>        </configuration>        <dependencies>          <dependency>            <groupId>org.codehaus.groovy</groupId>            <artifactId>groovy-eclipse-compiler</artifactId>            <version>2.9.1-01</version>          </dependency>          <dependency>            <groupId>org.codehaus.groovy</groupId>            <artifactId>groovy-eclipse-batch</artifactId>            <version>3.0.8-01</version>          </dependency>        </dependencies>      </plugin>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-jar-plugin</artifactId>        <version>2.6</version>        <configuration>          <includes>            <include>com/**</include>            <include>cn/**</include>            <include>demo/**</include>          </includes>          <excludes>            <exclude>*.txt</exclude>            <exclude>*.xml</exclude>            <exclude>*.properties</exclude>          </excludes>        </configuration>      </plugin>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-assembly-plugin</artifactId>        <version>3.1.0</version>        <executions>          <execution>            <id>make-assembly</id>            <phase>pre-integration-test</phase>            <goals>              <goal>single</goal>            </goals>            <configuration>              <!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可放慢打包速度 -->              <recompressZippedFiles>false</recompressZippedFiles>              <!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 -->              <appendAssemblyId>true</appendAssemblyId>              <!-- 指向打包形容文件 package.xml -->              <descriptors>                <descriptor>src/main/assembly/package.xml</descriptor>              </descriptors>              <!-- 打包后果输入的根底目录 -->              <outputDirectory>${project.build.directory}/</outputDirectory>            </configuration>          </execution>        </executions>      </plugin>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-assembly-plugin</artifactId>        <version>3.1.0</version>        <executions>          <execution>            <id>make-assembly</id>            <phase>pre-integration-test</phase>            <goals>              <goal>single</goal>            </goals>            <configuration>              <!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可放慢打包速度 -->              <recompressZippedFiles>false</recompressZippedFiles>              <!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 -->              <appendAssemblyId>true</appendAssemblyId>              <!-- 指向打包形容文件 package.xml -->              <descriptors>                <descriptor>src/main/assembly/package.xml</descriptor>              </descriptors>              <!-- 打包后果输入的根底目录 -->              <outputDirectory>${project.build.directory}/</outputDirectory>            </configuration>          </execution>        </executions>      </plugin>    </plugins>  </build>  <dependencies>    <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->    <dependency>      <groupId>org.codehaus.groovy</groupId>      <artifactId>groovy-all</artifactId>      <version>3.0.8</version>      <type>pom</type>    </dependency>    <dependency>      <groupId>com.jfinal</groupId>      <artifactId>jfinal-undertow</artifactId>      <version>2.5</version>    </dependency>    <dependency>      <groupId>com.jfinal</groupId>      <artifactId>jfinal</artifactId>      <version>4.9.12</version>    </dependency>    <dependency>      <groupId>ch.qos.logback</groupId>      <artifactId>logback-classic</artifactId>      <version>1.2.3</version>    </dependency>    <dependency>      <groupId>org.projectlombok</groupId>      <artifactId>lombok</artifactId>      <version>1.16.16</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>  </dependencies></project>

2.1.3.JFinalApplication.groovy

package com.litongjavaimport com.jfinal.server.undertow.UndertowServerimport com.litongjava.config.AppConfigimport lombok.extern.slf4j.Slf4j/** * @author create by ping-e-lee on 2021年7月15日 上午3:03:59  * @version 1.0  * @desc */@Slf4jclass JFinalApplication {  static void main(args) {    long start = System.currentTimeMillis();    // 创立server    UndertowServer.create(AppConfig.class,"undertow.properties").start()    long end = System.currentTimeMillis()    String message = (end - start) + "ms"    System.out.println(message);    //Groovy:Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class.    //log.info(message)  }}

2.1.4.AppConfig.groovy

package com.litongjava.configimport com.jfinal.config.Constantsimport com.jfinal.config.Handlersimport com.jfinal.config.Interceptorsimport com.jfinal.config.JFinalConfigimport com.jfinal.config.Pluginsimport com.jfinal.config.Routesimport com.jfinal.template.Engine/** * @author create by ping-e-lee on 2021年7月15日 上午3:00:06  * @version 1.0  * @desc */class AppConfig extends JFinalConfig{  @Override  void configConstant(Constants me) {  }  @Override  void configRoute(Routes me) {    me.scan("com.litongjava.controller.")  }  @Override  void configEngine(Engine me) {  }  @Override  void configPlugin(Plugins me) {  }  @Override  void configInterceptor(Interceptors me) {  }  @Override  void configHandler(Handlers me) {  }}

2.1.5.IndexController.groovy

package com.litongjava.controllerimport com.jfinal.core.Controllerimport com.jfinal.core.Pathimport com.litongjava.utils.LogUtilsimport groovy.util.logging.Slf4j/** * @author create by ping-e-lee on 2021年7月15日 上午3:01:07  * @version 1.0  * @desc */@Path("/")@Slf4jclass IndexController extends Controller{  void index() {    renderText("Hello groovy")  }  void logClassName() {    renderText(log.getClass().getName());  }  void thisClassName() {    renderText(this.toString())  }  void thisClassLoader() {    renderText(this.getClass().getClassLoader().toString())  }  void exception() throws Exception {    try {      throw new IllegalArgumentException("This is a test")    } catch (Exception e) {      //e.printStackTrace();      log.error(LogUtils.getStackTraceInfo(e))    }    renderText("ok")  }}

2.1.6.LogUtils.java

package com.litongjava.utils;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;/** * @author create by ping-e-lee on 2021年7月12日 下午8:36:28  * @version 1.0  * @desc */public class LogUtils {  /**   * 获取e.printStackTrace() 的具体信息,赋值给String 变量,并返回   * @param e Exception   * @return e.printStackTrace() 中 的信息   */  public static String getStackTraceInfo(Exception e) {    /*     * 将出错的栈信息输入到printWriter中     */    StringWriter sw = null;    PrintWriter pw = null;    try {      sw = new StringWriter();      pw = new PrintWriter(sw);      e.printStackTrace(pw);      pw.flush();      sw.flush();      return sw.toString();    } catch (Exception ex) {      return "printStackTrace()转换谬误";    } finally {      if (sw != null) {        try {          sw.close();        } catch (IOException e1) {          e1.printStackTrace();        }      }      if (pw != null) {        pw.close();      }    }  }}

2.1.7.在Eclipse中启动jfinal我的项目

右击JFinalApplication.groovy的main办法-->Run As-->Java Application,启动胜利,如图

2.1.8.查看编译后的class文件

2.2.部署

部署和jfinal的原有的部署形式雷同

2.2.1.生成可部署的产物

进入groovy-ee-jfinal-4.9-hello执行命令上面的命令

mvn clean pre-integration-test -DskipTests

执行胜利悔恨生成groovy-ee-jfinal-4.9-hello-1.0-release.tar.gz
解压

lib文件下的内容如下

ant-1.10.9.jarant-antlr-1.10.9.jarant-junit-1.10.9.jarant-launcher-1.10.9.jargroovy-3.0.8.jargroovy-all-3.0.8.pomgroovy-ant-3.0.8.jargroovy-astbuilder-3.0.8.jargroovy-cli-picocli-3.0.8.jargroovy-console-3.0.8.jargroovy-datetime-3.0.8.jargroovy-docgenerator-3.0.8.jargroovy-ee-jfinal-4.9-hello-1.0.jargroovy-groovydoc-3.0.8.jargroovy-groovysh-3.0.8.jargroovy-jmx-3.0.8.jargroovy-json-3.0.8.jargroovy-jsr223-3.0.8.jargroovy-macro-3.0.8.jargroovy-nio-3.0.8.jargroovy-servlet-3.0.8.jargroovy-sql-3.0.8.jargroovy-swing-3.0.8.jargroovy-templates-3.0.8.jargroovy-test-3.0.8.jargroovy-test-junit5-3.0.8.jargroovy-testng-3.0.8.jargroovy-xml-3.0.8.jarjavaparser-core-3.18.0.jarjavax.servlet-api-4.0.1.jarjboss-logging-3.4.0.Final.jarjcommander-1.78.jarjfinal-4.9.12.jarjfinal-undertow-2.5.jarjline-2.14.6.jarjquery-3.5.1.jarjunit-jupiter-api-5.7.0.jarjunit-jupiter-engine-5.7.0.jarjunit-platform-commons-1.7.0.jarjunit-platform-engine-1.7.0.jarjunit-platform-launcher-1.7.0.jarlogback-classic-1.2.3.jarlogback-core-1.2.3.jaropentest4j-1.2.0.jarpicocli-4.5.2.jarqdox-1.12.1.jarslf4j-api-1.7.25.jartestng-7.4.0.jarundertow-core-2.0.34.Final.jarundertow-servlet-2.0.34.Final.jarxnio-api-3.3.8.Final.jarxnio-nio-3.3.8.Final.jar

2.2.2.启动我的项目

在windows平台上执行jfinal-start.bat即可
最终调用的命令是

java -Xverify:none -cp config;lib\* com.litongjava.JFinalApplication

执行胜利

2.2.3.测试

拜访一些接口测试
http://localhost
http://localhost/thisClassName
http://localhost/thisClassLoader
http://localhost/exception
测试胜利

2.3.代码地址

github
https://github.com/litongjava...
gitee
https://gitee.com/litongjava_...