miyachi-yの日記: Maven2 and Properties
Maven2でjava.util.Propertiesを利用する方法。(Maven Getting Started Guideに書いてあるけど...)
- Projectの構造
example01
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- example01
| | |-- App.java
| `-- resources
| `-- application.properties
`-- test
|-- java
| `-- example01
| |-- AppTest.java
| `-- PropertiesTest.java
`-- resources
|-- commons-logging.properties
|-- simplelog.properties
`-- test.properties - test.properties
foo=test
- HelloTest.java
package example01;
import java.io.*;
import java.util.*;
import junit.framework.*;
import org.apache.commons.logging.*;
public class PropertiesTest extends TestCase
{
private static final Log log = LogFactory.getLog(PropertiesTest.class);
public void testProperties()
{
InputStream is = null;
try
{
is = this.getClass().getResourceAsStream("/test.properties");
Properties props = new Properties();
props.load(is);
String foo = props.getProperty("foo");
log.trace("foo: " + foo);
String hoge = props.getProperty("hoge");
log.trace("hoge: " + hoge);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Mavenのsiteでは、application.propertiesをsrc/resources/META-INF/に置くように書いてあるけど、当方の設定では、アクセスできません。誰か教えて!
Maven2 and Properties More ログイン