Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think there are different options how you can model your existing mvn profiles with gradle. I'll give you one example here:</p> <p>given you have a property file that looks like that:</p> <pre><code>property1=$prop1 //prop1 and prop2 are placeholder for your environment specific values property2=$prop2 </code></pre> <p>Now you can model your profiles in your build.gradle file:</p> <pre><code>def profileProperties = [ test:[prop1:"testValue1", prop2:"testValue2"], dev:[prop1:"devValue1", prop2:"devValue2"], prod:[prop1:"prodValue1", prop2:"prodValue2"] ] </code></pre> <p>This is just a ordinary nested map defined in groovy.</p> <p>By passing a commandline option 'profile' to your gradle call</p> <pre><code>gradle clean build -Pprofile=dev </code></pre> <p>you can pass to your gradle project what environment you're currently in. In your build script you can differentiate this by adding the following to your build file:</p> <pre><code>def usedProfile = project.getProperty("profile") processResources{ expand(profileProperties[usedProfile]) } </code></pre> <p>This takes your defined profileAttribute and reads the according map of environment properties. this environment properties are passed as a map to the expand filter method, that is part of the gradle API. have a look at <a href="http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:expand(java.util.Map" rel="nofollow">http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:expand(java.util.Map</a>) for details about the expand method.</p> <p>The whole build.gradle file for a simple java project would now look like this:</p> <pre><code>apply plugin:'java' def profileProperties = [ test:[prop1:"testValue1", prop2:"testValue2"], dev:[prop1:"devValue1", prop2:"devValue2"], prod:[prop1:"prodValue1", prop2:"prodValue2"] ] def usedProfile = project.getProperty("profile") processResources{ expand(profileProperties[usedProfile]) } </code></pre> <p>This is just a draft, of course you can use all groovy goodness here to add a bit more logic here, like having a default profile, etc.</p> <p>hope that helps,</p> <p>regards, René </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload