5

Is it possible to select a profile based on what the current git branch is?

For example, if you are in the master branch, it selects the 'production' profile. If it is the develop branch, it selects the 'development' profile.

I found the mavanagaiata plugin which provides the mvngit.branch property but it can not be used in the <profiles> section.

Is something like this possible? Or are there better ways? I want an easy way to automatically select the correct profile depending on the branch being build.

Koraktor
  • 41,357
  • 10
  • 69
  • 99
rve
  • 5,897
  • 3
  • 40
  • 64

2 Answers2

8
  1. Use profile ID the same as your git branch.
  2. Run maven with corresponding profile:

    mvn clean deploy -P $(git rev-parse --abbrev-ref HEAD)

ursa
  • 4,404
  • 1
  • 24
  • 38
2

Make the profile activation by a file. Then create the file in a corresponding branch only, so that when you switch branches the file will appear or disappear, it will affect the profile activation.

However, I feel that it is bad idea to select maven profile based on git branch.

kan
  • 28,279
  • 7
  • 71
  • 101
  • 1
    I think this is the only solution. However, I followed your note and do not select a profile based on a git branch. – rve Sep 17 '12 at 08:54
  • Not quite useful -- when you merge back to master, that file will either go to master silently, or get a merge conflict. – Agoston Horvath Jun 23 '16 at 08:29
  • you could create the file in a git-hook with the branch name as part of the filename, add the file to gitignore, e.g. `.env.currentBranch.exampleBranch`, add ".env*" to .gitignore, and also delete all ".env.currentBranch*" file before creating new ones. The activate the profile using `.env.currentBranch.exampleBranch` – Gerald Mücke Oct 25 '22 at 15:53