0

i have 2 external properties security.properties and env.properties and i have a key in security.properties as key1 and i want to replace this key1 value based on the env.properties key1

ex:

security.properties
hibernate.connection.url=jdbc:oracle:thin:@localhost:4521/NGI

env.properties
jdbc.url=jdbc:oracle:thin:@localhost:4521/NGI (it defers based on the environment)

i want to replace the hibernate.connection.url to jdbc.url based on the loaded environment.

I tried the following option, but i get a build failure error

<configuration>
           <file>target/${project.artifactId}/security.properties</file>
           <replacements>
               <replacement>
                   <token>${hibernate.connection.url}</token>
                   <value>${jdbc.url}</value>
               </replacement>         
           </replacements>
       </configuration>

please share your suggestions

3
  • You can achieve it using maven profiles and filtering. Check this stackoverflow.com/questions/8325366/… Commented Oct 21, 2013 at 15:26
  • The problem here is, i have a third party jar file which invokes my security.properties file for the key-hibernate.connection.url. I need to change this value depending on the environment. Commented Oct 21, 2013 at 17:38
  • Yes, with that solution the content of the file security.properties will be modified by Maven depending on the current profile. So, it will be transparent for the 3rd-party library Commented Oct 29, 2013 at 12:45

2 Answers 2

1

I believe, what you missed in your pom was loading the env properties. You need to load that file using maven properties plugin.

However, what everyone is trying to tell you, is to keep the values from the env.properties in different profiles in your pom.xml and activate the right profile depending on your targeted environment.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! i tried this as below <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <files> <file>src/main/resources/properties/environment-${buildProfile}.properties</file> </files> But i need to pass the buildProfile here, i am setting the build profile under the profiles section. where as i have given this plugin under pluginmanagement. is there a way to load the profiles first and then load pluginmanagement in pom.xml? I can give the plugin to each profile but i want to keep it in a common place to avoid the same code
By adding the plugin under each profile, it works. Also can you tell me if there is a way to keep this plugin in a common place?
You set the buildProfile variable in your profiles and configure the plugin in the general build section. -- why don't you configure the jdbc.url in the profiles?
thanks Peter! i moved the plugin to each profiles. its working fine.
@ Peter i tried to set the buildProfile variable in profiles and moved the plugin to general section. the issue with this is, the profile section is below the pluginmanagement section in pom.xml. so, its not reading the buildProfile variable in general pluginmanagement section
0

What about keeping all these URLs as property values in pom.xml instead of bunch of ".properties" files? Their values can be easily changed and overrided using profiles and they can be substituted into ".properties" files during filtered resource copying phase of Maven build.

2 Comments

The problem here is, i have a third party jar file which invokes my security.properties file for the key-hibernate.connection.url. I need to change this value depending on the environment.
@Suresh: What everyone is trying to tell you, is to keep the values from the env.properties in your profiles and then your replacement would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.