aspose file tools*
The moose likes Servlets and the fly likes Deploying servlets as a war file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Deploying servlets as a war file" Watch "Deploying servlets as a war file" New topic
Author

Deploying servlets as a war file

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Hi all,

I've got a query on the way web applications are deployed. I have an application that has several jsp files and serlvets that are in WEB-INF/classes.

When i deploy the application all i do is do a checkout on CVS, make the changes, rebuild the war using ANT and redeploy manually. This works fine but now i am having some doubts on this approach.

Basicaly if i even change one file i have to rebuild the war file meaning i have to recompile everything. I think this is unecessary. i would like to be able to just recompile the one file and redeploy the one file without having to rebuild the whole war file.

1. How do you deploy your application? If you deploy as a war file, do you recompile everything even if the change is to a single file or a configuration file.

2. Could you provide an example on the approach you use from the point a source file comes out from CVS to the point it get to the production environment. (An example with a single file change would be usefull)

2. Do you use any tool to manage this?

I am thinking that if i change one file i should be able to just compile that one file and move it to the WEB-INF/classes folder into the relevant folder. This might work but is there a better way of doing this instead of moving it manually?


Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

1. Yes
2. Just let Ant do its thing
2. Ant


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Bear Bibeault wrote:1. Yes
2. Just let Ant do its thing
2. Ant


Hi could you point me to an example?

Thanks
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Bascially, this is done by a Build tool (so that the manual errors are avoided to the maximum). The build tools need the inputs to do its thing (as Bear specified above) and that is all!

Ant is one famous tool. Look at http://ant.apache.org . One extra info is : Ant is purely Java based. Hence its 100% compatible with Java application

You can go through the website itself for an excellent tutorial and other useful information.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

O. Ziggy wrote:Hi could you point me to an example?

Not really -- it's not necessary to show you a huge Ant build script.

Just rebuild the war file using Ant and stop worrying about it.


O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Bear Bibeault wrote:
O. Ziggy wrote:Hi could you point me to an example?

Not really -- it's not necessary to show you a huge Ant build script.

Just rebuild the war file using Ant and stop worrying about it.





This is what i currently do i just rebuild everything and out pops out a war file.
And it does work without any problems.




What i am trying to get at is, i dont want to recompile every single source file everytime a file changes. Which is what the above is doing.

The reason i dont want to do recompile is because there are several modules/subsystems that make up the war file. Everytime i rebuild a war file i have to run several tests on all modules because everything has been recompiled.

Now if i can only deploy a file for a single module e.g. By just updating the war file with the one file change, i wont have to re-test every other module.

Thanks
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Raghavan Muthu wrote:Bascially, this is done by a Build tool (so that the manual errors are avoided to the maximum). The build tools need the inputs to do its thing (as Bear specified above) and that is all!

Ant is one famous tool. Look at http://ant.apache.org . One extra info is : Ant is purely Java based. Hence its 100% compatible with Java application

You can go through the website itself for an excellent tutorial and other useful information.


I do use Ant. My question was whether it was possible to update a war file using Ant without having to recompile everything.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

Ant should only be rebuilding the changed files. Otherwise, there's a problem with your script.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Bear Bibeault wrote:Ant should only be rebuilding the changed files. Otherwise, there's a problem with your script.


Thats interesting.


There is a clean task that runs first before the compilation.



Do you mean that if i dont delete the class files, the recompilation will not occur? What would happen if a class already exists? Does it not recompile and overwrite?
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Read the ant manual documentation on Javac task:

The source and destination directory will be recursively scanned for Java source files to compile. Only Java files that have no corresponding .class file or where the class file is older than the .java file will be compiled.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

There are also some stubble cases (specially if you use static constants) that might create a problem if you don't do a complete build.

Personally I would prefer doing a clean build - at least for the script that is creating the war file for production.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
gaurav kant
Greenhorn

Joined: Oct 06, 2008
Posts: 14
As i think, if you simply run ANT with out clean command, it builds only modified classes, other classes remain untouched. But this is not a good practice if you are building it for your production. you should clean it every time before creating WAR file. because some times even modified classes do not get any change (i have faced many times)
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks all for the responses.

In what situations might it cause a problem to not go for a clean rebuild? How is this likely to cause a problem. Any example?

Thanks
gaurav kant
Greenhorn

Joined: Oct 06, 2008
Posts: 14
as i think, you can use ANT with out "clean" command when you are not doing any serious deployment like testing or when work is going on (i.e. coding and you are just checking your code working). But you must go for complete clean and build your project when you want to create WAR for production or deployment on server.
Please correct me if m wrong at any point
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

gaurav kant wrote: But you must go for complete clean and build your project when you want to create WAR for production or deployment on server.
Please correct me if m wrong at any point


What is the reason for this? If a file has been compiled why is there a need to recompile it? Especially if it is for a different package/module why recompile it?
gaurav kant
Greenhorn

Joined: Oct 06, 2008
Posts: 14
There is no need to recompile a file that has been compiled earlier. This is for the sake of surety that you have latest classes in you build. Suppose you have a vast number of files and you had made some changes in say 60 or 70 files and if you by chance forget to copy 1 or 2 files in your build, what would be the result ? For this we just compile all the files not to leave even a single file and i think there is no harm to do this just before production. what do you think?

one more thing. ANT compiles only those file that have been modified and never touch unmodified files. clean means delete all those files that you have specified in command (in ant script) from build and run ant command by which ant can recompile and place into your build all those deleted files just to ensure that you have latest code in build.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

O. Ziggy wrote:Thanks all for the responses.

In what situations might it cause a problem to not go for a clean rebuild? How is this likely to cause a problem. Any example?

Thanks


Also there is one particular case that i can think of that might cause a problem.

Lets say you have a File called Constants.java with a line like,
public static final myName="Sam";

and you are using this constant in another file say xyz.java
print(Constants.myName);

Now you go and make a change to Constants.java and change 'Sam' to 'O.Ziggy'.

Now lets say you run the compile task. This means that your constants file would be recompiled since it has a newer timestamp than the class file. However if the xyz.java file would not be recompiled. (I'm not 100% - does ant work intelligently and re-compile all dependent files?)

If you run such application the see the output - you'll see 'Sam' getting printed instead of 'O.Ziggy'.

--

Another possible pain area. Say you change your system time for some reason while working on a java file. You save the file and run your build script - Since the timestamp of the saved file is older than the time stamp of the compiled file - it won't be re-compiled.

--

Some of the problems I mentioned are stretched too for - but that's why I prefer doing a clean & then build for any production going code - Added safety against some weird and hard to debug cases!
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

gaurav kant wrote:There is no need to recompile a file that has been compiled earlier. This is for the sake of surety that you have latest classes in you build. Suppose you have a vast number of files and you had made some changes in say 60 or 70 files and if you by chance forget to copy 1 or 2 files in your build, what would be the result ? For this we just compile all the files not to leave even a single file and i think there is no harm to do this just before production. what do you think?

one more thing. ANT compiles only those file that have been modified and never touch unmodified files. clean means delete all those files that you have specified in command (in ant script) from build and run ant command by which ant can recompile and place into your build all those deleted files just to ensure that you have latest code in build.


Yes i am thinking for situations where i have only modified one or two files. In situations where i have modified hundreds of files then it wouldnt make sense to do a partial compilation.

Basically i have 3 modules in my application one of which is dependent on by the other two. What i am thinking of doing is if the change is on only one module then there shouldnt be any problems just recompiling the file in that one module. If a file from each module is changed then i should rebuild everything.

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sam Mercs wrote:
O. Ziggy wrote:Thanks all for the responses.

In what situations might it cause a problem to not go for a clean rebuild? How is this likely to cause a problem. Any example?

Thanks


Also there is one particular case that i can think of that might cause a problem.

Lets say you have a File called Constants.java with a line like,
public static final myName="Sam";

and you are using this constant in another file say xyz.java
print(Constants.myName);

Now you go and make a change to Constants.java and change 'Sam' to 'O.Ziggy'.

Now lets say you run the compile task. This means that your constants file would be recompiled since it has a newer timestamp than the class file. However if the xyz.java file would not be recompiled. (I'm not 100% - does ant work intelligently and re-compile all dependent files?)

If you run such application the see the output - you'll see 'Sam' getting printed instead of 'O.Ziggy'.

--

Another possible pain area. Say you change your system time for some reason while working on a java file. You save the file and run your build script - Since the timestamp of the saved file is older than the time stamp of the compiled file - it won't be re-compiled.

--

Some of the problems I mentioned are stretched too for - but that's why I prefer doing a clean & then build for any production going code - Added safety against some weird and hard to debug cases!


This is interesting and i didnt realise that would happen. Ill have to test it out and see.
What is the reason behind it? Surely if i recompile Constants.java the class file will be updated which is what xyz.java will be using?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sam Mercs wrote:
O. Ziggy wrote:Thanks all for the responses.

In what situations might it cause a problem to not go for a clean rebuild? How is this likely to cause a problem. Any example?

Thanks


Also there is one particular case that i can think of that might cause a problem.

Lets say you have a File called Constants.java with a line like,
public static final myName="Sam";

and you are using this constant in another file say xyz.java
print(Constants.myName);

Now you go and make a change to Constants.java and change 'Sam' to 'O.Ziggy'.

Now lets say you run the compile task. This means that your constants file would be recompiled since it has a newer timestamp than the class file. However if the xyz.java file would not be recompiled. (I'm not 100% - does ant work intelligently and re-compile all dependent files?)

If you run such application the see the output - you'll see 'Sam' getting printed instead of 'O.Ziggy'.

--

Another possible pain area. Say you change your system time for some reason while working on a java file. You save the file and run your build script - Since the timestamp of the saved file is older than the time stamp of the compiled file - it won't be re-compiled.

--

Some of the problems I mentioned are stretched too for - but that's why I prefer doing a clean & then build for any production going code - Added safety against some weird and hard to debug cases!



Hi,

I did try the test for the statics using the following classes




The output after compiling both files was



I then made a change to TestClass and recompiled just TestClass and the output was




So it looks like the change does get picked up without having to recompile the dependant class. Is there any situation where this wont work?
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
O. Ziggy wrote:Is there any situation where this wont work?


It would not work for static final, as Sam Mercs wrote above
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
O. Ziggy wrote:2. Could you provide an example on the approach you use from the point a source file comes out from CVS to the point it get to the production environment. (An example with a single file change would be usefull)


In case you need to change one file for production

1) Get source code from CVS
2) Do the change
3) Test the change
4) Commit updated source code back to CVS
5) Get updated source code from CVS
6) Compile, build and deploy to production

All this could be done with ANT.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Misha Ver wrote:
O. Ziggy wrote:Is there any situation where this wont work?


It would not work for static final, as Sam Mercs wrote above


And I am not kidding - something similar had really happened - Fortunately for us not in production.
(Build wasn't used that time - just the class file was replaced manually -- but final static's are in-lined at compile time -- You can get some more info on my blog -- http://twisters.quiz4j.com/2009/09/puzzle-53-statically-speaking.html!!)

Saravanan Mahalingam
Greenhorn

Joined: May 18, 2006
Posts: 4
By default, Greedy compiler doesn't re-compile the dependent classes. You can turn off this feature by setting ‘false’ to includeDestClasses attribute

Please refer this link for further details
http://ant.apache.org/manual/CoreTasks/javac.html
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok i have changed the above example to change the "name" property to a static final and yes you are right i had to recompile both files for the second file to detect the change.



the output




What exactly is the technical reason for this?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sam Mercs wrote:
Misha Ver wrote:
O. Ziggy wrote:Is there any situation where this wont work?


It would not work for static final, as Sam Mercs wrote above


And I am not kidding - something similar had really happened - Fortunately for us not in production.
(Build wasn't used that time - just the class file was replaced manually -- but final static's are in-lined at compile time -- You can get some more info on my blog -- http://twisters.quiz4j.com/2009/09/puzzle-53-statically-speaking.html!!)



Hi Sam, Please repost the URL as that is pointing to a 404 page.

Thanks
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sam Mercs wrote:
Misha Ver wrote:
O. Ziggy wrote:Is there any situation where this wont work?


It would not work for static final, as Sam Mercs wrote above


And I am not kidding - something similar had really happened - Fortunately for us not in production.
(Build wasn't used that time - just the class file was replaced manually -- but final static's are in-lined at compile time -- You can get some more info on my blog -- http://twisters.quiz4j.com/2009/09/puzzle-53-statically-speaking.html!!)



What is the technical reason for this behaviour?
(That link does not work)
 
 
subject: Deploying servlets as a war file