Under a Java EE enviroment, I have a web page built including several JSP files. Lets say one of the included JSP is header.jsp. Can be very similar to the one used by Stackoverflow, showing info about the user, links etc. This JSP is linked to a java controller to fetch user info, and is part of my WAR file including all the web application.
To develop it I used Eclipse and to build and package it I use Maven.
Now I need to build a new webApplication (so a new war) and I want to reuse the header.
What I pretend is:
So the question is:
There is way to avoid copy & pasting both files (header.jsp & controller.java) from old project to the new one?
The Maven WAR plugin, which is the plugin that handles the packaging of a WAR file for you, provides the "Overlay" mechanism to solve exactly this issue. You are already using the WAR plugin simply by having your project be of type "WAR"; maven uses that as a clue as to how to build your project, including use of the WAR plugin for the package phase binding.
The way the Overlay works is that your WAR file declares another WAR project as a dependency, just like you would delcare a dependency on a jar file you need. When the WAR plugin's package mojo fires, it will see this dependency on another WAR and build your new war as a combination of the two. Note, there is no merging of files if the file exists in both WARS; one simply overrides the other. You can configure the plugin to choose which one get's used; by default, it's the current project's file that wins. Also note, you can depend on more than just one war if you like.