关于java:eclipse使用groovy开发jfinal49

28次阅读

共计 8096 个字符,预计需要花费 21 分钟才能阅读完成。

应用 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.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, 启动胜利, 如图

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

执行胜利

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

正文完
 0