gpt4 book ai didi

jsp - 使用 Gradle 预编译 JSP

转载 作者:行者123 更新时间:2023-12-04 12:29:58 26 4
gpt4 key购买 nike

我正在尝试将我们的构建过程从 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 做同样的事情。
我找到了一些 informations/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 工作正常......
有人有最新的操作方法或提示吗?

最佳答案

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

有几点需要注意。

  • JSPC 编译器对 Ant 有依赖性,因此需要将其添加到类路径
  • JSPC 编译器使用 log4j,因此我们创建了一个属性文件来输出一些基本的日志记录
  • 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 依赖项]

    关于jsp - 使用 Gradle 预编译 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354112/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com