0

I am building a command line tool which relies on some configuration files.

On development, I put those files under src/main/resources, and accessed by getClass().getResourceAsStream("/config-file-name"), which works well.

I choose maven-assembly-plugin to build the jar with all dependencies.

    <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.myproject.Scheduler</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                   <id>make-my-jar-with-dependencies</id>
                   <phase>package</phase>
                   <goals>
                       <goal>single</goal>
                   </goals>
               </execution>
            </executions>
    </plugin>       

The configuration files are all wrapped in jar, but I want to separate them.

So, I add following config,

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>conf.properties</include>
            </includes>
            <filtering>true</filtering>
            <excludes>
                <exclude></exclude>
            </excludes>
        <targetPath>..</targetPath>             
        </resource>
    </resources>

After done this, configuration files are along side with jar instead of wrapped in jar.

But my code can't access the files now.

Do I need to change my way of accessing the files in code?

Or should I need to not put those files in src/main/resources/, any alternative?

If someone knows, please help.

Thanks.


Update: Finally, I give up using maven resource file, instead, I set the file path to the same path as jar by default, everything works well except Eclipse complains.

Since I want to move the config file out of jar, and don't want it to be in target/classes folder, so I use <targetPath>..</targetPath> to move the configure file to the same directory as jar file.

When I re-generate eclipse project settings, Eclipse says: Cannot nest output folder "project/target/test-classes" inside output folder "project/target".

Anybody help me?

Chris Zheng
  • 1,509
  • 1
  • 15
  • 20
  • Is the location of the configuration file in your classpath? `getClass().getResourceAsStream("/config-file-name")` uses classpath locations to lookup for resources. – Guillaume Polet Mar 08 '12 at 07:30
  • Yes, it uses, but it's the normal way to access resource files in maven project. I think I may put my configuration files to some other place instead of src/main/resources, I prefer same dir as the running jar, I am looking for how to get running jar path inside code. – Chris Zheng Mar 08 '12 at 07:49
  • `System.getProperty("user.dir")` will give you path inside code. See [System.getProperties()](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29) and [System.getProperty(String key)](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty%28java.lang.String%29). – FrVaBe Mar 08 '12 at 08:05
  • @FrVaBe Yes, I already found it, and worked well. Thanks very much. – Chris Zheng Mar 08 '12 at 08:18

3 Answers3

0

Your application cannot access the configuration files since it is no longer in its classpath.

If you want to separate your configuration files from the rest of the application, you may want to relook at your assembly format. You may want to use bin instead of jar-with-dependencies, though it comes with challenges on distribution and usage.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
0

Depending on how complicated the property file is, you could use the resource plugin filter ability to change the value based on maven profiles. For example, if you wanted to have a property named location you could define a dev and production profile, and then set the value to a maven property in the pom (or profiles.xml if you're on maven 2):

<properties>
  <my.location>the_real_value</my.location>
</properties>

Then in the property file you'd do this:

location=${my.location}

Just make sure you set filtering on in your pom, like so:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

When you build, if you look in the target directory, and view that property file, you should see the correct value substituted in.

Michael
  • 6,141
  • 2
  • 20
  • 21
  • The property is complicated, so, I have to use a property file, but never mind, I no longer put it in src/main/resources, and access it by file path. – Chris Zheng Mar 09 '12 at 05:26
  • You should be able to use anything in the property and it'll work. So not sure why "complicated" is causing you to move the file and access it by file path which is a pretty unportable method to use. – Michael Mar 09 '12 at 13:02
  • It has lots of config settings which might be changed from time to time. I just want to separate the config file and the running jar, put them in pom.xml seems no help. – Chris Zheng Mar 11 '12 at 05:31
0

I finally give up using maven resource file, set the file path to the same path as jar by default instead, everything works well except Eclipse complains.

(Since I want to move the config file out of jar, and don't want it to be in target/classes folder, so I use .. to move the configure file to the same directory as jar file. When I re-generate eclipse project settings, Eclipse says: Cannot nest output folder "project/target/test-classes" inside output folder "project/target".)

I didn't find solution until now, seems a eclipse bug.

Now, I give up Eclipse, and changed to IntelliJ IDEA, it's a pretty nice IDE, especially perfectly integrated with Maven. I'll avoid any chance to use eclipse for any maven project.

Chris Zheng
  • 1,509
  • 1
  • 15
  • 20