Vit Bernatik
Vit Bernatik

Reputation: 3802

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;

Upvotes: 18

Views: 7702

Answers (1)

Vit Bernatik
Vit Bernatik

Reputation: 3802

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 :)

Upvotes: 11

Related Questions