温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java maven项目怎么读取配置文件信息

发布时间:2021-11-19 10:10:25 来源:亿速云 阅读:506 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关java maven项目怎么读取配置文件信息的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

maven项目读取配置文件信息

目录结构

java maven项目怎么读取配置文件信息

主类

App.java

package com.tomy.hive; 
import java.io.*;
import java.util.Properties;
 
/**
 * Hello world!
 *
 */
public class App {
    private static String JDBC_URL;
    private static String JDBC_DRIVER;
 
    /**
     * 读取配置文件
     * @return
     */
    public static void readConfigFile(String cfgFile) {
        try {
            InputStream in = App.class.getClassLoader().getResource(cfgFile).openStream();
            Properties prop = new Properties();
            prop.load(in);
            JDBC_URL = prop.getProperty("jdbc.url");
            JDBC_DRIVER = prop.getProperty("jdbc.driver");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        readConfigFile("resources/jdbc.properties");
        System.out.println(JDBC_URL);
        System.out.println(JDBC_DRIVER);
    }
}

配置文件

jdbc.properties

jdbc.url=jdbc:mysql://10.6.52.35:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
jdbc.driver=com.mysql.jdbc.Driver

pom文件

<?xml version="1.0" encoding="UTF-8"?> 
<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>com.tomy.hive</groupId>
  <artifactId>hive-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>hive-demo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <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>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.tomy.hive.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <executions>
            <execution>
              <id>copy-resources</id>
              <phase>validate</phase>
              <goals>
                <goal>copy-resources</goal>
              </goals>
              <configuration>
                <outputDirectory>${project.build.directory}/conf</outputDirectory>
                <resources>
                  <resource>
                    <directory>src/main/resources</directory>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
 
    </pluginManagement>
 
    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/**.properties</include>
        </includes>
        <targetPath>/resources</targetPath>
      </resource>
    </resources>
  </build>
</project>

控制台运行

java maven项目怎么读取配置文件信息

jar命令运行

java maven项目怎么读取配置文件信息

maven工程读取resources配置文件的正确姿势

我们maven项目结构如下:

java maven项目怎么读取配置文件信息

使用相对路径来读取resources目录下的资源文件

InputStream in = new InputStream(new File(“src/main/resources/car.txt”));

这样在本地运行的时候,是能正常读取到的,不会报错,但是如果打成jar包,运行的时候就会报路径错误。

从jar包的结构可以看到,resources目录的资源文件位置变了,在项目的最外层了,所以导致相对路径也发生了变化。

java maven项目怎么读取配置文件信息

这个时候,我们可以通过getClassLoader()方法来获取正确的配置文件路径。

(this也可以换成类的名称)

InputStream in = this.class.getClassLoader().getResourceAsStream(path);

这样,在本地运行或者jar包运行都能正常读取到配置文件了。

感谢各位的阅读!关于“java maven项目怎么读取配置文件信息”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI