【发布时间】:2015-05-01 15:51:54
【问题描述】:
您好,我正在使用 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 程序集插件可以做到这一点,或者如果它不存在,您可以在运行时编写一个(默认)配置文件。
-
maven 程序集插件在运行时选择属性文件吗?即使它不存在于 src/main/resources 目录结构中
-
不,程序集插件仅用于构建您的发行版。您仍然需要在运行时读取文件,但打包的配置文件也是如此 - 唯一的区别是您如何从代码中访问文件(
new File("path/to/external/file")与getResource("path/to/packaged/file"))。
标签: java maven properties