Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top