Precompile JSP with Gradle

im trying to change our build process from Maven to Gradle (V 2.9). In Maven, I used precompilation for JSP as follows:

<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> 

and it worked fine. Now I'm trying to find a way to do the same with gradle. I found some examples of /build.gradle, but nothing works. Im currently using Tomcat 7 as a servlet container, and I plan to switch in a few weeks to 8. It would be ideal, of course, to compile them for the target servlet container, but first of all I would be happy to copy them first, as if I did it with maven for / with a pier.

Part of my current build.gradle that gives me an error:

 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}" } 

Im getting the following error:

 :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. 

Running this jsp in Tomcat 7 works fine ... Does anyone have an updated guide or tooltip?

+5
source share
3 answers

With gradle, you can directly call the JSPC compiler. Therefore, it is clear what is happening. We use something like the following.

A few things to note.

  1. The JSPC compiler depends on Ant, so you need to add it to the classpath.
  2. The JSPC compiler uses log4j, so we create a properties file to display some basic logs
  3. The JSPC compiler is built into Jetty, which is a dependency of "provide Compile", therefore, on our class path at runtime.

Anyway, our gradle looks something like this;

 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 } } 

[Edit; greatly simplified compilation of JSP]

[Edit 2; explicitly add tomcat-jasper dependency]

+2
source

In your main build.gradle file below

 apply from: 'jspc.gradle' 

Then your jspc.gradle file looks like this:

 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" } 

then to use precompile do:

 gradle tomcatJasper 
+1
source

Can someone please let me know how to use the jetty-jspc-maven-plugin in the Gradle file to precompile and pack the precompiled jsp files into a war file. I can do the same using pom.xml using jetty-jspc-maven-plugin. But my project is a gradle project.

0
source

Source: https://habr.com/ru/post/1238628/


All Articles