使用groovy开发jfinal-4.9

1.搭建groovy开发环境

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

2.使用groovy开发jfinal-4.9

2.1.开发

2.1.1.目录结构如下

image.png

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.litongjava

import com.jfinal.server.undertow.UndertowServer
import com.litongjava.config.AppConfig

import lombok.extern.slf4j.Slf4j

/**
 * @author create by ping-e-lee on 2021年7月15日 上午3:03:59 
 * @version 1.0 
 * @desc
 */
@Slf4j
class 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.config

import com.jfinal.config.Constants
import com.jfinal.config.Handlers
import com.jfinal.config.Interceptors
import com.jfinal.config.JFinalConfig
import com.jfinal.config.Plugins
import com.jfinal.config.Routes
import 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.controller

import com.jfinal.core.Controller
import com.jfinal.core.Path
import com.litongjava.utils.LogUtils

import groovy.util.logging.Slf4j

/**
 * @author create by ping-e-lee on 2021年7月15日 上午3:01:07 
 * @version 1.0 
 * @desc
 */
@Path("/")
@Slf4j
class 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,启动成功,如图
image.png

2.1.8.查看编译后的class文件

image.png

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
解压
image.png
lib文件下的内容如下

ant-1.10.9.jar
ant-antlr-1.10.9.jar
ant-junit-1.10.9.jar
ant-launcher-1.10.9.jar
groovy-3.0.8.jar
groovy-all-3.0.8.pom
groovy-ant-3.0.8.jar
groovy-astbuilder-3.0.8.jar
groovy-cli-picocli-3.0.8.jar
groovy-console-3.0.8.jar
groovy-datetime-3.0.8.jar
groovy-docgenerator-3.0.8.jar
groovy-ee-jfinal-4.9-hello-1.0.jar
groovy-groovydoc-3.0.8.jar
groovy-groovysh-3.0.8.jar
groovy-jmx-3.0.8.jar
groovy-json-3.0.8.jar
groovy-jsr223-3.0.8.jar
groovy-macro-3.0.8.jar
groovy-nio-3.0.8.jar
groovy-servlet-3.0.8.jar
groovy-sql-3.0.8.jar
groovy-swing-3.0.8.jar
groovy-templates-3.0.8.jar
groovy-test-3.0.8.jar
groovy-test-junit5-3.0.8.jar
groovy-testng-3.0.8.jar
groovy-xml-3.0.8.jar
javaparser-core-3.18.0.jar
javax.servlet-api-4.0.1.jar
jboss-logging-3.4.0.Final.jar
jcommander-1.78.jar
jfinal-4.9.12.jar
jfinal-undertow-2.5.jar
jline-2.14.6.jar
jquery-3.5.1.jar
junit-jupiter-api-5.7.0.jar
junit-jupiter-engine-5.7.0.jar
junit-platform-commons-1.7.0.jar
junit-platform-engine-1.7.0.jar
junit-platform-launcher-1.7.0.jar
logback-classic-1.2.3.jar
logback-core-1.2.3.jar
opentest4j-1.2.0.jar
picocli-4.5.2.jar
qdox-1.12.1.jar
slf4j-api-1.7.25.jar
testng-7.4.0.jar
undertow-core-2.0.34.Final.jar
undertow-servlet-2.0.34.Final.jar
xnio-api-3.3.8.Final.jar
xnio-nio-3.3.8.Final.jar
2.2.2.启动项目

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

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

执行成功
image.png

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_...


李通
215 声望0 粉丝