Jfinal maven jar

大家好。我写这个文章的时候是因为,我在使用的时候走了很多弯路,也很感谢 http://www.jfinal.com/share/422  这篇文章的作者对我的帮助,再次感谢。整理后,我重新处理了下,大概是这样的。 重点声明,这个是对他的那篇文字的补充说明。pom文件用我的。app.class 文件也是可以参考我的。其他参考他的

第一pom文件

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ynwl.clientdata</groupId>
  <artifactId>charge_increment_model</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>charge_increment_model Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

    <!-- 调试工具 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- jfinal 默认工具 -->
    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jfinal</artifactId>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jetty-server</artifactId>
      <version>8.1.8</version>
    </dependency>

    <!-- 数据库驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <!-- 阿里巴巴数据源 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.31</version>
    </dependency>

    <!-- 阿里巴巴JSON -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.36</version>
    </dependency>

    <!-- 日志管理工具 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>

    <!-- 缓存管理工具 -->
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache-core</artifactId>
      <version>2.6.11</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.xiaoleilu/hutool -->
    <dependency>
      <groupId>com.xiaoleilu</groupId>
      <artifactId>hutool</artifactId>
      <version>2.16.2</version>
    </dependency>
    <dependency>
      <groupId>com.xiaoleilu</groupId>
      <artifactId>hutool-all</artifactId>
      <version>3.2.2</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
              <transformers>
                <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <!-- main 函数的入口函数,切勿配置错误 -->
                  <mainClass>config.App</mainClass>
                </transformer>
                <transformer
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>


第二个就是函数需要这样写: 当然这个文件是复制了他的内容: djs19960601 

package config;

import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.xiaoleilu.hutool.util.ZipUtil;

import java.io.IOException;

public class App {
    /**
     * 用于在 IDEA 中,通过创建 main 方法的方式启动项目,不支持热加载
     * 本方法存在的意义在于此方法启动的速度比 maven 下的 jetty 插件要快得多
     * <p>
     * 注意:不支持热加载。建议通过 Ctrl + F5 快捷键,来快速重新启动项目,速度并不会比 eclipse 下的热加载慢多少
     * 实际操作中是先通过按 Alt + 5 打开 debug 窗口,才能按 Ctrl + F5 重启项目
     */
    //用于启动JFinal
    public static void main(String[] args) {

        /**
         * 特别注意:IDEA 之下建议的启动方式,仅比 eclipse 之下少了最后一个参数
         */
        String baseBath = String.valueOf(AppConfig.class.getProtectionDomain().getCodeSource().getLocation());
        String classPath, webRootPath, jarPath;
        if (StrKit.notBlank(baseBath) && baseBath.contains("file:/")) {
            // 获取运行操作系统的运行方式  window和linux的细微区别
            boolean windows = System.getProperties().getProperty("os.name").contains("Windows");
            System.out.println(System.getProperties().getProperty("os.name"));
            jarPath = (windows ? "" : "/") + baseBath.substring("file:/".length());
            classPath = (windows ? "" : "/") + jarPath.substring(0, jarPath.lastIndexOf("/")) + "/class-path";
            System.out.println("jarPath:" + jarPath);
            System.out.println("classPath:" + classPath);
            webRootPath = classPath;

            try {
                ZipUtil.unzip(jarPath, classPath);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 这两步是核心指定 webapp目录和classpath目录
            PathKit.setWebRootPath(webRootPath);
            PathKit.setRootClassPath(classPath);
            // eclipse 启动是4个参数
            JFinal.start(webRootPath, 80, "/");
        } else {
            throw new RuntimeException("你丫的路径不对!");
        }
    }
}

重点是这个方式是可以用Java 7 编译。不用Java 8

项目结构如图:blob.png

AppConfig

不影响:

blob.pngblob.png

ok .应该清楚了吧。


最终生成的文件如图

blob.png