如何从maven java项目加载外部属性文件
我有一个maven java项目,在src/main/resources目录中有一个属性文件。我已经打包了jar,但是jar中没有属性文件,因此可以将它部署到具有不同设置的不同环境中,但是单元测试失败了 项目结构如下:如何从maven java项目加载外部属性文件,java,maven,junit,properties,Java,Maven,Junit,Properties,我有一个maven java项目,在src/main/resources目录中有一个属性文件。我已经打包了jar,但是jar中没有属性文件,因此可以将它部署到具有不同设置的不同环境中,但是单元测试失败了 项目结构如下: Properties Application Project |-- pom.xml `-- src |-- main |-- java | `-- java classes |-- resources
Properties Application Project
|-- pom.xml
`-- src
|-- main
|-- java
| `-- java classes
|-- resources
| `-- myProps.properties
`-- target
|--properties-app.jar
myProps.properties
这些值将加载到此文件中
public class MyProperties {
private Properties myProperties;
public MyProperties() {
File file = new File("./myProps.properties");
try {
myProperties = new Properties();
FileInputStream myPropertiesInputStream = new FileInputStream(file);
myProperties.load(myPropertiesInputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public Properties getPropertyValue() {
return myProperties.getProperty("value");
}
}
单元测试如下所示
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class MyPropertiesTest {
private MyProperties myProperties;
@Before
public void setup() {
myProperties = new MyProperties();
}
@Test
public void testMyProperties() {
assertNotNull(myProperties.getPropertyValue());
}
}
我应该使用什么从目标目录加载属性文件,以便从命令行成功构建它?我希望您使用的是maven jar插件。如果是这样,请在jar插件中使用此配置
<configuration>
<archive>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
.
然后,jar将从jar文件的根目录中获取属性文件。用这两个建议以及对pom的一些更改解决了这个问题。 谢谢你们两位 在pom中,资源目录中的属性文件被复制到目标目录,并从pom创建的jar文件中排除
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>properties</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.properties.MyProperties</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
现在所有测试都通过了,jar文件从命令行运行并加载属性文件
我已在此处将生成的项目上载到github:
myProperties.load(getClass().getResourceAsStream(“/myProps.properties”)代码>谢谢。但是我不理解lib/
和MainClass
部分。你能解释一下吗?MainClass是在使用“java-jar myJar.jar”时默认执行的类,请参阅“lib/”命令JRE在lib目录中查找额外的jar文件。这与问题无关,应该删除,抱歉造成任何混乱
myProperties.load(getClass().getResourceAsStream("/myProps.properties"));