0

For fetching the artifactId on my project, I'm using the following code.

public static void main(String[] args) {
    Properties prop = new Properties();
    InputStream in = null;

    try {
        String fileName = "application.properties"; 
        in = ConfigServiceApplication.class.getClassLoader().getResourceAsStream(fileName);
        prop.load(in);
        SpringApplication.run(ConfigServiceApplication.class, args);
        System.out.println(prop.getProperty("prop.name");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My application.properties contains the following:

prop.name=${project.artifactId}

If everything were to run correctly, the expected output is:

config-service //this is my artifactId

but the output that I get when I run the above code is

${project.artifactId}

I have referenced these links for fetching the information: Retrieve version from maven pom.xml in code, http://www.mkyong.com/java/java-properties-file-examples/

Can anyone correct my code to correctly fetch the artifactId?

Edit: Pom file:

<project>
...
     <artifactId>config-service</artifactId>
     ...
     <properties>
         <name>${project.artifactId}</name>
     </properties>
     ...
     <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
     </build>
     ...
</project>
Community
  • 1
  • 1
Sudha
  • 159
  • 3
  • 5
  • 17

1 Answers1

0

I believe there is either a problem in your directory structure or in the way you run your code, as the configuration looks ok.

See below small example

# assumed structure
pom.xml
src/main/java/sub/optimal/mvnproperties/Main.java
src/main/resources/application.properties

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal</groupId>
    <artifactId>config-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

src/main/java/sub/optimal/mvnproperties/Main.java

package sub.optimal.mvnproperties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Main {

    public static void main(String[] args) {
        String fileName = "application.properties";
        ClassLoader classLoader = Main.class.getClassLoader();
        try (InputStream in = classLoader.getResourceAsStream(fileName)) {
            // add check if the stream is open
            Properties prop = new Properties();
            prop.load(in);
            prop.entrySet().stream().forEach((entry) -> {
                System.out.printf("%s:%s%n", entry.getKey(), entry.getValue());
            });
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
    }
}

src/main/resources/application.properties

prop.name=${project.artifactId}
application.name=${pom.name}

Now compile and execute it

mvn clean compile
mvn exec:java -Dexec.mainClass=sub.optimal.mvnproperties.Main

or

mvn clean package
java -cp target/config-service-1.0-SNAPSHOT.jar sub.optimal.mvnproperties.Main

the outpout of the properties is in both cases

prop.name:config-service
application.name:config-service
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • 1
    The problem was way I was running. I followed what you told and its executing correctly. Thank you. – Sudha Jan 25 '16 at 09:15