gradle-tomcat-plugin icon indicating copy to clipboard operation
gradle-tomcat-plugin copied to clipboard

Add support for compiling the Java source obtained by the JSPs

Open mauromol opened this issue 13 years ago • 8 comments

Hi there, your Tomcat plugin is great! I could enhance my Gradle project to translate JSPs to Java source files within minutes.

However, now I'm missing the next (natural?) step: to compile that source code into bytecode, so that I can be sure that my JSPs are correct.

Is there any easy way to do that even right now? Or can your plugin be enhanced to do that?

Thanks in advance.

mauromol avatar May 15 '12 14:05 mauromol

I ended up with writing this:

task compileJsps(type: Compile) { group = 'build' description = 'Translates and compiles JSPs' classpath = configurations.tomcat + sourceSets.main.output + sourceSets.main.runtimeClasspath sourceCompatibility = jasper.compilerSourceVM targetCompatibility = jasper.compilerTargetVM destinationDir = file("$buildDir/jasper-classes") source = jasper.outputDir dependsOn classes dependsOn tomcatJasper }

Please note that the properties of "jasper" (outputDir, compilerSourceVM, compilerTargetVM) must be explicitly set, otherwise it doesn't work. I also added a dependency between the "check" task and the "compileJsps" task, so that the "compileJsps" is executed in the verification stage of the build process.

Problem: if your webapp is compliant with Servlet API 2.3/JSP API 1.2, or with Servlet API 2.4/JSP API 2.0 and you are using Tomcat 6 to translate JSPs into Java code, you must add 'javax.servlet.jsp:jsp-api:2.1' to the "tomcat" configuration, otherwise the source code generated by Jasper from the JSPs won't compile with the JSP API 1.2 or 2.0 used by your script to build the webapp. I guess a similar problem is present if you use Tomcat 7 instead. Unfortunately, there's no Jasper option to drive the JSP translation process with regards to this aspect.

It would be nice if your Tomcat plugin could take care of all this "rough edges" and provide its own compileJsps task..

mauromol avatar May 15 '12 19:05 mauromol

Better solution to the need to add 'javax.servlet.jsp:jsp-api:2.1' dependency to the tomcat configuration: coherently with the other Tomcat lib dependencies declared in the tomcat configuration, "org.apache.tomcat:jsp-api:${tomcatVersion}" could be specified instead of 'javax.servlet.jsp:jsp-api:2.1'.

mauromol avatar May 16 '12 08:05 mauromol

It looks like you already found a temporary solution for your project. Yes, it would be the next natural addition to the plugin. Don't the Tomcat dependencies provide you with the correct JSP JAR as transitive dependencies automatically? I thought so. I personally don't use Jasper a lot but is the compile option of any help in this regard?

bmuschko avatar May 16 '12 10:05 bmuschko

No, the JSP JAR does not come as a transitive dependency, unfortunately. Regarding Jasper, do you mean the compiler option? As per the documentation at http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html, if not specified it should use the default JDT compiler. However IMHO documentation is quite ambiguous, because it's unclear what they mean for "compile". In fact, if you look at the example on that page the outputDir of jasper task is "${webapp.path}/WEB-INF/src" (which suggests that the output is "source" code and not "byte" code) and an additional javac task is shown to do the actual source to byte code compilation of the translated JSPs. So it seems like the Jasper may be able to compile the JSPs (and this might be what Tomcat does at runtime), but that the jasper Ant task alone does not.

Mine is just a guess.

mauromol avatar May 16 '12 12:05 mauromol

Hi Benjamin, I can confirm that with Tomcat 7 it isn't necessary to add the dependency to jsp-api in order to be able to compile the Java code obtained from JSPs: it's only needed when using Tomcat 6.

By the way I noticed a little problem: I created a script that lets the user switch from Tomcat 7 to 6, like this:

dependencies {
    if(tomcatVersion =~ /6(?:\.d+)*/) {
    tomcat "org.apache.tomcat:catalina:${tomcatVersion}",
                 "org.apache.tomcat:coyote:${tomcatVersion}",
                 "org.apache.tomcat:jasper:${tomcatVersion}",
                 "org.apache.tomcat:dbcp:${tomcatVersion}",
                 "org.apache.tomcat:jsp-api:${tomcatVersion}"
    }
    else {
        tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
                     "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
                 "org.apache.tomcat:tomcat-dbcp:${tomcatVersion}"
        tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
            exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
        }
    }       
}

If you run the tomcatJasper task with Tomcat 7, then you change the tomcatVersion to 6 and re-run tomcatJasper, Gradle says the task is up-to-date. If you then try to compile such Java code (generated from JSPs using Tomcat 7 libraries) with Tomcat 6 libraries, you get compilation errors. What I mean is that I think the tomcatJasper task should add the tomcat configuration to its inputs.

mauromol avatar Aug 06 '12 13:08 mauromol

Are you interested in contributing this feature? Otherwise, I am going to close this due to this issue's inactivity.

bmuschko avatar Mar 08 '14 18:03 bmuschko

Hi Benjamin, I would really like to contribute this feature, but I don't know how long will it take. On the other hand, I think that having this feature in the plugin would be an interesting plus, so I won't close this without mercy! :-P Anyway, I really hope to be able to contribute myself.

mauromol avatar Mar 10 '14 08:03 mauromol

I won't get to it anytime soon. It's very low on my priority list. It would be great to get a pull request for this.

bmuschko avatar Mar 11 '14 01:03 bmuschko

Closing as issue got no traction.

bmuschko avatar Sep 24 '23 02:09 bmuschko