Home > Software engineering >  Is there any way to get maven project version from pom.xml in classpath dependency jar
Is there any way to get maven project version from pom.xml in classpath dependency jar

Time:11-17

We have a health Utility jar, which is added as maven dependency in another project A. We want to get maven version of project A from health Utility jar. Can anybody suggest how to get it?

CodePudding user response:

Have a look at org.springframework.boot.SpringBootVersion, this is one way to retrieve version information for a class/package.

CodePudding user response:

Easiest for this scenario (for few properties):

"Filter" properties into a (spring-used) properties file.

pom.xml:

...
   <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.5.6</version>
     <relativePath /> <!-- lookup parent from repository -->
   </parent>
   ...
   <version>1.2.3.SNAPSHOT</version>
...

This configures maven to filter all .yaml, .yml and .properties files from src/main/resources/ (see here, here and here).

Then in src/main/resources/application.properties, you would:

my.domain.maven.project.version=${project.version} 
# a key of your choice, but the value *has to be* 
# ${project.version} or @project.version@

On a build (involving process-ressources phase (e.g. mvn clean process-classes)), this will(should) be "filtered" into ${project.buildDir}(normally target/classes)/application.properties:

my.domain.maven.project.version=1.2.3.SNAPSHOT

Which is then available like:

@SpringBootApplication // @Configuration/@Component/Bean-like/@...
public class MyApp {

    @Value("${my.domain.maven.project.version}")
    private String mavenProjectVersion;
    // do something with it
}

But of course there are many different solutions/approaches, e.g.:

  • Related