1

I'm using maven to build and run my project on Intellij IDEA.

For tracking purposes I would like to be able to get/fetch the build number associated on VCS/Localhistory directly into my java code. Rather than get the number, if I was able to create my own and set the build number on local history it should be ok.

Build number example

Like you see on that picture, I would like to get 6826bed7 or 40cfe86c like this :


public static void main(...) {
    // Stuff like this
    String buildNumber = IntellijInternalApi.getThisBuildNumber(); 
    // this should print 6826bed7 in my example
    System.out.println(builNumber); 
}

Any idea or solution ?

bastien enjalbert
  • 337
  • 1
  • 3
  • 18

1 Answers1

-1

Forget about it, 1) most probably you do not want to have intellij api in your application classpath 2) you dont have git information in runtime, this is the property of your working copy, not binary distribution.

You have to create this information on build stage, and write to properties file, then just simply read the value. You can do this either using git rev-parse HEAD or git decribe using maven exec-plugin, or use maven-git-commit-id-plugin

Actually, your question is duplicate of Injecting current git commit id into Java webapp

  • I'm not trying to get last commit id. Intellij IDEA use his own VCS and build number, and this is that number/identifier that I want to fetch. I'm not interested about commit id since I run several times my application without commiting. So you're not answering the question, and the trick is not what I'm looking for. By the way, I already know that possibility, again that's not what I'm looking for. – bastien enjalbert Nov 06 '19 at 21:08
  • I didn't got clear what you want to get. IDEA does not have own VCS. May be you mean local history? Ids on your screenshot (6226bed7 and 40cfe86c) - are git commit hashes, not a build numbers. Also, I don't understand what issue are you trying to solve, looks like an X-Y problem. Anyway, you can't use IDEA api without making dependency on it in your maven pom. In 99.9% cases it is not what you want, unless you creating your own plugin or IDE – Alexander Bubenchikov Nov 07 '19 at 08:32
  • Yes I mean Local History. And no, these ids are Intellij Built-in id which are created at each execution. I'm not interested about getting commit hashes or whatever related with git. I need Local History build numbers (IDEA generated) in runtime because I'm saving some files during running and I need to refer to which build these files are saved. Using IDEA api was an idea, I didn't say that was the solution of my problem. BUT my question is about getting these ids, which are not related to my git versioning. IDEA is not commiting my project at each run, right ? Own plugin may be a solution. – bastien enjalbert Nov 07 '19 at 10:37