【问题标题】:Precompile JSP's with Gradle使用 Gradle 预编译 JSP
【发布时间】:2016-03-25 01:46:19
【问题描述】:

我试图将我们的构建过程从 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 可以正常工作... 有人有最新的操作方法或提示吗?

【问题讨论】:

  • 曾经得到过这个问题的答案吗?我在 Maven 中看到了同样的问题,其中 在服务器上工作但不在预编译上下文中
  • 很遗憾,没有(我自己也没有找到任何解决方案......)
  • 我们也放弃了支持提升 jetty 的 jdt-core 以获得与 jre 18u112 兼容的 jasper 编译器

标签: jsp gradle tomcat7 precompile


【解决方案1】:

使用 gradle,可以直接调用 JSPC 编译器。这样就可以清楚地了解正在发生的事情。我们正在使用类似以下的内容。

有几点需要注意。

  1. JSPC编译器对Ant有依赖,所以需要添加到类路径中
  2. JSPC 编译器使用 log4j,所以我们创建一个属性文件来输出一些基本的日志记录
  3. JSPC 编译器嵌入在 Je​​tty 中,这是一个“providedCompile”依赖项,因此位于我们的运行时类路径中。

无论如何,我们的 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依赖]

【讨论】:

  • 以上代码导致找不到org.apache.jasper.JspC的类。将jspc 'org.apache.tomcat:tomcat-jasper:9.0.17' 添加到依赖项可以解决这个问题。
  • 是的,说得好。我已经在我的类路径中有它,所以这不是问题。已更新以反映这一点。
【解决方案2】:

在你的主 bu​​ild.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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多