java - 如何配置 Maven 项目以从 config.properties 文件中读取?

标签 java maven properties pom.xml

我有一个带有 DAO 类的 servlet,它具有以下构造函数:

public DAO() {
   env = new Hashtable<String, String>();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
   env.put(Context.PROVIDER_URL, "ldap://localhost:1389");
   env.put(Context.SECURITY_AUTHENTICATION, "simple");
   env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
   env.put(Context.SECURITY_CREDENTIALS, "secret");
}

我在 source/main/resources 中编写了以下 config.properties 文件:

 INITIAL_CONTEXT_FACTORY=com.sun.jndi.ldap.LdapCtxFactory
 PROVIDER_URL=ldap://localhost:1389
 SECURITY_AUTHENTICATION=simple
 SECURITY_PRINCIPAL=cn=Directory Manager
 SECURITY_CREDENTIALS=secret

如何配置 pom.xml 文件来告诉 maven 使用 config.properties ?

最佳答案

在这里,我决定添加插件 block :

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
     <version>1.0-alpha-2</version>
      <executions>
       <execution>
        <phase>initialize</phase>
         <goals>
          <goal>read-project-properties</goal>
          </goals>
            <configuration>
             <files>
             <file>${basedir}/src/main/resources/config.properties</file>
             </files>
            </configuration>
       </execution>
     </executions>
    </plugin>

关于java - 如何配置 Maven 项目以从 config.properties 文件中读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293905/

相关文章:

java - 重复条目 : com/google/android/gms/analytics/internal/Command. 类

java - 跨多个项目共享单个 .editorconfig 文件

java - 从顶级模块中排除 Maven 子模块依赖项

delphi - 让 getter 返回私有(private) var 属性的值

适用于 Linux 和 Window 的 Java Spring 项目环境属性文件

java - Android Studio 从资源加载文件

java - Spring Boot 验证消息未解决

Hibernate示例程序中的java.lang.NullPointerException

java - Spring Bundlor 类似物

ant - Ant 如何将从文件中读取的值转换为属性值?