【问题标题】:Fetching artifactId from POM file gone wrong从 POM 文件中获取 artifactId 出错了
【发布时间】:2016-01-25 06:50:50
【问题描述】:

为了在我的项目中获取 artifactId,我使用了以下代码。

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();
        }
    }
}

我的 application.properties 包含以下内容:

prop.name=${project.artifactId}

如果一切正常运行,预期的输出是:

config-service //this is my artifactId

但是当我运行上述代码时得到的输出是

${project.artifactId}

我参考了这些链接来获取信息:Retrieve version from maven pom.xml in code, http://www.mkyong.com/java/java-properties-file-examples/

谁能更正我的代码以正确获取 artifactId?

编辑: Pom 文件:

<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>

【问题讨论】:

  • 你能分享你配置资源的部分 pom 吗?你在应用 maven 过滤吗?
  • @A.DiMatteo 我已经更新了主帖中的 POM 文件。
  • 你是如何运行主课的? Maven 构建是否将属性文件中的令牌替换为目标/类文件夹的一部分?
  • @A.DiMatteo 我以错误的方式运行。我遵循了以下答案中提到的内容。

标签: java maven spring-boot pom.xml


【解决方案1】:

我认为您的目录结构或运行代码的方式存在问题,因为配置看起来不错。

看下面的小例子

# 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}

现在编译并执行它

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

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

属性的输出是两种情况

prop.name:config-service
application.name:config-service

【讨论】:

  • 问题在于我的跑步方式。我按照您所说的进行了正确的执行。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 2016-11-30
  • 2019-11-22
  • 1970-01-01
  • 2015-07-14
  • 2018-05-27
  • 1970-01-01
相关资源
最近更新 更多