jagr
jagr

Reputation: 339

2 wars sharing code

I need to create 2 war applications deployed on tomcat server. One of the applications have the exact same logic and code as the other application but with added changes to the view and controllers.

Then App1 and App2 will have the same code to access data and I don't want to duplicate code. My idea is create 2 WARs and these WAR files should use a library or other project (I don't know) that has access to the database.

Which solution is the best for performance?

Upvotes: 5

Views: 227

Answers (3)

Sebastian van Wickern
Sebastian van Wickern

Reputation: 1749

Option 1

If you are sharing code (and it's a big piece of code, which drives you crazy while uploading the war-files) it may be an option to create a jar containing the code and add the jar file to tomcats library-folder, which is

${CATALINA_BASE}/lib/

Note that this is usually not something you want to do lightly, because that jar file will be available to ALL war-files on the tomcat, creating possible namespace-problems.

Option 2

If sharing the code with all the projects on the application-server is not an option you'll have to add the jar-file to the projects and add it into it's classpath (which happens automatically within eclipse if you add the jar into

${PROJECT_ROOT}/WebContent/WEB-INF/lib
).

Preformance-wise this doesn't really make a difference since tomcat will load the class-files, which are not very big. The instances might be, but the type of deployment doesn't really have an impact upon instances.

Upvotes: 2

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

If you want to use the same classes for both projects just simple create one .jar file which will contain those classes. Then add that .jar into your web projects' classpath and use it in both.

Upvotes: 2

Kent
Kent

Reputation: 195119

You can extract the common part out, and make it as a jar. And then two wars use this jar as library.

If you used maven for building your wars, it would be easier to build a project hierarchy.

something like:

parent
 |_common(jar)
 |_war1
 |_war2 

Upvotes: 1

Related Questions