2015-04-23 44 views
5

之外我有一个app.properties文件在我的Maven项目resources文件夹下,如下所示(简化):负载的.properties从IDE文件,也从JAR

 
myApp 
    |----src 
    |  | 
    |  |--main 
    |   |--java 
    |   | |--ApplicationInitializer.java 
    |   | 
    |   |--resources 
    |    |--app.properties 
    | 
    |---target 
     |--myApp.jar 
     |--app.properties  

ApplicationInitializer类我想加载从性质该app.properties文件,下面的代码段:

Properties props = new Properties(); 

String path = "/app.properties"; 

try { 
    props.load(ApplicationInitializer.class.getResourceAsStream(path)); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

System.out.println(props.getProperty("property")); 

这段代码加载性能正常,当我运行它从我的IDE内,但失败例外

Exception in thread "main" java.lang.NullPointerException 
    at java.util.Properties$LineReader.readLine(Properties.java:434) 
    at java.util.Properties.load0(Properties.java:353) 
    at java.util.Properties.load(Properties.java:341) 
    at cz.muni.fi.fits.ApplicationInitializer.main(ApplicationInitializer.java:18) 

当试图以JAR文件运行时。

为了创建我使用的maven-shade-plugin组合的jar文件,maven-jar-plugin(用于排除属性文件的JAR的外部)和maven-resources-plugin(用于复制属性文件来特定文件夹)中pom.xml文件如下所示

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>2.3</version> 
     <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
        <goal>shade</goal> 
       </goals> 
       <configuration> 
        <transformers> 
         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
          <mainClass>cz.muni.fi.fits.ApplicationInitializer</mainClass> 
         </transformer> 
        </transformers> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 

    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-jar-plugin</artifactId> 
     <version>2.5</version> 
     <configuration> 
      <excludes> 
       <exclude>**/*.properties</exclude> 
      </excludes> 
     </configuration> 
    </plugin> 

    <plugin> 
     <artifactId>maven-resources-plugin</artifactId> 
     <version>2.7</version> 
     <executions> 
      <execution> 
       <id>copy-resource</id> 
       <phase>package</phase> 
       <goals> 
        <goal>copy-resources</goal> 
       </goals> 
       <configuration> 
        <outputDirectory>${basedir}/target</outputDirectory> 
        <resources> 
         <resource> 
          <directory>src/main/resources</directory> 
         </resource> 
        </resources> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 

我然后切换的代码在主方法这一个:

Properties props = new Properties(); 

String path = "./app.properties"; 

try (FileInputStream file = new FileInputStream(path)) { 
    props.load(file); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

System.out.println(props.getProperty("property")); 

并设法在运行JAR时从文件加载属性,但是这次我无法在IDE中运行时加载它们,它以与上述相同的异常结束。

所以我的问题是:如何设置文件路径(或pom.xml文件?),我将能够加载从IDE和JAR文件运行的属性?

感谢提前:)

+0

我'看target'你'myApp.jar'和'app.properties'。你可以检查'app.properties'是否在'.jar'文件中? – Kuba

+0

不,这就是为什么我使用'maven-jar-plugin'从jar中排除它 –

+0

为什么你要把属性文件放在jar文件之外?如果你这样做,你不能使用'getResourcesAsStream()'。 – khmarbaise

回答

0

你是做什么工作,你依靠你的java进程的工作目录的方式。通常,这是您的命令行(又名外壳)指向启动应用程序时。

在大多数IDE上,您可以在启动器设置中配置此目录。 (对于eclipse,它在第二个选项卡'Arguments'上)所以你需要到目标目录(对于eclipse有一个按钮'Workspace ...')

特别在intelliJ中,你可以在Run/Edit Configurations下找到这个设置...这将打开一个这样的窗口: enter image description here 在那里你可以编辑工作目录。您只需在最后添加目标。

编辑: 其实你已经在最后添加src/main/ressources。

+0

那么你的建议是什么?我很抱歉,但我不明白你的答案。 –

+0

我的建议是将您的IDE配置为使用正确的工作目录启动java进程。你在用什么IDE? –

+0

我正在使用IntelliJ IDEA 14 –

0

在Java代码中读取你的app.properties文件是这样的:

final String ROOT_PATH = "custom.path"; // Or whatever you most like 
final String PROPERTIES_FILE = "app.properties"; 

// start :: try-catch here to manage I/O resources 
File directory = new File(System.getProperty(ROOT_PATH), "conf"); 
File file = new File(directory, PROPERTIES_FILE); 
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); 
// ... 
// end :: try-catch here to manage I/O resources 

然后,你必须设置方式/宣称custom.path

  • 声明为变量的环境
  • 设置它在运行时(环境属性)与-Dcustom.path=/somewhere/in/your/filesystem(我更喜欢这一个,除非路径是固定的和/或共享acr oss不同的应用程序)

然后,最终你需要复制/把你的app.properties文件在/somewhere/in/your/filesystem/conf里面。以及为什么在/conf?因为您在声明directory字段时使用它。如果你不想要它,那就不要设置它并删除, "conf"部分。

此外,对于运行为 “本地”(在你的IDE)使用VM选项设置(IntelliJ IDEA的):

enter image description here

+0

谢谢,这看起来很有希望,但对我来说定义路径为环境变量有点矫枉过正,而且我也不能使用运行时参数,因为它会混淆我当前的参数。 –

+0

@Supermartzin我可以理解“环境变量”部分,但运行时参数(环境属性)可以有任意数量,并且它们不会互相干扰 –

+0

当然,你是对的,但它会贬低一个我的程序清晰,我想避免这种情况。无论如何感谢您的建议。 –