Howto re-use War resources between Wars with Maven War Plugin

独自空忆成欢 提交于 2019-12-04 04:47:59

问题


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:

  • not to duplicate code
  • both wars use one version of the header code

So the question is:

There is way to avoid copy & pasting both files (header.jsp & controller.java) from old project to the new one?


回答1:


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.



来源:https://stackoverflow.com/questions/14142081/howto-re-use-war-resources-between-wars-with-maven-war-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!