Re: Keeping java's version in sync with project's version
BTW you can also write the version to the manifest and then use class.getPackage().getImplementationVersion() like I did here: https://github.com/ecki/et-otp/blob/663228f2b6ad7d3f20aad04ca5f675b49662a78c/src/main/java/net/eckenfels/etotp/GUI.java#L64 This only requires addDefaultImplementationEntries in the maven-jar-plugin, which is a good idea anyway. Having said that if you prefer a properties file use the one from META-INF/maven/pom.properties. The most direct way is probably setting up resource filtering for a Java file where you add ${project.version} as a string constant. (But that require quite some plugin config) Gruß Bernd - To unsubscribe, e-mail: users-unsubscr...@maven.apache.org For additional commands, e-mail: users-h...@maven.apache.org
Re: Keeping java's version in sync with project's version
Hi Joseph! Not really sure what you mean by "keep java's version in sync". Since you said "-version option on my project", I am guessing you have a CLI app and you want to report the version which you specify in your pom.xml file. Now to do that and have it working with your project, here are the steps I usually take. This is just an extended version of what Alex already wrote. If you have a multi-module project, and you want to access your project version in many other modules, just create a dedicated module. You can also apply the steps to a single-module project. This module should have a class in its own package (Maybe Version.java). It should load a file "version.properties" from the src/main/resources folder. Now the trick: put it into the same directory structure as your Version.java file's package. For the file to get populated, add this line: my.project.version=${project.version} Now, enable resource filtering for your project [1]. When building the project, you should see the filtered file: ${module}/target/classes/path/to/package/version.properties (next to the compiled Version.class). In your Version.java, load the file using the standard Properties mechanism and expose the version via a simple getter. This way you can use the version in other modules, too. If you want to add other properties, you can add plugins like the build-number-maven-plugin to your build. This way you do not need to rely on parsing xml or property files which may be out of scope for your classes. HTH, - Ben [1] - https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html Am Do., 9. Nov. 2023 um 01:18 Uhr schrieb Joseph Kessselman : > > For obvious reasons, if would be nice if the -version option on my > project automatically reported the version I'd set in the pom.xml rather > than my trying to synchronize the two manually. > > The best approaches I've found so far are in > https://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime > ... but there's a note that at least one of them doesn't work in all > classloaders, or in openjdk. > > I'm willing to have the pom write this into a file that the Java code > would query, if that's what it takes to have a portable solution, but > I'm not sure what the best way to express that would be. > > > There must be an established Best Practices solution for this, right? > > - > To unsubscribe, e-mail: users-unsubscr...@maven.apache.org > For additional commands, e-mail: users-h...@maven.apache.org > - To unsubscribe, e-mail: users-unsubscr...@maven.apache.org For additional commands, e-mail: users-h...@maven.apache.org
Re: Keeping java's version in sync with project's version
I'll just mention that I've used the file referenced in the StackOverflow question, META-INF/maven/${groupId}/${artifactId}/pom.properties, before to do something much like you describe, print my application version to the log at startup. It did not need any special configuration to appear, and only took a handful of lines to getResourceAsStream(), load into a Properties object, and ask pomProperties.getProperty("version","development"). 'development' is a safety default here for when the project is running in a context before being packaged, like testing. Now, how does pom.properties get populated? Will it always be populated? Will this still work in a Java 9+/JPMS/Maven 4+ world? Unspecified, exercise for the reader.
Re: Keeping java's version in sync with project's version
Joseph, you probably want to look into resource filtering and put a corresponding placeholder into a properties file and read it as a classpath resource from your artifact. https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html Cheers -- Alexander Kriegisch https://scrum-master.de Joseph Kessselman schrieb am 09.11.2023 07:17 (GMT +07:00): > For obvious reasons, if would be nice if the -version option on my > project automatically reported the version I'd set in the pom.xml rather > than my trying to synchronize the two manually. > > The best approaches I've found so far are in > https://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime > ... but there's a note that at least one of them doesn't work in all > classloaders, or in openjdk. > > I'm willing to have the pom write this into a file that the Java code > would query, if that's what it takes to have a portable solution, but > I'm not sure what the best way to express that would be. > > > There must be an established Best Practices solution for this, right? > > - > To unsubscribe, e-mail: users-unsubscr...@maven.apache.org > For additional commands, e-mail: users-h...@maven.apache.org > > - To unsubscribe, e-mail: users-unsubscr...@maven.apache.org For additional commands, e-mail: users-h...@maven.apache.org
Keeping java's version in sync with project's version
For obvious reasons, if would be nice if the -version option on my project automatically reported the version I'd set in the pom.xml rather than my trying to synchronize the two manually. The best approaches I've found so far are in https://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime ... but there's a note that at least one of them doesn't work in all classloaders, or in openjdk. I'm willing to have the pom write this into a file that the Java code would query, if that's what it takes to have a portable solution, but I'm not sure what the best way to express that would be. There must be an established Best Practices solution for this, right? - To unsubscribe, e-mail: users-unsubscr...@maven.apache.org For additional commands, e-mail: users-h...@maven.apache.org