jfinal-3.4_demo_for_maven 改造成可运行jar

参考:https://github.com/hengyunabc/executable-embeded-tomcat-sample

使用spring-boot-maven-plugin打包,按照spring-boot规范。

git:https://gitee.com/g-vic/jfinal_demo_for_maven_jar

效果:

1.png

2.png

代码改造:

bom:

  1. <plugins>
  2.     <plugin>
  3.         <groupId>org.springframework.boot</groupId>
  4.         <artifactId>spring-boot-maven-plugin</artifactId>
  5.         <version>1.3.3.RELEASE</version>
  6.         <executions>
  7.             <execution>
  8.                 <phase>package</phase>
  9.                 <goals>
  10.                     <goal>repackage</goal>
  11.                 </goals>
  12.             </execution>
  13.         </executions>
  14.     </plugin>


dependencies:

  1. <tomcat.version>8.0.32</tomcat.version>
  2.  
  3. <dependency>
  4.     <groupId>org.apache.tomcat.embed</groupId>
  5.     <artifactId>tomcat-embed-core</artifactId>
  6.     <version>${tomcat.version}</version>
  7. </dependency>
  8. <dependency>
  9.     <groupId>org.apache.tomcat.embed</groupId>
  10.     <artifactId>tomcat-embed-el</artifactId>
  11.     <version>${tomcat.version}</version>
  12. </dependency>
  13. <dependency>
  14.     <groupId>org.apache.tomcat.embed</groupId>
  15.     <artifactId>tomcat-embed-logging-juli</artifactId>
  16.     <version>${tomcat.version}</version>
  17. </dependency>
  18. <dependency>
  19.     <groupId>org.apache.tomcat.embed</groupId>
  20.     <artifactId>tomcat-embed-websocket</artifactId>
  21.     <version>${tomcat.version}</version>
  22. </dependency>
  23. <dependency>
  24.     <groupId>org.apache.tomcat.embed</groupId>
  25.     <artifactId>tomcat-embed-jasper</artifactId>
  26.     <version>${tomcat.version}</version>
  27. </dependency>

Main:

  1. public class Main {
  2.     public static void main(String[] args) throws ServletException, LifecycleException, IOException {
  3.  
  4.         String hostName = "localhost";
  5.         int port = 80;
  6.         String contextPath = "";
  7.  
  8.         String tomcatBaseDir = TomcatUtil.createTempDir("tomcat", port).getAbsolutePath();
  9.         String contextDocBase = TomcatUtil.createTempDir("tomcat-docBase", port).getAbsolutePath();
  10.  
  11.         Tomcat tomcat = new Tomcat();
  12.         tomcat.setBaseDir(tomcatBaseDir);
  13.  
  14.         tomcat.setPort(port);
  15.         tomcat.setHostname(hostName);
  16.  
  17.         Host host = tomcat.getHost();
  18.         Context context = tomcat.addWebapp(host, contextPath, contextDocBase, new EmbededContextConfig());
  19.  
  20.         context.setJarScanner(new EmbededStandardJarScanner());
  21.  
  22.         ClassLoader classLoader = Main.class.getClassLoader();
  23.         context.setParentClassLoader(classLoader);
  24.  
  25.         // context load WEB-INF/web.xml from classpath
  26.         context.addLifecycleListener(new WebXmlMountListener());
  27.  
  28.         tomcat.start();
  29.         tomcat.getServer().await();
  30.     }
  31. }

DemoConfig改造:

  1. public void configEngine(Engine me) {
  2.     me.setBaseTemplatePath("static");
  3.    me.setSourceFactory(new ClassPathSourceFactory());
  4.    me.addSharedFunction("/common/_layout.html");
  5.    me.addSharedFunction("/common/_paginate.html");
  6. }

静态文件:

3.png


评论区

JFinal

2018-05-13 22:40

又见一种新玩法, 感谢你的分享

JFinal

2018-05-13 22:42

将 webapp 下的 WEB-INF 挪到 resources 下再打包,是一个非常好的思路 jfinal 3.5 会找到一种最佳方案,你给的这个 WEB-INF 挪走的思脉很不错

tang_vi

2018-05-14 15:11

赞了

无为

2018-05-14 19:14

@JFinal 我最擅长拷贝改,IDEA在project模式下不显示webapp,放到resources下比较方便

简单代码

2018-05-15 09:14

@JFinal 尽快实现打包为Jar和SQLite全面支持,如果打包方便,使用JFinal开发本地HTML5应用是多么轻松的事情?期待3.5!

热门分享

扫码入社