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
<[email protected]>:
>
> 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: [email protected]
> For additional commands, e-mail: [email protected]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to