使用Gradle预编译JSP

时间:2015-12-18 11:01:07

标签: jsp gradle tomcat7 precompile

我试图将构建过程从Maven更改为Gradle(V 2.9)。在Maven中,我正在使用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容器编译它们当然是完美的但首先我很乐意预编译它们就像我这样做与maven为/与码头。

我当前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.

在Tomcat 7中运行这个jsp工作正常...... 有人有最新的howto或提示吗?

2 个答案:

答案 0 :(得分:2)

使用gradle,您可以直接调用JSPC编译器。这样就可以明确了解到底发生了什么。我们正在使用以下内容。

有几点需要注意。

  1. JSPC编译器依赖于Ant,因此需要将其添加到类路径
  2. JSPC编译器使用log4j,因此我们创建一个属性文件来输出一些基本的日志记录
  3. JSPC编译器嵌入在Jetty中,这是一个“providedCompile”依赖项,因此在我们的运行时类路径中。
  4. 无论如何,我们的gradle看起来类似于;

    configurations {
        jspc
    }
    
    dependencies {
        jspc 'org.apache.tomcat:tomcat-jasper:9.0.17'
        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编译]

    [编辑2;显式添加tomcat-jasper依赖]

答案 1 :(得分:1)

在你的主要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"  

}

然后使用预编译do:

gradle tomcatJasper