2015-12-18 144 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