Tuesday, April 04, 2006

How to pre-compile JSP pages for Tomcat with Ant

JSP pre-compilation is very important part of the build process. Even if you don't want to use pre-compiled pages in your production environment (it's for specific application server (AS) only), it can guarantee the integrity of your source.

With regular approach you make changes to JSP pages, then copy/deploy them to AS and only at this time possible error will be discovered. In contrast, JSP pre-compilation will give you this knowledge at compile time.


How to do the pre-compilation? I will explain it with the help of Ant build tool.


1. Specify "project.classpath" for your project. It will include all jars required to compile or run your project:


<path id="project.classpath">
...
</path>


2. Specify "tomcat.jsp.classpath". You need to have Tomcat Web Server installed somewhere. For this example I use tomcat, embedded into JBoss Application Server ("${jboss.home}").

You also need to specify where you have your implementation of logging system
("${repository.home}/log4j").


<path id="tomcat.jsp.classpath">

<!-- 1. You have to include jars from your project. -->

<path refid="project.classpath"/>

<!-- 2. Tomcat jsp compiler (jasper) by itself. -->

<fileset dir="${jboss.home}/server/default/deploy/jbossweb-tomcat55.sar">
<include name ="jasper-compiler.jar"/>
<include name ="jasper-compiler-jdt.jar"/>
<include name ="jasper-runtime.jar"/>
<include name ="commons-logging.jar"/>
<include name ="commons-el.jar"/>
<include name ="jsp-api.jar"/>
</fileset>

<!-- 3. This library is required by jasper. -->

<fileset dir="${ant.home}/lib">
<include name ="ant.jar"/>
</fileset>

<!-- 4. JavaEE/Servlet/JSP interface classes. -->

<fileset dir="${jboss.home}/server/default/lib">
<include name ="jboss-j2ee.jar"/>
<include name ="javax.servlet.jar"/>
<include name ="javax.servlet.jsp.jar"/>
</fileset>

<!-- 5. Implementation of logging system (if it is not in "project.classpath" yet). -->

<fileset dir="${repository.home}/log4j">
<include name ="log4j-1.2.8.jar"/>
</fileset>

</path>


3. Now we can generate Java sources for JSP files:


<property name="jsp.src.dir" value="<the root for your JSP files>"/>
<property name="jsp.package.name" value="<the package name for your JSPs, like com.mycompany.jsp>"/>

<property name="build.dir" value="target/build"/>
<property name="jsp.generated.src.dir" value="${build.dir}/jsp_sources"/>
<property name="jsp.classes.dir" value="${build.dir}/jsp_classes"/>

<target name="tomcat.jsp.generate">
<mkdir dir="${jsp.generated.src.dir}"/>
<mkdir dir="${jsp.classes.dir}"/>

<java classname="org.apache.jasper.JspC" fork="yes">
<classpath refid="tomcat.jsp.classpath" />

<arg line="-uriroot ${jsp.src.dir} -d ${jsp.generated.src.dir} -p ${jsp.package.name} -webapp ${jsp.src.dir}" />
</java>
</target>


4. After the generation of Java classes from JSPs, we need to compile them. The "depend" task is responsible for "smart" recompilation of JSP-Java files.


<target name="tomcat.jsp.compile" depends="tomcat.jsp.generate"
description="Generates java classes from jsp files, then compiles them">
<depend srcdir="${jsp.src.dir}"
destdir="${jsp.classes.dir}"
cache="${build.dir}"
closure="true">
<classpath refid="tomcat.jsp.classpath"/>
</depend>

<javac destdir="${jsp.classes.dir}" debug="off" optimize="false">
<classpath refid="tomcat.jsp.classpath"/>

<src path="${jsp.generated.src.dir}"/>
<include name ="com/**/*.java"/>
</javac>
</target>


If on this step you don't have compiler errors, your source code (JSP part) is not broken.

55 comments:

Anonymous said...

Hi,

Excellent article, this has saved me a lot of time, so thanks.

One thing I noticed, in the depend tag, shouldn't srcdir be jsp.generated.src.dir (where the generated java source is) instead of jsp.src.dir (where the JSP files are)?

From my understanding of the depend tag, that's the folder the depend task will need to scan to find dependencies.

Neale Rudd

Anonymous said...

Hi,
We are going to create a pre-compile target for our web application, as part of our ANT build procedures. We are using Jboss4.0.5 ( Tomcat 5.5 bundled ) and Ant 1.6.

The directory tree of the application is as follows:
/project
|--- > /war/pages/jsp ( it contains all the jsp files )
|----> /war/WEB-INF ( it contains the web.xml file.)
|----> /war/WEB-INF/tld ( it contains the tld files)
|----> /war/WEB-INF/src ( it would coutain the generated src file from jsp )
|---->/war/WEB-INF/classes ( it contains the classes from src. )
|----> build.xml ( the ant script )

But it does not work. There are two issues:
1. "file /WEB-INF/web.xml not found."
That was the first message I received when the ant script is executed. Although it is a just a warning, it would affect lots of jsp files. That is because I have such clauses in this web.xml:
"
/tags/wandengine
/WEB-INF/wandengine.tld
",
if the web.xml was not found, this jsp files would not be generated.
Any idea what I did wrong ?


2.Some java files did generated, but with a weird "005f", such as "inc_005fcommons_005fjsp.java".
Where does this 005f coming from, how can I excluded it from the generated files ?

Thanks a lot


Charlie

Anonymous said...

Hi,
Even I am getting the same error
WEB-INF/web.xml not found and it is unable to file the tag library directory though it exists in the WEB-INF/classes/TagLib Name .

Did you find a solution for this.
If so Please post the same here

Unknown said...

There is a mistake at this line:


<arg line="-uriroot ${jsp.src.dir} -d ${jsp.generated.src.dir} -p ${jsp.package.name} -webapp ${jsp.src.dir}" />


After -webapp, you have to specify the root of your web application

Anonymous said...

That only works for tomcat 1.4.
Is there anyway to make it work under tomcat 6.0?

Anonymous said...

It was very interesting for me to read this blog. Thank author for it. I like such topics and anything that is connected to them. I definitely want to read a bit more on that blog soon.

Anonymous said...

Great article as for me. I'd like to read something more about that matter.
BTW check the design I've made myself A level escorts

heilkräuter said...

When Tomcat starts up, it kicks
off a servlet that will precompile all of the JSP pages. This servlet
pings every single JSP page in the website. When the JSP page is
pinged, it is forced to compile. It takes nearly 2 hours to compile
all of the JSP pages using this servlet. The server is a solaris 8
machine with a 333 MHz Processor
and 128 Mb of RAM.

Anonymous said...

surat michele explorers annotated hkkjr complaint departments grindekso quarterly dermal items
masimundus semikonecolori

Anonymous said...

By the way, buy GSM blocker to jam all secret devices in your home or office.

Anonymous said...

great post, just the kind of information I was looking for

Anonymous said...

I have to hear what Wendell will do about this :D


hannan insurance

Anonymous said...

If I could save time in a bottle the first thing that I'd like
to do is to save every day until hiehwfdh eternity passes away just to spend them with you
If I could make days last forever if words could make.
Wholesale New Era Hats
Cheap 59fifty Hats
Cheap New Era Hats
New Era Snapback Hats
New Era Fitted Hats

Anonymous said...

If I could save time in a bottle the first thing that I'd like
to do is to save every day until hiehwfdh eternity passes away just to spend them with you
If I could make days last forever if words could make.
Wholesale New Era Hats
Cheap 59fifty Hats
Cheap New Era Hats
New Era Snapback Hats
New Era Fitted Hats

price per head online said...

Well this is my first visit. Great stuff over the blog. Illustration has something special and I think we can't avoid this kind of initiative.

call center voip solutions said...

Beautifully expressed article, i really enjoyed my stay on this plus wonderful website

Anonymous said...

hi!,I reallу like your writing very much! ѕhare wе be in contact more approхimatelу your post on ΑOL?
I requirе an expеrt οn this house to unraνel my
problem. Maybe that's you! Taking a look ahead to look you.

Take a look at my web blog: premature ejaculation pills

Anonymous said...

Ahaa, its nice conversation on the topic of this paragraph
at this place at this web site, I have read all that, so now me also commenting
at this place.

Feel free to surf to my web-site - コーチバッグ

Anonymous said...

Yes! Finally something about クロエ アウトレット.


Feel free to visit my blog post http://www.025bihua.com/

Anonymous said...

It's an remarkable piece of writing designed for all the web people; they will obtain benefit from it I am sure.

Here is my homepage :: www.coachhandbagsoutletshop2013.com

Anonymous said...

Good day! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me.
Nonetheless, I'm definitely delighted I found it and I'll be
book-marking and checking back frequently!

Take a look at my webpage: 財布トリーバーチ

Anonymous said...

Greetings! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone 4. I'm trying
to find a theme or plugin that might be able to correct this issue.
If you have any recommendations, please share. Thank you!


My blog - トリーバーチ店舗

Anonymous said...

Way cool! Some extremely valid points! I appreciate you writing this post and the rest of the site is also really good.



Here is my page; chloe バッグ

Anonymous said...

It's in fact very difficult in this busy life to listen news on TV, therefore I only use the web for that reason, and obtain the hottest news.

My homepage; トリーバーチ財布

Anonymous said...

Howdy! I simply want to offer you a huge thumbs up for your excellent information you have right here on this post.
I am returning to your site for more soon.

my site ... トリーバーチ財布

Anonymous said...

Hello there, just became alert to your blog through Google, and found that it is really informative.
I'm gonna watch out for brussels. I'll be grateful
if you continue this in future. Many people
will be benefited from your writing. Cheers!

Visit my webpage クロエ店舗

Anonymous said...

Focus on your own want to be when the difference is complete.

Now we all know you are worried about what the difference between the two is definitely.
It has a rubber outsole but also presents the most effective
quantity of usage and durability. Jessica Simpson,
Kate Hudson, Cameron Diaz, just to name a few. http://buzzedforum.

freehostia.com/index.php?action=profile&u=9825

my blog; nike womens air max

Anonymous said...

Co- branding starts with endorsements with fames and
institutions. Historically, you rapidly realize several such cases attributable
to Nike. This article a person with information on the way
to size various running shoes in the right way. it's flexible grooves to enhance the articulation and flexibility. http://www.beaba.info/modules.php?name=Your_Account&op=userinfo&username=MaureenTa

Anonymous said...

My parents gave me a pair of Nike Air Max as a existing.
Attempt to alter it as much as terrific then go on
to receive a series of photographs.

My blog post; white air max

Anonymous said...

Thanks for an thought, you sparked at thought from a angle I hadn’t given thoguht to yet.
Now lets see if I can do something with it.


Feel free to surf to my web page: how to increase chances of getting pregnant

Anonymous said...

I was curious if you ever considered changing the structure of your website?
Its very well written; I love what youve got to say. But maybe you could a little more in
the way of content so people could connect with it better.
Youve got an awful lot of text for only having one or 2 images.
Maybe you could space it out better?

my web blog: コーチバッグ

Anonymous said...

Every weekend i used to pay a quick visit this web page, for
the reason that i wish for enjoyment, since this this web page conations really
pleasant funny information too.

Here is my site; クリスチャンルブタン

Anonymous said...

Awesome article.

My webpage www.e-niemiecki.pl

Anonymous said...

Thanks for a marvelous posting! I really enjoyed reading it, you may be a great author.
I will make sure to bookmark your blog and may come back later
on. I want to encourage that you continue your great job, have a nice weekend!


Here is my weblog トリーバーチバッグ

Anonymous said...

This post will assist the internet visitors for building up new website or even a blog from start to end.


Feel free to surf to my website :: www.rolexwatchoutletsale.com

Anonymous said...

I’m not that much of a online reader to be honest but your
blogs really nice, keep it up! I'll go ahead and bookmark your site to come back down the road. Cheers

Here is my webpage; prada バッグ

Anonymous said...

Hi, Neat post. There is a problem with your site in internet explorer,
might test this? IE nonetheless is the marketplace leader and a large section of folks will pass over your fantastic writing because
of this problem.

Also visit my web-site プラダバッグ

Anonymous said...

Have you ever thought about publishing an e-book or guest authoring on other sites?
I have a blog centered on the same ideas
you discuss and would really like to have you share some stories/information.

I know my readers would appreciate your work. If you are even remotely interested, feel free
to shoot me an e mail.

Here is my website ... gucci バッグ

Anonymous said...

Tremendous things here. I'm very satisfied to look your article. Thank you so much and I am looking ahead to touch you. Will you kindly drop me a mail?

Have a look at my website ... モンスター ヘッドホン

Anonymous said...

Pretty! This has been an extremely wonderful post.
Thanks for supplying this info.

Here is my blog post air jordan

Anonymous said...

Hi everybody, here every one is sharing such experience, so it's fastidious to read this webpage, and I used to pay a quick visit this webpage every day.

My webpage; ジョーダン

Anonymous said...

Appreciation to my father who informed me about this weblog, this web site is really remarkable.


my page; アバクロンビーフィッチシャツ

Anonymous said...

they have flexible grooves enhance the articulation and suppleness.
There are various people who feel nostalgic whenever they come across
1985 Air jordans. You can choose from a huge
range of nike air jordan shoes depending on
your requirement. So you have a custom nike Dunk SB and moreover a new survey for it.
http://www.camotweet.com/FletcherPa

Anonymous said...

Nonetheless , it can also be very fulfilling and exciting.
Tennis and women's layout for too long has been said to be an oxymoron. It has a high-resolution multi-touch more effective inch screen having a wide viewing angle. Think of some sort of nike swoosh and simply the Toyota symbolic representation. http://www.veritaevisioni.it/author/DaniellaK

my page :: women air max

Anonymous said...

No Green, No Play ELLE
No Green, No PlayMarch 19 11:06 AMby ELLE 0 Comments Trovata's Polish [url=http://english.ipp.ac.cn/coachoutlet365.html]coach outlet[/url] language flash card lookbook. Too cute. Late last week, I went down to SoHo to see the various collections at Williamson PR. Among them was the new fall 07 Trovata line headed up by the oh-so-cute John Whitledge. Trovata always comes out with these quirky, adorable, high-quality clothes that have an entire storyline of curiously compelling characters behind them. It's always slightly eccentric and highly original. This fall, the collection is called Ladles and Letters and it's about Bridget Ann Davis (totally fictional character), a high school Polish language teacher in 1981 who runs a soup kitchen for the homeless in her spare time and the man who wants to turn the soup kitchen's building into a museum dedicated to the historical craft of printing. A battle of wills is waged and the story leaves off asking "will their disdain for each other win out over a budding attraction?" I mean, who wouldn't want to wear Trovata after that? There's something great about dressing up in clothes with a story, isn't there? I think that's why people love vintage. And why they have so much fun on holidays when it's required to [url=http://english.ipp.ac.cn/coachoutlet365.html]coach outlet store online[/url] wear something costume-y like Halloween or St. Patrick's Day (when you at least have to wear something green). Speaking of St. [url=http://english.ipp.ac.cn/coachoutlet365.html]coach outlet store online[/url] Pat's Day, my friend Jessica's mom always throws the most lavish, extravagant parties on every occasion and this Saturday was no exception. I arrived at Peggy's gorgeous Jamie-Drake-decorated apartment on the Upper East Side (where one of her closets is the size of one of my.....uh.....okay, the size of my entire apartment. Seriously.) where I was immediately decorated with green clover stickers on my face. After drowning ourselves in corned beef and champagne/beer/wine/vodka, a group of us headed out to the bars. We Irish-jigged our way down Second Avenue, where everyone was decked out in green plastic hats and such, and then arrived at Piano's on the Lower East Side where apparently NO ONE got the "It's St. Patty's Day so wear green" memo, but where guys still proceeded to approach us trying to use OUR green accoutrements as a conversation starter. And to that, I had just a few things to say: No green, no play buddy. Or my favorite: If you're not wearing green, you're at a red light. Wisely, I stopped myself from actually saying these things out loud, and went with the simple but pointed: WHERE'S YOUR HOLIDAY SPIRIT?!! I mean, really. Tags:

Anonymous said...

Thank you for the auspicious writeup. It in truth was
once a leisure account it. Glance complicated to more introduced agreeable from you!
By the way, how could we communicate?

my web page; cheap nike air max

Anonymous said...

Thanks for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your next write ups thanks once again.


Check out my web page: cheap nike air max

Anonymous said...

I was curious if you ever thought of changing the page layout of your website?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having one or 2 images.
Maybe you could space it out better?

my web blog: cheap nike air max shoes

Anonymous said...

Howdy, i read your blog from time to time and
i own a similar one and i was just wondering if you get a lot
of spam responses? If so how do you prevent it, any plugin or anything you
can advise? I get so much lately it's driving me insane so any assistance is very much appreciated.

My web page: cheap nike air max

Anonymous said...

Ι am nο longer suге where you're getting your information, however great topic. I needs to spend a while finding out much more or working out more. Thanks for wonderful info I was looking for this information for my mission.

Feel free to visit my weblog; party pills cheap

Anonymous said...

Anyone ever been motivated to fill in net questionnaire or
take part in the a satisfaction stare at? Line-code
of bright stripes soon grows to be to the hallmark of the brand name
name. Layering creates bulk that is not only uncomfortable, but is also very unflattering.
Annually he owns two hundred shops in Asia. http://wiki.
edu54.ru/index.php/Typically_The_Top_7_Guys_Fashion_Shops_From_The_Uk

Unknown said...

Thanks for sharing the information. It is very useful for my future. keep sharing
Signature:
download descargar facebook gratis para Android celular and download free descargar facebook apk en español, descargar facebook plus , descarga facebook

Unknown said...

I am happy to find this post very useful for me
Signature:
Jugar frozen juegos en línea gratis, los nuevos de princesa de Disney juegos frozen - la princesa encantadora y linda. Divertirse frozen!

Unknown said...

Thanks for sharing the information. It is very useful for my future. keep sharing
Signature:
download baixar facebook movel, Facebook movel, baixar facebook movel. Facebook chat, baixar whatsapp gratis, fazer o download baixar whatsapp para Android, iPhone. Últimas Facebook

Unknown said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.

kid games
friv2
unblocked games 77
juego de un show mas