【问题标题】:how to make a executable jar pick the properties file during time time using maven如何使用 maven 在时间段内使可执行 jar 选择属性文件
【发布时间】: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


【解决方案1】:

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

包结构: 进行 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>

【讨论】:

  • 多么敬业啊,其实我也有类似的情况。我的意思是,读取外部属性文件不起作用。我试试你的方法,谢谢!
【解决方案2】:

如果我没听错的话,你想要一个可移植的、可配置的 jar,这样你就可以在不重新打包的情况下编辑配置。这可以通过提供-cp(或-classpath)选项以及包含您utility.properties 的文件夹的路径来轻松实现。所以调用看起来像:

java -cp ./conf -jar youApp.jar 

假设配置位于主 jar 所在的 conf 子文件夹中。

更多关于classpath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2015-12-10
    • 2016-11-08
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多