Apache freemarker不会使用gradle

时间:2018-06-06 18:28:32

标签: java gradle freemarker cloud9-ide cloud9

我有一个java web应用程序,我正在使用cloud9,我已经将Apache freemarker设置为模板引擎。要导入我使用gradle的包,但是当我运行gradle build时,由于在我的代码中引用了freemarker包,我得到了异常。当我在import语句中注释掉freemarker的所有用法时,我的gradle将构建我的代码,但是当我运行它时会抛出错误(freemarker包不存在)。我怎样才能解决这个问题? App.java

import freemarker.template.*;
import com.sun.net.httpserver.HttpServer;
import java.net.*;
import java.util.*;
public class App{
    public static Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
    int port;
    public static void main(String[] args){
        App app = new App(8081);
        try{
            app.init();
            app.templateConfig();
        }catch(Exception e){
            System.out.println(e);
        }
    }
    public App(int port){
        this.port = port;
    }
    public void init() throws Exception{
        HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
        System.out.println("server started at " + port);
        server.createContext("/", new HomeController.Index());
        server.createContext("/echoHeader", new HomeController.EchoHeaderHandler());
        server.createContext("/echoGet", new HomeController.EchoGetHandler());
        server.createContext("/echoPost", new HomeController.EchoPostHandler());
        server.setExecutor(null);
        server.start();
    }
    public void templateConfig() throws Exception{
        cfg.setDirectoryForTemplateLoading(new File("../templates"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHanlder.HTML_DEBUG_HANDLER);
        cfg.setLogTemplateExceptions(false);
        cfg.setWrapUncheckedExceptions(true);
    }
}

HomeController.java

import freemarker.template.*;
import com.sun.net.httpserver.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class HomeController{
    public static class Index implements HttpHandler{
        public void handle(HttpExchange exchange) throws IOException{
            //String response = "<h1>Server start success if you see this message</h1><h1>Port: " + 8081 + "</h1>";
            exchange.sendResponseHeaders(200, response.length());
            OutputStream os = exchange.getResponseBody();
            //os.write(response.getBytes());
            Map<String, Object> root = new HashMap<>();
            root.put("port", 3000);
            Template home = App.cfg.getTemplate("home.ftlh");
            home.process(root, os);
            os.close();
        }
    }
    public static class EchoHeaderHandler implements HttpHandler{
        public void handle(HttpExchange exchange){

        }
    }
    public static class EchoGetHandler implements HttpHandler{
        public void handle(HttpExchange exchange){

        }
    }
    public static class EchoPostHandler implements HttpHandler{
        public void handle(HttpExchange exchange){

        }
    }
}

的build.gradle

repositories {
    mavenCentral()
}
apply plugin: "java"
dependencies {
    compile "org.freemarker:freemarker:2.3.28"
}
sourceSets {
    main.java.srcDir "src/main"
}
jar {
    from configurations.compile.collect { zipTree it }
    manifest.attributes "Main-Class":"com.isaackrementsov.app.App"
}

我的目录结构

workspace
  build
    classes
      java
        main
          //Source code compiles here
    libs
    tmp
      compileJava
        jar
  src
    main
      com
        isaackrementsov
          app
            //Source code here

我的整个项目都在这里:Cloud9 Project

0 个答案:

没有答案