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:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.3.3.RELEASE</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>


dependencies:

<tomcat.version>8.0.32</tomcat.version>

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-el</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-logging-juli</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <version>${tomcat.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>${tomcat.version}</version>
</dependency>

Main:

public class Main {
    public static void main(String[] args) throws ServletException, LifecycleException, IOException {

        String hostName = "localhost";
        int port = 80;
        String contextPath = "";

        String tomcatBaseDir = TomcatUtil.createTempDir("tomcat", port).getAbsolutePath();
        String contextDocBase = TomcatUtil.createTempDir("tomcat-docBase", port).getAbsolutePath();

        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir(tomcatBaseDir);

        tomcat.setPort(port);
        tomcat.setHostname(hostName);

        Host host = tomcat.getHost();
        Context context = tomcat.addWebapp(host, contextPath, contextDocBase, new EmbededContextConfig());

        context.setJarScanner(new EmbededStandardJarScanner());

        ClassLoader classLoader = Main.class.getClassLoader();
        context.setParentClassLoader(classLoader);

        // context load WEB-INF/web.xml from classpath
        context.addLifecycleListener(new WebXmlMountListener());

        tomcat.start();
        tomcat.getServer().await();
    }
}

DemoConfig改造:

public void configEngine(Engine me) {
    me.setBaseTemplatePath("static");
   me.setSourceFactory(new ClassPathSourceFactory());
   me.addSharedFunction("/common/_layout.html");
   me.addSharedFunction("/common/_paginate.html");
}

静态文件:

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!

热门分享

扫码入社