Discussion:
Auto Increment Build/Version Number
rwmorey
2008-10-29 20:26:34 UTC
Permalink
Hi --



I am new to Java/NetBeans programming and I am wondering if there is a way to access a build number for my project from within the project and to also have this number auto increment each time I compile my project.



If so, can someone provide me guidance with how to access the build number as well as setting it to auto-increment?



Thanks



Rich
Andreas Hesse
2008-10-30 15:49:22 UTC
Permalink
Hi Rich,

try this:

1) Add the following code to yout build.xml


<target name="-pre-jar" >
<propertyfile file="${src.dir}\version.properties">
<entry key="BUILD" value="1" type="int" operation="+"/>
</propertyfile>
</target>

2) create a file named 'version.properties' in your sources directory
and put a 'BUILD=0' into it (without quotes )

Now each successfully compilation of you app will increase the
buildnumer in the property-file

To acces the build number from witihin you app add something like this
to your app (e.g. in Class Main):


final static ResourceBundle rb =
ResourceBundle.getBundle("version.properties");

public static final String getRbTok(String propToken) {
String msg = "";
try {
msg = rb.getString(propToken);
} catch (MissingResourceException e) {
System.err.println("Token ".concat(propToken).concat(" not
in Propertyfile!"));
}
return msg;
}

Now you can acces your build-Number quit simple:

System.out.println("This is build %s", getRbTok("BUILD"));


Good Luck,
Andreas
Post by rwmorey
Hi --
I am new to Java/NetBeans programming and I am wondering if there is a way to access a build number for my project from within the project and to also have this number auto increment each time I compile my project.
If so, can someone provide me guidance with how to access the build number as well as setting it to auto-increment?
Thanks
Rich
Michael Saunders
2008-10-30 16:27:56 UTC
Permalink
If you are using version control a better approach would be to run the
svnversion tool on the top-level of your source tree and parse the first
number returned (it may return a single number or a number followed by a
colon and another number).

Michael

-----Original Message-----
From: Andreas Hesse [mailto:***@bvu.de]
Sent: Thursday, October 30, 2008 8:49 AM
To: ***@netbeans.org
Subject: Re: [nbusers] Auto Increment Build/Version Number

Hi Rich,

try this:

1) Add the following code to yout build.xml


<target name="-pre-jar" >
<propertyfile file="${src.dir}\version.properties">
<entry key="BUILD" value="1" type="int" operation="+"/>
</propertyfile>
</target>

2) create a file named 'version.properties' in your sources directory
and put a 'BUILD=0' into it (without quotes )

Now each successfully compilation of you app will increase the
buildnumer in the property-file

To acces the build number from witihin you app add something like this
to your app (e.g. in Class Main):


final static ResourceBundle rb =
ResourceBundle.getBundle("version.properties");

public static final String getRbTok(String propToken) {
String msg = "";
try {
msg = rb.getString(propToken);
} catch (MissingResourceException e) {
System.err.println("Token ".concat(propToken).concat(" not
in Propertyfile!"));
}
return msg;
}

Now you can acces your build-Number quit simple:

System.out.println("This is build %s", getRbTok("BUILD"));


Good Luck,
Andreas
Post by rwmorey
Hi --
I am new to Java/NetBeans programming and I am wondering if there is a
way to access a build number for my project from within the project and
to also have this number auto increment each time I compile my project.
Post by rwmorey
If so, can someone provide me guidance with how to access the build
number as well as setting it to auto-increment?
Post by rwmorey
Thanks
Rich
shaulgo
2008-10-30 18:16:52 UTC
Permalink
I'm working with CVS. Can I do what you're saying from within the code. I
haven't tried the first option yet. But wont it be a problem with the CVS.


If you are using version control a better approach would be to run the
svnversion tool on the top-level of your source tree and parse the first
number returned (it may return a single number or a number followed by a
colon and another number).
--
View this message in context: http://www.nabble.com/Auto-Increment-Build-Version-Number-tp20244335p20252544.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
rwmorey
2008-10-30 19:35:11 UTC
Permalink
Hi --



Thank you for this info. I added the lines to my BUILD.XML file but this line:



final static ResourceBundle rb =

ResourceBundle.getBundle("version.properties");



is throwing an exception. It also does not appear that the BUILD= value is being incremented. I have put the version.properties file in the \projectname\src\ directory.



Any ideas? Is the file not in the correct location? Can I 'hard' code the directory just to make sure the incrementing part is working?



Thanks



Rich
rwmorey
2008-10-30 20:17:03 UTC
Permalink
Hi --



Ok, I changed the target name="" to "-pre-compile" and copied the file into \src\java\ and now the incrementing is working.



Still working on getting the value.



Rich
rwmorey
2008-10-30 20:46:51 UTC
Permalink
Hi --



Still can't get this to work. The error is



Can't find bundle for base name version.properties



I'm guessing I have to do something to put this value inside the Java and thats why it was tagged as -pre-jar ?



Thanks



Rich
Marián Petráš
2008-10-30 22:10:46 UTC
Permalink
Post by rwmorey
Hi --
Still can't get this to work. The error is
Can't find bundle for base name version.properties
The base name should be without the extension ".properties", i.e. just
"version".

Marián
Andreas Hesse
2008-10-31 08:25:40 UTC
Permalink
Hi,
If you change the ant-target from "-pre-jar "to "-pre-compile" you
increase the build number each time you start a compile - althoug the
compilation fails. When using -pre-jar it should only increase on
successfull compilation.

Can't find bundle: As Marián said, remove '.properties' extension in getBundle()-call. (Sorry 4 that)
Post by rwmorey
Hi --
Ok, I changed the target name="" to "-pre-compile" and copied the file into \src\java\ and now the incrementing is working.
Still working on getting the value.
Rich
rwmorey
2008-10-31 02:19:20 UTC
Permalink
Thanks! I made that change and it worked.

Continue reading on narkive:
Loading...