为了在我的项目中获取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>
答案 0 :(得分:0)
我认为您的目录结构或运行代码的方式存在问题,因为配置看起来不错。
见下面的小例子
# 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 /主/爪哇/分/最佳/ 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 /主/资源/ 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