Maven, Java and custom deployment files

I have a Java project managed by Maven2. The scenario I am trying to solve is the following: in development mode I need to use some config files (like hibernate.cfg.xml set up for dev environment), while in production I need to exclude all files and configurations related to development, and instead use some others for my production environment. How can I handle this situation?

thanks

+2


a source to share


5 answers


There is no single answer to this question, there are many ways to deal with this situation:

  • using profiles and filtering
  • using profiles and various filter files
  • using different config files and choosing one of them at build time
  • using environment specific files in different directories and creating different flavors.

The choice of one solution depends on your specific needs.



see also

+1


a source


The best way is to just use one spring config file . spring is the best choice for configuring Java application and dependencies. Infact, spring supports hibernation right out of the box, so it's really easy for the two to work together. Once this is done, you can use the property value holders and then configure spring "PropertyPlaceholderConfigurer" to load different .properties files based on which environment you are using the application in. An example using these placeholders is as follows:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName"><value>${driver}</value></property>
  <property name="url"><value>jdbc:${dbname}</value></property>
</bean>

      



A good example of how to put it all together can be seen here .

+1


a source


I handled this with Spring PropertyPlaceholderConfigurer and included the property files in the pathpath and one in the filesystem:

<context:property-placeholder 
    location="classpath*:META-INF/spring/*.properties,file:myapp*.properties"/>

      

If there is a myapp * .properties file in the current directory when the application starts (or tests run, etc.), it overrides the properties from the files baked in war / ear / whatever.

+1


a source


One easy way to do this is to use different build profiles in Maven so you can have different variable values ​​or include a different file based on if you are using a "dev" profile or a "production" profile.

0


a source


The way to work around this issue is to use Maven WAR overlays. This works well when you are setting up / configuring a site based on a basic Maven project for which you do not have commit rights.

Indeed, you can use a combination of Maven WAR file overlays and Spring XML smart files to solve the more general problem of customizing a site built from multiple webapps. For an example of this approach, take a look at the Danno project I'm currently working on.

0


a source







All Articles