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}" />
Related
Teamcity Build ID (which is different from BUILD_NUMBER) is used in various URLs. I want to send an email having path of a build's artifacts/ overview etc.
In Java, we can get currently running teamcity build number as follows:
String tc_BuildNumber = System.getenv("BUILD_NUMBER");
This is because TC provides an environment variable namely BUILD_NUMBER.
But unfortunately, there is no environment variable corresponding to BUILD_ID.
TeamCity does provide Configuration parameters (like teamcity.build.id) and System property (like system.teamcity.auth.userId) but I don't know how to access these using Java. I want to read the value of teamCity.build.id jusy like we can read environment variables names mentioned in How to fetch the Value of Teamcity Configuration in java?
Are you executing the java code using a build runner?
If so, then you should be able to pass %system.teamcity.build.id% to the runner, and make it available to your code.
i.e. If you're using the command line runner
java -Dbuild_id=%system.teamcity.build.id%
which you can then access as system arguments
Or if you're using gradle, you can do something like
if (project.hasProperty("teamcity")) {
version = project.teamcity["teamcity.build.id"]
}
and pass 'version' to the java command line.
In maven, you can just access it using:
${teamcity.build.id}
in your pom.xml
(I could do with a little more info about how you're running java to answer this specifically)
I've noticed that lots of people want to know the answer to this question.
Fortunately with the help of comment from #Jayan I was able to do solve my exact problem which was how to get URL for build artifacts.
As mentioned in link https://confluence.jetbrains.com/display/TCD10/Patterns+For+Accessing+Build+Artifacts, by default, TeamCity uses Internal Build ID for the path that can be used to access build artifacts:
/repository/download/BUILD_TYPE_EXT_ID/BUILD_ID:id/ARTIFACT_PATH
Accessing build Id could be difficult in the runtime(That is the reason of this question), but we can also use Build Number to access artifacts
/repository/download/BUILD_TYPE_EXT_ID/BUILD_NUMBER/ARTIFACT_PATH
And as shown in my question build number can be accessed as
String BUILD_NUMBER= System.getenv("BUILD_NUMBER");
and
String BUILD_TYPE_EXT_ID = System.getenv("TEAMCITY_BUILDCONF_NAME");
Yes, but you can create env var with value "%system.teamcity.buildType.id%" and read it in build. After that you can do an api request like:
$APIURL = "${API_BaseUrl}/httpAuth/app/rest/builds/?locator=buildType:${API_BuildType},state:running,count:1"
$APIXML = (Invoke-RestMethod -Headers $API_CredentialsHeader -Credential $API_Credentials -Uri $APIURL -Method GET -ContentType "application/xml" -TimeoutSec 20)
# Here you build id.
$APIXML.builds.build.id
This is PS example. But idea the same. In Java that might be more easy.
A link to a TeamCity build can use build number instead of buildID. But, it requires buildTypeId as well (can be seen in build configuration page URL).
A sample of such link is:
https://buildserver/viewLog.html?buildTypeId=Project_Trunk&buildNumber=46523
Hope this helps someone.
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.
I'm trying to learn some ant for a Struts 1.x project that I was thrown onto. Mainly I'm trying to find a good referent for the inherent variables/properties of ant...beginners tutorial. Any GOOD reference really.
A couple lines of the ant file that I've been trying to figure out just for example...
<available file=${deploy.ant.docbase.dir}/WEB-INF/sun-web.xml" property="sun.web.present"/>
and
<replace file="${temp.sun.web}">
<replacetoken><![CDATA[<!DOCTYPE]]></replacetoken>
<replacevalue<![CDATA[<!-- <!DOCTYPE]]></replacevalue> //in ant is <!-- the comment out flag?
</replace>
I did do some searching and only could find ant build examples without explanation, but if it is covered and I just didn't find it a link will suffice. No reason to make someone reexplain it....I just couldn't find it.
Your first code block refers to the "available" ant task. It sets the property sun.web.present if the given file exists.
In your second code block, "<!--" starts an XML comment ("-->" closes one). This is true for all XML, not just ant build.xml files. In this case it is using the "replace" ant task to replace "<!DOCTYPE" with "<!-- <!DOCTYPE" within the file named by temp.sun.web.
In general an ant build file has targets like "build" or "clean". These depend on each other so that "test" runs "build" first. The targets are implemented by "tasks", where each XML tag refers to a task. You can read their manual and refer to the per-task docs for how each task works.
The Ant Manual is your friend. There's a link Ant tasks on the left side of the page. Click on that link, and then the List of Tasks link. That will list all of the Ant tasks on the left and their explanation on the right. There, you'll see the available task and the replace task.
Unfortunately, the Ant manual uses Frames (bad Ant Manual! Bad Ant Manual!), so I can't supply a link that will list both the
(Shameless bid for reputation)
The example doesn't use a built-in property. Most Ant targets won't, because after properties are first set they are immutable. Instead, Ant scripts usually define their own properties. The Ant manual lists the properties that Ant predefines.
If you want to get into the guts of Ant, I recommend the Manning "Ant in Action" book.
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/
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