Search code examples
javamavenjarmaven-assembly-pluginmaven-jar-plugin

How am I supposed to choose the location of config files in maven project


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?


Solution

  • 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.