I am creating an archetype and want to create an accessible project property that contains things like the current date and all the lower case of the artifact identifier. I found an https://stackoverflow.com/a/3126169/ which shows how this should usually be possible.
I tried adding this to the archetype-metadata.xml file like this:
... <requiredProperty key="artifactIdLower"> <defaultValue>${artifactId.toLowerCase()}</defaultValue> </requiredProperty> <requiredProperty key="ldt"> <defaultValue>${package.getClass().forName("java.time.LocalDateTime").getMethod("now").invoke(null)}</defaultValue> </requiredProperty> <requiredProperty key="dtf"> <defaultValue>${package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke(null, "dd MMM yyyy")}</defaultValue> </requiredProperty> <requiredProperty key="date"> <defaultValue>${ldt.format($dtf)}</defaultValue> </requiredProperty> ...
The artifactIdLower property works like a charm, but ldt , dtf and date do not seem to work, which gives an error:
Null reference [template 'dtf', line 1, column 1] : ${package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke(null, "dd MMM yyyy")} cannot be resolved.
After that, I tried to see where in the chain this null link is. I managed to install pkgClass on $package.getClass() (or on $package.Class ),
but after that I was not able to set $strClass to $package.getClass().forName("java.lang.String") or $pkgClass.forName("java.lang.String") (funnily enough, both pkgClass and strClass must be class objects).
This made me wonder if there is a restriction on the use of reflection within the archetype-metadata.xml.
My question is: how can I set dynamically generated property values (e.g. above) that can be used in a project?
I do not want to define these properties in every file that I create, because there may be more properties that I want to add later.
Edit : Instead, I tried to create a generalproperties.vm file containing the #set directives. This file will then be loaded by each file on the first line using #parse ("generalproperties.vm"). While I received a file that would be parsed from the pom.xml file, it did not behave the way I wanted.
Next entry
test $null #set( $ldtClass = $package.getClass().forName("java.time.LocalDateTime") ) $ldtClass.Name $ldtClass.getMethod("now") #set( $ldtNowMethod = $ldtClass.getMethod("now") ) $ldtNowMethod.Name #set( $clsLoader = $package.getClass().getClassLoader() ) $clsLoader #set( $ldtClass2 = $clsLoader.loadClass("java.time.LocalDateTime") ) $ldtClass2.Name $ldtClass2.getMethod("now") #set( $ldtNowMethod2 = $ldtClass2.getMethod("now") ) $ldtNowMethod2.Name #set( $ldt = $ldtNowMethod2.invoke($null) ) $ldt #set( $dtf = $package.getClass().forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.getClass().forName("java.lang.String")).invoke($null, "yyyy/MM/dd HH:mm:ss") ) $dtf
Created the following output:
test $null java.time.LocalDateTime $ldtClass.getMethod("now") $ldtNowMethod.Name $clsLoader $ldtClass2.Name $ldtClass2.getMethod("now") $ldtNowMethod2.Name $ldt $dtf
The first 3 exits are expected, but after that I do not get the results that I want. If someone can solve any of the above problems (with a metadata file or with a generalproperties file), he will be highly appreciated.