如何使用 Maven 使可执行 jar 在一段时间内选择属性文件
how to make a executable jar pick the properties file during time time using maven
您好,我正在使用 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 文件,它在 运行ning 给出了以下错误
java.util.MissingResourceException: Can't find bundle for base name utility
当我解压缩 jar 文件时,我发现其中没有 .properties 文件。
我是 maven 的新手,所以请任何人帮我制作这个 jar 在 运行 时间从 src/main/resources 以外的目录结构中选择属性文件。
如果我没听错,你想要一个可移植的、可配置的 jar,这样你就可以编辑配置而无需重新打包。这可以通过提供 -cp
(或 -classpath
)选项以及包含您 utility.properties
的文件夹的路径来轻松实现。所以调用看起来像:
java -cp ./conf -jar youApp.jar
假设配置位于主 jar 所在的 conf
子文件夹中。
有关 classpath 的更多信息。
我满足了这个要求,下面我已经详细回答了,以便有一天它可以帮助其他人
包结构:
在进行 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>
您好,我正在使用 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 文件,它在 运行ning 给出了以下错误
java.util.MissingResourceException: Can't find bundle for base name utility
当我解压缩 jar 文件时,我发现其中没有 .properties 文件。
我是 maven 的新手,所以请任何人帮我制作这个 jar 在 运行 时间从 src/main/resources 以外的目录结构中选择属性文件。
如果我没听错,你想要一个可移植的、可配置的 jar,这样你就可以编辑配置而无需重新打包。这可以通过提供 -cp
(或 -classpath
)选项以及包含您 utility.properties
的文件夹的路径来轻松实现。所以调用看起来像:
java -cp ./conf -jar youApp.jar
假设配置位于主 jar 所在的 conf
子文件夹中。
有关 classpath 的更多信息。
我满足了这个要求,下面我已经详细回答了,以便有一天它可以帮助其他人
包结构: 在进行 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>