Maven, Java, and custom files for deployment
i've a Java project managed by Maven2. The scenario i'm trying to solve is the following: in development mode i need to use some configuration files (for example, a hibernate.cfg.xml configured for the dev environment), while in production i need to exclude all the development specific files and configurations, and get instead some other ones for my production environment. 开发者_运维知识库How can i handle this situation?
Thanks
There is no single answer to this question, there are many ways to deal with this kind of situation:
- using profiles and filtering
- using profiles and various filter files
- using different configuration files and picking of one them at build time
- using environment specific files in different directories and building various flavors
Choosing one solution or the other depends on your exact needs.
See also
- A Maven2 multi-environment filter setup
- 5.5. Tips and Tricks
- One artifact with multiple configurations in Maven
- Maven - Building For Different Environments with Maven 2
The best way is to simply use one spring configuration file. Spring is the best choice for java application configuration and dependency injection. Infact, Spring supports hibernate right out of the box so the two are really easy to get working together. Once this is done you can use property value place holders and then configure a Spring 'PropertyPlaceholderConfigurer' to load of different .properties files based on what environment you are running the app on. An example of the usage of 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 wire this all together can be seen here.
I have handled this using Spring's PropertyPlaceholderConfigurer and including property files on the classpath and one on 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 app starts (or tests are run etc.) it will override properties from files baked into the war/ear/whatever.
One simple way to do this is to use different build profiles within Maven, so that you can have different variable values or include a different file based on if you are using the "dev" profile or "production" profile.
The way I deal with this problem is to use Maven WAR overlays. This works well in the case where you are tailoring / configuring a site based on a base Maven project that you don't have commit rights for.
Indeed, you can use a combination of Maven WAR file overlays and clever Spring XML wiring files to solve the more general problem of configuring a site built from multiple webapps. For an example of this approach, take a look at the Danno project I'm currently working on.
精彩评论