1

By default the Maven Tomcat plugin with tomcat7:run reads resources from src/main/webapp.

However when you have custom resources from the Maven resources plugin this needs to be configured to include these. The general advice is to configure warSourceDirectory to point to target/${project.build.finalName}, which is created by the Maven WAR plugin in the package phase. This works, but now files I change in src/main/webapp will not change in the Tomcat instance until mvn package is run. IDEs typically do a mvn compile so resources get copied to target/classes. I tried pointing it to target/classes but it expects a WEB-INF/ folder in there. I also tried using additionalClasspathDirs to point to both target/classes and src/main/webapp which had no effect.

Plugin version is 2.2.

How do I include resources in the Maven Tomcat plugin while still being able to reload reosurces on the fly?

Community
  • 1
  • 1
bcoughlan
  • 25,987
  • 18
  • 90
  • 141
  • what kind of resources are you talking about? in src/main/webapp there are js, css, html, jsp files so if you another folder in src/main/myresouces with those you have two resource folders for js css html and so on. can you specify kind of resources you want to reload, please? – Eddú Meléndez Feb 02 '15 at 03:38
  • Properties files and Spring config files. Maven resources plugin copies either `src/main/config/dev/` or `src/main/config/production/` to the target depending on the profile. I want to reload the js/html files in `src/main/webapp`. If there was a way for the plugin to override the target resources with these files this would solve the problem. – bcoughlan Feb 03 '15 at 01:43
  • I don't think that changing spring config files is that easy. I know that it's possible, but it often fails -- even when using dynamic classloaders like jrebel. Other property files may be problematic too, for the same reason. However, if all you want to do is have the ability to have changes to html, css, and js files, then this is quite possible. If you can clarify your requirements then I may be able to help. – Software Engineer Feb 03 '15 at 09:48
  • The config you described, having a dev and prod folder, isn't very good practice. You should be aiming to create a single binary and have it discover its environment from an external config file or from environment variables. – Software Engineer Feb 03 '15 at 09:50
  • @EngineerDollery I don't see why it's bad practice for a small project that only has one dev and production configuration, as this is the only case where it causes problems. I consider it a defect of the Maven Tomcat plugin if it can not be configured to run in a state that is consistent with the packaged WAR file while reloading resources. However I agree that in this case it is a valid workaround to the issue and will accept Eddu's answer if no better solution exists. – bcoughlan Feb 05 '15 at 16:35

1 Answers1

0

My advice is to use spring profiles in order to load the right beans per environment and load properties from a external file file://opt/myapp/application.properties where application.properties will be different per environment.

Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35
  • This is exactly what I do already. The Tomcat plugin does not use the `target/` folder by default at all. – bcoughlan Feb 03 '15 at 12:04
  • You should keep all properties in one path. If you deploy in dev environment, properties will have another values (different from local) And the same for production. – Eddú Meléndez Feb 03 '15 at 12:11