Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven copying applicationContext.xml from src/main/resources to target/myproject/WEB-INF

At the moment, the default I think, it copies to

target/myproject/WEB-INF/classes

so when deploying it does not pick up the context.

Also, i want to reference a server specific config file database.properties, I want to put it in tomcat/conf and then reference it in applicationContext.xml, how can I do this ?

Also(2), I am under the impression that this is a fairly standard and decent way to set things up - please correct me if I am wrong.

edit for the server specific config file I user this

<context:property-placeholder 
      location="file:${catalina.home}/conf/database.properties" 
      ignore-unresolvable="true"
 />
like image 696
NimChimpsky Avatar asked Feb 22 '23 09:02

NimChimpsky


2 Answers

If you need to keep applicationContext.xml as a classpath resource, you can configure ContextLoaderListener to pick it from the classpath by adding the following lines to web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

It's much easier than configuring Maven to copy it to WEB-INF.

Regarding the second question, you can configure PropertyPlaceholderConfigurer or <context:property-placeholder> to load .properties file from a file system.

like image 138
axtavt Avatar answered Feb 26 '23 22:02

axtavt


For your title question: Often in a .war maven module, you'll put web related resources under src/main/webapp instead of src/main/resources. Then the maven plugin will pick them up automatically because it matches convention. So, move your applicationContext.xml to src/main/webapp/WEB-INF

Another option is to configure the webResources as described in the documentation

For the second question you can look at a PropertyPlaceHolderConfigurer. You'll just have to get the path correct.

like image 44
digitaljoel Avatar answered Feb 26 '23 22:02

digitaljoel