Java Jar 指定 Config 路径
引言
在开发 Java 应用程序时,我们经常需要使用配置文件来设置应用程序的行为。通常情况下,我们会将配置文件与应用程序的代码放在同一个目录下,但有时我们可能希望将配置文件放在不同的位置,以便更好地管理和维护。
本文将介绍如何使用 Java 的 Jar 文件来指定配置文件的路径,以便在运行时可以轻松地切换不同的配置。
1. 设置配置文件路径
在 Java 中,我们可以使用类加载器来读取资源文件。通过将配置文件放置在 classpath 下的特定路径中,我们可以在运行时轻松地访问它们。
1.1 项目结构
假设我们的项目结构如下:
- src
  - main
    - java
      - com.example
        - MyApp.java
    - resources
      - config
        - myconfig.properties
配置文件 myconfig.properties 放在 resources/config 目录下。
1.2 读取配置文件
在 Java 中,我们可以使用 ClassLoader 的 getResourceAsStream 方法来读取配置文件。下面是一个简单的示例:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MyApp {
    public static void main(String[] args) throws IOException {
        // 通过类加载器读取配置文件
        InputStream inputStream = MyApp.class.getClassLoader().getResourceAsStream("config/myconfig.properties");
        
        // 创建 Properties 对象
        Properties properties = new Properties();
        
        // 加载配置文件
        properties.load(inputStream);
        
        // 读取配置项
        String value = properties.getProperty("key");
        
        // 输出配置项的值
        System.out.println("Value: " + value);
    }
}
上述代码通过类加载器读取了配置文件,并使用 Properties 类加载了配置文件的内容。然后我们可以使用 getProperty 方法获取配置项的值。
2. 打包成 JAR 文件
一旦我们的应用程序和配置文件都准备好了,我们可以将它们打包成一个 JAR 文件。JAR 文件是 Java 应用程序的一种常见发布格式,可以在不同的环境中使用。
2.1 使用 Maven 打包
如果你使用 Maven 构建项目,可以使用 Maven 的 maven-jar-plugin 插件来打包应用程序和配置文件。
首先,在 pom.xml 中添加以下配置:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.example.MyApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
上述配置中,我们在插件中指定了应用程序的入口类 com.example.MyApp。
然后,在命令行中执行以下命令来构建 JAR 文件:
mvn clean package
Maven 将会在 target 目录下生成一个 JAR 文件,包含应用程序和配置文件。
2.2 打包成可执行 JAR 文件
如果我们希望将 JAR 文件打包成一个可执行的 JAR 文件,可以使用 Maven 的 maven-assembly-plugin 插件。
首先,在 pom.xml 中添加以下配置:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.MyApp</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
上述配置中,我们在插件中指定了应用程序的入口类 com.example.MyApp。
