Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Spring

Removing Unused Mappings from web.xml

0.00/5 (No votes)
13 Jan 2017CPOL3 min read 9.3K  
A quick to implement way to remove unused mappings from web.xml

Introduction

If you use code generation to code your Spring MVC application, e.g., with tools like MyEclipse, the web.xml file will contain a lot of mappings that have been auto-generated.

Now, if you modify your database again and again and re-run code generation, it is possible that your web.xml will accumulate mappings that are obsolete. This is because web.xml mappings are automatically added, but not necessarily automatically removed. For example, when you remove a table from the database, the corresponding mappings for CRUD operations in the web.xml will stay there.

Over time, you may find yourself with a humongous web.xml with lots of useless mappings.

Removing them by hand is a hard task if you have lots of them (in my case, with a large DB schema and lots of CRUD operations, I have 2500+ mappings). I've wanted to find an easy way to search which ones are obsolete. Here is my first approach, which for the moment seems to work well. You are welcome to suggest improvements.

Background

The idea behind the trick is that, if a mapping is used, there will be some place in the code other than web.xml where the URL of the mapping will appear (e.g., in a controller). This assumption is valid in most cases; your mileage may vary though.

The following assumes you are familiar with Spring MVC applications, using regular expressions, using a text editor, using grep, using a command prompt, creating and executing a bash script.

Getting the List of Unused Mappings

  • Open your web.xml, copy the server mappings part (exclude the rest) and paste it in a text editor that supports regular expressions.
  • Using regular expressions, remove all the XML code and all the text apart from the mapping URLs. You should now have a list of URLs of all your mappings, one per line.
  • Do a search & replace using regular expressions; replace each line...
    ^(.*)$ 

    ...with the following code:

    ! grep -m 1 -qIR --exclude="checkmappings.sh" --exclude="web.xml" 
    --exclude="*.class"  "$1" . && echo $1

    The ! at the start negates the grep output, so that the echo command is only executed if the pattern is NOT found; -m 1 stops at the first match; -q makes grep quiet; -I ignores binary files; and -R is the recursive search. The script itself and web.xml are excluded, as they would always give a match, rendering the script useless. Also .class files are excluded for speed.

    This will result in a list of grep commands that search for the mapping's URL, and print said URL if it is not found anywhere in your code.

  • Put all these commands in a bash script at the root of your project called checkmappings.sh (Note: The name is important as it is excluded from the grep search).
  • Run the script and go about your day, this will take forever...
  • When the script is done, the output will tell you what URLs were not found. You can then go and remove them from web.xml (either by hand, or if there are too many, you can use regular expressions again).

There! Hope this is useful to somebody. Thanks for any suggestions on how to improve this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)