2015-12-18 178 views
5

即時嘗試將我們的構建過程從Maven更改爲Gradle(V 2.9)。在Maven中,我使用瞭如下的JSP預編譯:使用Gradle預編譯JSP

 <plugin> 
      <groupId>org.eclipse.jetty</groupId> 
      <artifactId>jetty-jspc-maven-plugin</artifactId> 
      <version>9.2.7.v20150116</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <id>jspc</id> 
        <goals> 
         <goal>jspc</goal> 
        </goals> 
        <configuration> 
         <excludes>**\/*inc_page_bottom.jsp,**\/*inc_page_top.jsp</excludes> 
         <includes>**\/*.jsp</includes> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

它工作正常。 現在我試圖找到一種方法來做到與gradle相同。 我找到了一些信息/ build.gradle的例子,但沒有任何工作真的。我目前正在使用Tomcat 7作爲servlet容器,並計劃在幾周內切換到8.當然,將它們編譯爲目標servlet容器是完美的,但首先我會很樂意預編譯它們,就像我在做這件事一樣與碼頭的/與碼頭。

我目前的build.gradle的一部分,給了我一個錯誤:

buildscript { 
    repositories { 
     jcenter() 
    } 

    dependencies { 
     classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.4' 
    } 
} 

apply plugin: 'com.bmuschko.tomcat' 

tomcat { 
    jasper { 
     validateXml = true 
     errorOnUseBeanInvalidClassAttribute = false 
     compilerSourceVM = "1.8" 
     compilerTargetVM = "1.8" 
    } 
} 

task compileJsps(type: JavaCompile, dependsOn: 'clean') { 
    dependsOn tomcatJasper 
    group = 'build' 
    description = 'Translates and compiles JSPs' 
    classpath = configurations.tomcat + sourceSets.main.output + sourceSets.main.runtimeClasspath 
    sourceCompatibility = "1.8" 
    targetCompatibility = "1.8" 
    destinationDir = file("$buildDir/jasper-classes") 
    sourceSets { 
     main { 
      java { 
       srcDir "$buildDir/jasper" 
      } 
     } 
    } 
} 

dependencies { 
    def tomcatVersion = '7.0.59' 
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", 
     "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", 
     "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}" 
} 

即時得到以下錯誤:

:tomcatJasper FAILED 
FAILURE: Build failed with an exception. 
* What went wrong: 
Execution failed for task ':tomcatJasper'. 
> org.apache.jasper.JasperException: file:/xxx/xxx/xxx/xxx/src/main/webapp/index.jsp (line: 6, column: 0) The value for the useBean class attribute xxx.xxx.xxx.XxxXxx is invalid. 

運行該JSP的Tomcat中7工作正常... 有人有最新的howto或暗示嗎?

+0

有沒有得到一個答案呢?我在maven中發現了一個<%@ include file =「directives.jsp」%>在服務器上工作但在預編譯上下文中不起作用的問題 –

+0

不幸的是,沒有(並且沒有找到任何解決方案......) – StephanM

+0

我們放棄了對jetty的jdt-core的提升,以獲得與jre 18u112兼容的jasper編譯器 –

回答

1

使用gradle,您可以直接調用JSPC編譯器。這樣就明確了發生了什麼。我們正在使用類似下面的內容。

有幾件事要注意。

  1. 的JSPC編譯器有螞蟻的依賴,因此需要被添加到類路徑
  2. 的JSPC編譯器使用log4j,所以我們創建了一個屬性文件,以輸出一些基本的日誌
  3. 的JSPC編譯嵌入在Jetty中,這是一個「providedCompile」依賴項,因此在我們的運行時類路徑中。

無論如何,我們的gradle看起來類似於;

configurations { 
    jspc 
} 

dependencies { 
    jspc 'org.apache.ant:ant:1.10.1' 
} 

def jspSrc = "$projectDir/src/main/webapp" 
def jspJavaSrc = "$buildDir/jsp-java-source" 
def jspPackage = "com.example.jsp" 

task writeJspcProperties(type: WriteProperties) { 
    outputFile = "$buildDir/log4j.jspc.properties" 
    property('log4j.rootLogger', 'WARN, stdout') 
    property('log4j.logger.org.apache', 'INFO, stdout') 
    property('log4j.appender.stdout', 'org.apache.log4j.ConsoleAppender') 
    property('log4j.appender.stdout.Target', 'System.out') 
    property('log4j.appender.stdout.layout', 'org.apache.log4j.PatternLayout') 
    property('log4j.appender.stdout.layout.ConversionPattern', '%d [%C] %m%n') 
} 

task jspToJava(type: JavaExec, dependsOn: [compileJava, writeJspcProperties]) { 

    inputs.dir jspSrc 
    outputs.dir jspJavaSrc 

    File xmlPartial = file("$jspJavaSrc/WEB-INF/web.xml.partial") 

    doFirst { 
     // Create the target WEB-INF folder so the JspC can create the web.xml.partial 
     File webInfFolder = xmlPartial.getParentFile() 
     if (!webInfFolder.exists()) { 
      webInfFolder.mkdirs() 
     } 
    } 

    classpath = configurations.jspc + sourceSets.main.runtimeClasspath 
    main = 'org.apache.jasper.JspC' 
    jvmArgs "-Dlog4j.configuration=file:$buildDir/log4j.jspc.properties" 

    args '-webapp', jspSrc, 
      '-d', jspJavaSrc, 
      '-p', jspPackage, 
      '-webxmlencoding', 'UTF-8', 
      '-webinc', xmlPartial.absolutePath 

    doLast { 
     // Merge the partial XML with the original 
     String originalXML = file("$jspSrc/WEB-INF/web.xml").text 
     String xmlToMerge = xmlPartial.text 
     String mergedXML = originalXML.replaceFirst('(?s)(<web-app.*?>)', '$1' + xmlToMerge) 
     file("$jspJavaSrc/WEB-INF/web.xml").text = mergedXML 
    } 
} 

[編輯;大大簡化了JSP編譯]

0

在你的主要的build.gradle在底部做

apply from: 'jspc.gradle' 

然後你jspc.gradle文件看起來像:

buildscript { 
    repositories { 
     jcenter() 
    } 


    dependencies { 
     classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.5' 
    } 
} 

apply plugin: com.bmuschko.gradle.tomcat.TomcatPlugin 
apply plugin: 'java' 

dependencies { 
    def tomcatVersion = '8.0.42' 
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", 
      "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", 
      "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}", 
      "javax.servlet:jstl:1.2" 

    providedCompile "javax.servlet:servlet-api:2.5" 

} 

然後使用預編譯做:

gradle tomcatJasper