For Gradle 5.x, 6.x and 7.x
(tested personally on Gradle 7.6 and verified the documentation of version 5)
In more recent versions of Gradle the results described by the OP are achieved automatically by the default building script if you specify the version property in your module build.gradle file (commonly seen right after your plugin block and before any other blocks).
So it would look something like this:
plugins {
// your plugins...
}
// This is the string you want to have.
version = '1.0.0'
dependencies {
// your dependencies
}
version accepts any string, so you can also append pre-release strings like 1.0.0-beta if you need to.
However, one more thing to note is that by default Gradle uses the module name as the base name or your archives and jar files. If your module name matches the name of your library this shouldn't be a problem, but a common scenario it to work with sub-modules where you usually have a lib or core module, and in this case your jar file will end up being named something like lib-1.0.0.jar. To avoid this, you can specify the archivesBaseName property. You can put it right below (or above) your version:
version = '1.0.0'
archivesBaseName = 'abc'
This will correctly generate a abc-1.0.0.jar file.
Note that archivesBaseName uses the plural archives, and it comes from the Project type, and NOT the singular form archive that comes from the jar task.
But, if you need more granularity other than what described above, you will indeed need to use the jar task, similarly to how the OP originally intended to.