How to get system user in ant (within eclipse)? - java

How can I access the name of the system user (which eclipse also uses for the javadoc
author tag) in my ant build file?
I'm trying to show some information about the current program version in my java application.
I decided to use jreleaseinfo which passes variables from my ant build script to my java classes (to show them in a window). With svnant I'm even capable of accessing the latest revision number and build date from svn within my build.xml.
Now: The last thing I need is to show who made that build!

This will work anywhere. It uses the java system property user.name.
<property environment="env" />
<echo message="user: ${user.name}" />

user.name can be used:
<echo>User is: ${user.name}</echo>
Results in:
[echo] User is: coobird

Related

The J2SE Platform is not correctly set up.( NetBeans IDE 8.0.2 )

I am getting this error while running my program in NetBeans.
nt -f D:\\PMT_LandingPage jfxsa-run
D:\PMT_LandingPage\nbproject\jfx-impl.xml:3725: The following error occurred while executing this line:
D:\PMT_LandingPage\nbproject\build-impl.xml:87: The J2SE Platform is not correctly set up.
Your active platform is: default_platform, but the corresponding property "platforms.default_platform.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.default_platform.home" in a .properties file)
or ant -Dplatforms.default_platform.home=<path_to_JDK_home> jar (where no properties file is used)
BUILD FAILED (total time: 0 seconds)
I am new with NetBeans and really don't know how to fix it. I googled this issue but could not get any solution.
Go to Tools -> Java Platformsand select the correct path to your JDK.
ant uses a variable named platform.active for identifiying the java home path. Netbeans should initialize that variable with the correct path, and indeed it does depending on your selected java platform.
But, if you select the default platform, netbeans does not assign the correct path to that variable. Instead its value become default_platform.
So, in order to correctly find the java path, you should probably change a line like this:
<webproject1:property
name="platform.home"
value="platforms.${platform.active}.home"/>
into something like this:
<condition
property="platform.home"
value="${java.home}/../"
else="platforms.${platform.active}.home">
<equals arg1="${platform.active}" arg2="default_platform" />
</condition>
This checks the value of platform.active and use it, if it is a path, or use the value of java.home instead.
I tested all of above answers but no one worked for me, the only solution worked for me is replacing below line in file build-impl.xml:
<j2seproject1:property name="platform.home" value="platforms.${platform.active}.home"/>
to:
<j2seproject1:property name="platform.home" value="C:/Program Files/Java/jdk-14.0.2"/>

Your active platform is: default_platform, but the corresponding property "platforms.default_platform.home" is not found

Im currently developing JavaFX applications. I used Netbeans 7.4 before but now I switch to Netbeans 8.0.2. After running my project with the new IDE, an error occured saying:
C:\projects\client\QueueBoard\nbproject\build-impl.xml:87: The J2SE Platform is not correctly set up.
Your active platform is: default_platform, but the corresponding property "platforms.default_platform.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.default_platform.home" in a .properties file)
or ant -Dplatforms.default_platform.home=<path_to_JDK_home> jar (where no properties file is used)
BUILD FAILED (total time: 0 seconds)
How do you resolve this?
The easiest way to resolve this problem is:
open the Project in NetBeans
open Project-Settings for this project
check (and maybe correct/fix) all settings (including Platform-Settings)
click on Save/OK in settings dialog
Clean+Build project
By doing this, project-settings-file will be updated to current settings.
Cheers!
I know this is a very old question, but I encountered this recently. The solution I came up with is to modify the user-editable build.xml file (which is usually one directory up from build-impl.xml) and add the following after the <import> of build-impl.xml:
<property name="platforms.default_platform.home"
location="${java.home}/.."/>
This allows any user/local settings to have precedence, but if not defined, the java used should be the same java that is executing Ant.
This problem still exists in Netbeans 15 and can be solved by updating the nbproject/private/private.properties file and ensure this row exists:
javac.debug=true

Is it possible to to modify variables during the build with Ant?

Hi so recently I have just discovered the amazing power of Ant and I was wondering if Ant has the following capability.
Let me first explain what I am trying to do... I have a product that requires different build specifications for each build and I would hate to do it manually (the site is not live yet but I am expecting a fair amount of orders/day). I can't just build a jar for each combination of settings mostly because each jar will need to have a customer's user id and their license built in. Something like the following is the desired effect:
private final String license = "0123-4567-8910";
private final int userId = 1337;
To clarify: the above values would be set by passing arguments through the command line (hopefully).
You can use Ant's <replace> tag for this.
suppose your log4j properties contains placeholder as
log4j.properties
log4j.appender.R.File=LOGS_DIR_PATH/JBulletinBoard.log
Then you can do like
<replace file="log4j.properties" token="LOGS_DIR_PATH" value="D:/logs"/>
I am not exactly sure what you are trying to do and I will avoid the debate about Maven vs Ant as life is to short..
Its very possible to externalize a properties file and have a common ant file.
Tell ant about your property file
<property file="licence.properties" />
and you can refer to your properties in that file like
<echo message="Registered licence = ${build.licence}" />

How to insert version numbers in our java jars, that a user can access?

We have a library that gets released with a different version number every couple of weeks. The problem is that in order to store the version number in our jars we have a version.txt file that just contains the version number and then gets included into the build. This seems like the wrong way to do this but I can't come up with a better solution. What is a better way to store the version number in our jar's so that if a user calls me up I can easily find out the version of our product they are using?
Firstly -- make sure your program or tool can some SHOW the version number. But where does it come from? We include it in the build.
Just make sure it's visible someplace when they run it! If there's nothing runnable, add a Main, and make it the Main-Class, that just prints the version. Then you can say, Please type java -jar YourLibrary.jar, and it just runs main and prints your version.
Here's the beginnings of the code to read resources out of your jar, from inside the jar, if the resource (such as Version.txt) is next to klazz:
ClassLoader loader = klazz.getClassLoader();
InputStream in = loader.getResourceAsStream (name);
I like to make it automatic in every build, so I don't forget to bump it. Rather than a text file, I use .properties... but you could do the same thing in Version.txt.
(Actually, at the moment, we include just the build-time. But the idea is the same.)
I do it like so -- I have a Version.properties file, with:
buildHost = #HOSTNAME#
buildTime = #BUILDTIME#
buildUser = #USERNAME#
And as part of the ANT script, we do:
<tstamp>
<format property="BUILDTIME" pattern="yyyy.MM.dd.HH:mm:ss z" locale="en,UK" />
</tstamp>
<exec executable="hostname" outputproperty="HOSTNAME">
<!-- note, this is unixey, of course -->
<arg value="-s" />
</exec>
<property environment="env"/>
<property name="USERNAME" value="${env.USER}"/>
<property name="build.info" value="path/to/Version.properties" />
<copy file="${build.info}" tofile="${obj.dir}/${build.info}" overwrite="true">
<filterchain>
<replacetokens>
<token key="BUILDTIME" value="${BUILDTIME}"/>
<token key="HOSTNAME" value="${HOSTNAME}"/>
<token key="USERNAME" value="${USER}"/>
</replacetokens>
</filterchain>
</copy>
Note -- the above is a bit platform specific, but you get the idea.
And how you read .properties files, it's another little pile of code but easy enough.
Do you mean manual access for users, or also access from Java applications? You can create a MANIFEST.MF, which contains the version number. I think every major buildtool has some facilities to do that automatically during the build process.
Consider writing yourself a small class that reads your txt file. (Better yet, switch to a properties file as David suggests.)
Give it a main method that prints that version on the console.
Create a shell script or batch file that puts the .jar on the classpath and calls your class.
Then, during your build process, have your build system insert the current build number/version/timestamp into that file. For ant, see Davids example, for Maven I recommend the Maven Build Number plugin.
When you need to see the version of a .jar file, run your script.
I'm with Reupke on this one.
Store the number however you like, in a properties file, or a txt file, plain text, encrypted, in an embedded DB.. doesn't matter how you store it as long as you have it.
Then, I would write class with a main method that reads the value in an prints it out nicely to the screen... That way if the user wants to know their version number then can simply run the command and get this version.

Appending software version to a JAR filename

I would like to append the output JAR filename of a Netbeans project with some version number: something like 1.0, 2.0b or even a Subversion revision number. I can't seem to find anything on this, though. I am also not sure if this would the responsibility of the build system (Ant) or if the IDE (Netbeans) can delegate the process. Is there a centralised, clean way of doing this?
IMO, this is the responsibility of the build system, not of the IDE. Let me say it in other way: don't rely on your IDE to build your project, use a build tool. Using an IDE is fine during development but being IDE dependent to build a project is not a good thing (what if you change your IDE tomorrow, what if you want to build your project on another machine/OS without that IDE, what if you want to build your project on a headless machine, what if you want to automate your build, what if someone wants to build that project and doesn't have that IDE, etc, etc). Really, this is what build systems are for.
Now, regarding your initial request, there are plenty ways to add a version number. One of them is to use the Ant's BuildNumber task:
This is a basic task that can be used to track build numbers.
It will first attempt to read a build number from a file (by default, build.number in the current directory), then set the property build.number to the value that was read in (or to 0, if no such value). It will then increment the number by one and write it back out to the file. (See the PropertyFile task if you need finer control over things such as the property name or the number format.)
Use it for example like this:
<target name="jar" depends="compile">
<property name="version.num" value="1.00"/>
<buildnumber file="build.num"/>
<jar destfile="foo-${version.num}-b${build.number}.jar"
basedir="."
includes="**/*.class"
/>
</target>
Or you could indeed add subversion revision number. An easy way to do this seems to install the SVNAnt task and use the status task:
<target name="revisionnumber">
<!-- get the svn revision number -->
<svn>
<status path="application.cfm" revisionProperty="svn.revision" />
</svn>
<echo>Sandbox Revision: ${svn.revision}</echo>
</target>
Finally, another option would be to use Maven instead of Ant which has a built-in version management feature as pointed out by cetnar.
I'm not sure if it's the best way, but we put it in MANIFEST.MF file like this:
Implementation-Version: 2.0b
We can get this value programmatically like this:
String version_num = this.getClass().getPackage().getImplementationVersion();
If you feel like using a tool to handle your builds then there are lots about, such as CruiseControl, which is ANT based and has pretty deep integration with your source code control.
I use it to automatically increment a build number and use that as the last digit in my version number for the jar, e.g. 1.4.168, where 168 is the build number. I am just about to get it to put a label into CVS just before the fetch with the build number so I know exactly what code is in the jar.
Well is done default by Maven. Even if you want name your jar file with more detailed information you can use build number plugin.
EDIT
At begining I misunderstood your question so following part relates to adding this information inside jar files.
You can do it yourself by creating manifest file. In Maven it you can tune proces of creating manifest file by additional configuration. I suppouse (I'm sure) that in Ant should be similar functionality.
you can use maven for vesion and read it from pom.
read this article:
Embedding the maven version number
at
http://happygiraffe.net/blog/2008/10/01/embedding-the-maven-version-number/

Categories