Getting maven project version and artifact ID from pom while running in Eclipse

后端 未结 2 448
-上瘾入骨i
-上瘾入骨i 2020-12-08 09:43

I was looking up how to get the application name(artifact id) and version from maven pom or manifest when I came across this question Get Maven artifact version at runtime.<

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 10:39

    An easy solution with maven 4 is now to add a VersionUtil static method in your package:

    package my.domain.package;
    public class VersionUtil {
      public static String getApplicationVersion(){
        String version = VersionUtil.class.getPackage().getImplementationVersion();
        return (version == null)? "unable to reach": version;
      }
    }
    

    The thing is you need this ´mave-war-plugin´ in the project's pom, saying you want to add addDefaultImplementationEntries:

    
        ${project.artifactId}
        
         ...
          
                maven-war-plugin
                3.2.2
                
                    false
                    
                        
                            true
                        
                    
                
            
        ...
    

    Then call the VersionUtil.getApplicationVersion() from some place in your code.

提交回复
热议问题