Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA - can I have automatically incremented build (version) number?

Can I set somehow IntelliJ building process to pre-process Java source codes and give me ever incrementing build number? Something like:

int myBuildNumber = INTELLI_J_IDEA_MAGIC_WHICH_WILL_INCREMENT_EVERY_BUILD;
like image 258
Vit Bernatik Avatar asked Mar 25 '15 20:03

Vit Bernatik


People also ask

How do I know what version of IntelliJ I have?

In the top bar where File, Edit, View... Are written, go to the Help section. Click on Help section and in the drop-down click on About. That will show you the information regarding the Intellij Version you are using.


1 Answers

Ok so with hint from AtomHeartFather I got it.

First we need to write an ant xml file. This file will create file where build number will be stored and incremented and then it will look through your source code file ${src}/com/yourPath/Main.java for variable public static final String BUILD_NUMBER = ".*"; and replace it with current build number

The xml file would look like this:

<project name="MyProject" default="init" basedir=".">
    <description>
        simple example increment build variable
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="../src"/>

  <target name="init">
    <echo file="myAntOut.txt">My first ant ${src} ${line.separator}</echo>

    <buildnumber/>
    <replaceregexp file="${src}/com/yourPath/Main.java"
               match="public\s+static\s+final\s+String\s+BUILD_NUMBER\s+=\s+&quot;.*&quot;;"
               replace="public static final String BUILD_NUMBER = &quot;${build.number}&quot;;"
               byline="true"
    />

  </target>
</project>

Then in your intelliJ (I'm using 14.0.3) you click on View->Tool Windows->Ant Build. Then + and browse to your xml file (note that current path used by your xml will be the path to that xml file itself - not inteliJ project - thus you may want to correct the part location="../src" depending on where you store your xml). Than you shall see our target init you can select it and click play button. If it works you shall see BUILD_NUMBER incremented in you source code file Main.java. Now the important trick how to make it automatically: Just right click on init and select Execute on->Before Compilation. Done :)

like image 81
Vit Bernatik Avatar answered Oct 09 '22 11:10

Vit Bernatik