gpt4 book ai didi

java - 如何使用 Maven 使可执行 jar 在一段时间内选择属性文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:11 25 4
gpt4 key购买 nike

您好,我正在使用 maven buil 创建一个可执行 jar,我几乎没有属性文件。如果我将属性文件放在

src/main/resources 

maven 将它们打包在 jar 本身中。我不希望发生这种情况,相反,我想将属性文件放在一个名为 conf 的文件夹中,并且我希望这些属性文件在运行时对 jar 可用。

之所以这样,是因为将来用户可以灵活地更改端口号等一些属性值,而无需。

我已经把 pom.xml 粘贴在下面了

<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.hp.nfv</groupId>
<artifactId>DescriptorA</artifactId>
<version>1.0.0</version>
<name>DescriptorA/name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.abc.Descripto</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>


</project>

我正在使用的属性文件是“utility.properties”,它存在于 src/main/resources 中

我在下面的 java 代码中使用它

ResourceBundle locationUtilityProp = ResourceBundle.getBundle("utility", locale);

但是当我执行上面的 pom.xml 文件时,我得到一个 jar 文件,它在运行时给出以下错误

java.util.MissingResourceException: Can't find bundle for base name utility

当我解压 jar 文件时,我发现里面没有 .properties 文件。

我是 maven 的新手,所以请任何人帮助我让这个 jar 在运行时从 src/main/resources 以外的目录结构中选择属性文件。

最佳答案

我得到了这个要求,下面我已经详细回答了,以便有一天它可以帮助其他人

包结构: 在进行 Maven 构建时,您需要将 .properties 文件放在 src/main/resources 中

src
|-main/java/com.abc/.java classes
|-main/resources/error.properties

maven 生成jar 文件后,创建一个名为config 的文件夹并将所有.properties 文件复制到其中。并将你的 jar 文件放在与你的配置文件夹相同的目录中,如下所示

example
|-config
-error.properties
|-jar file generated by maven

获取资源文件的代码

static Locale locale = new Locale("en", "US");
static ResourceBundle locationUtilityProp =ResourceBundle.getBundle("error", locale);

pom.xml 创建可执行jar文件

首先告诉 maven 不要包含 src/main/resources 中存在的任何属性文件

<build>


<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>

</resources>

现在包括 maven 编译器插件

<plugins>
<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>
</plugin>
</plugins>
</build>

现在包含 maven 程序集插件以创建可执行 jar。

在提到你希望maven选择资源文件的文件夹名称(在我的例子中是error.properties)

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.abc.hello</mainClass>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

关于java - 如何使用 Maven 使可执行 jar 在一段时间内选择属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com