All compiled codes in .Net are called assemblies (it could be a .dll or .exe file). In order for the assembly to have a version or any additional information, it need to include the following:
Imports System.Reflection
<Assembly: AssemblyTitle("Hello World")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("Programmer's Journal")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("Programmer's Journal")>
<Assembly: AssemblyTrademark("")>
<Assembly: AssemblyVersion("1.0.0")>
<Assembly: AssemblyFileVersion("1.0.0")>
Once you compile the program and you right click on the properties, you will be able to see the version as follows:
You can then access the Assembly Version using the following code:
System.Reflection.Assembly.GetExecutingAssembly.GetName.Version.ToString
and the AssemblyFileVersion using the following code:
System.Diagnostics.FileVersionInfo.GetVersionInfo(system.Reflection.Assembly.GetExecutingAssembly.Location).FileVersion
AssemblyVersion is used for strongnamed assemblies and usually used by applications to check on assembly compatibilities.
If you are using Visual Studio, this information is usually added into a separate file called AssemblyInfo.* (.vb for Visual Basic, .cs for C# projects).
Also by default it will only have AssemblyVersion("1.0.*"). This will cause the version to automatically be incremented whenever the project is compiled. I prefer to change it manually so that I can control and document the changes with the version.
Note that since the version is only available after the assembly is compiled, you won't have it if you create a website in Microsoft Visual Studio 2005 and Visual Studio 2008 and deploy it as source code.
Version in Java (J2SE)
This is done using the manifest file as mentioned in the following sun document:
http://java.sun.com/docs/books/tutorial/deployment/jar/packageman.html
You can then access the information using the following:
java.lang.Package p = this.getClass().getPackage();
System.out.println("Hello Specification Version : "
+ p.getSpecificationVersion());
System.out.println("Hello Implementation Version : "
+ p.getImplementationVersion());
The full description is shown in the following article:
http://www.rgagnon.com/javadetails/java-0388.html
Snippet from above is as follows:
Let's say we have the following class (package is mandatory)
package com.rgagnon;
public class Hello {
public static void main(String[] args) {
new Hello().say("Hello World!");
}
public void say(String s) {
System.out.println(s);
}
}
First create a MANIFEST.MF file :
Manifest-Version: 1.0
Main-Class: com.rgagnon.Hello
Then build the executable Jar and run it.
>jar cvfm hello.jar MANIFEST.MF com\rgagnon\Hello.class
>java -jar hello.jar
Hello World!
Now modify the manifest to include versioning information.
Manifest-Version: 1.0
Main-Class: com.rgagnon.Hello
Specification-Version: 2.1
Implementation-Version: 1.1
Modify the class to display the version.
package com.rgagnon;
public class Hello {
public static void main(String[] args) {
new Hello().say("Hello World!");
}
Hello() {
Package p = this.getClass().getPackage();
System.out.println("Hello Specification Version : "
+ p.getSpecificationVersion());
System.out.println("Hello Implementation Version : "
+ p.getImplementationVersion());
}
public void say(String s) {
System.out.println(s);
}
}
Compile and rebuild the Jar and execute
> jar cvfm hello.jar MANIFEST.MF com\rgagnon\Hello.class
> java -jar hello.jar
Specification Version : 2.1
Implementation Version : 1.1
Hello World!
I have not found a way to display the version in a J2ee application since when we deploy the .war or .ear file, the application servers such as JBoss will explode it into .class file and directory structure.
No comments:
Post a Comment