41

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.

The above works for me when I package the project but I can't seem to get anything to work when I try to run the program using eclipse. I tried using the .properties method when building since I assumed that is not package dependent but I am still not getting a result. If anyone has an idea or solution to this problem it would be greatly appreciated.

My last attempt is below. This uses the manifest when packaged(which works) and trying to get the .properties file when running in eclipse.

String appVersion = getClass().getPackage().getImplementationVersion();
    if(appVersion == null || "".equals(appVersion)) {
        appVersion = Glob.getString(appVersion);
        if(appVersion == null || "".equals(appVersion)) {
            System.exit(0);
        }
    }
Community
  • 1
  • 1
swhite
  • 471
  • 1
  • 6
  • 11
  • Accessing the `pom.xml` as ... well ... `XML` is not an option? What is _"the program"_'s intention? Is _the program_ implemented as Maven plugin an option? – Gerold Broser Oct 24 '14 at 21:17
  • so my problem was that my property variable was the same name as my local variable and I forgot to enclose the property variable call in quotes. – swhite Oct 27 '14 at 13:51

2 Answers2

103

Create a property file

src/main/resources/project.properties

with the below content

version=${project.version}
artifactId=${project.artifactId}

Now turn on maven resource filtering

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

so that this file is processed into

target/classes/project.properties

with some content similar to this

version=1.5
artifactId=my-artifact

Now you can read this property file to get what you want and this should work every time.

final Properties properties = new Properties();
properties.load(this.getClassLoader().getResourceAsStream("project.properties"));
System.out.println(properties.getProperty("version"));
System.out.println(properties.getProperty("artifactId"));
Tombart
  • 30,520
  • 16
  • 123
  • 136
coderplus
  • 5,793
  • 5
  • 34
  • 54
  • 3
    I did follow your steps but it doesn't work. Could you please have a look at my question http://stackoverflow.com/questions/37490162/get-maven-properties-at-build-time#37490162 – Marc May 27 '16 at 19:50
  • 21
    I needed to use `properties.load(this.getClass(). getClassLoader().getResourceAsStream("project.properties"));` in order for it to work – drtf May 30 '16 at 15:54
  • 1
    Hi! It worked just fine, but, in the resources dir I have others ".properties" files and those are now having encoding problem. Do you know if there is a way to set encoding on the tag in maven? – onluiz Jan 10 '17 at 14:44
  • Hi, i am using your code, but i don't think we need to add something to pom.xml. I only using Properties Class and it is worked just fine, without setting anything to my pom.xml – David Vincent Jul 23 '18 at 09:17
  • 2
    If you are trying it from the public static void main(), "this" wont be available as its a static method. So you need to use it with the class name containing the public static void main() , like this: `properties.load(
    .class.getResourceAsStream("project.properties"));`
    – Dilip Muthukurussimana Jul 14 '19 at 16:28
  • For doing this in a static method, @DilipMuthukurussimana 's didn't work for me. You need .class.getClassLoader().getResourceAsStream("project.properties") – Joey Gough Jan 17 '20 at 09:52
  • 2
    For spring boot applications, consider that default placeholders have been re-configured: use @ - so @project.version@ instead of ${project.version}. See https://stackoverflow.com/a/37491454/3957413 – SaschaH Mar 01 '21 at 12:24
2

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:

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
     ...
      <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    ...

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

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • 2
    Unfortunatelly this solution doesn't work when code is running from classes(ie from tests in Intellij) : this is one limitation of this approach. – mlapeyre Jan 22 '21 at 15:14