Build genarated java code to capture project version and git hash

During build , we want to capture the project version and git hash. We know these info can be added to artifacts manifest files. But, we want to capture this through build generated java code.
How can be we achieve this?

Thanks

Probably the easiest thing to do would be to generate a properties file with this information in it (we do something similar here https://github.com/gradle/gradle/blob/master/gradle/buildReceipt.gradle) and put that in your jar as a resource.