Question

I have searched for a plugin/sets of plugins for maven to include all the dependencies in a jar file except configuration files in resources folder.

I found assembly plugin a good solution for including all the assembly, and using the resources and exclude tags, I have excluded the config file.

But, the question is how can I copy these files to a folder next to my jar file?

In other words, what should I do if I want a single jar (including all the dependencies) and a folder containing my config (.xml, .properties files). So, when I want to run it, I have just add the config folder to the classpath and run the jar file.

Thanks,

P.S. If you find a duplicate question with a clear accepted answer, I will appreciate.

Was it helpful?

Solution

You can get a directory output from the maven-assembly-plugin by setting the format to ''dir'' in your descriptor XML file like this:

<assembly>
    <formats>
        <format>dir</format>
    </formats>
    .....
</assembly>

Then using dependencySets and fileSets to include or exclude the files that you want.

To get a directory of just config files, use a fileSet to include only the files you want into this new directory.

You could use a second assembly descriptor to build a jar that excludes the config files, and then you could include both the directory and jar on the class path.

To have two executions of the same plugin, you do something like this:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-assembly-plugin</artifactId>
     <executions>
         <execution>
             <id>config-dir</id>
             <configuration>
                 ....
             </configuration>
         </execution>
         <execution>
             <id>jar-without-config</id>
             <configuration>
                 ....
             </configuration>
         </execution>
     </executions>
 </plugin>

See here for more details:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

Alternative Implementation

This is beyond your questions scope and is better suited to another question, but I think a better alternative would be to use a framework like Spring that allows your external config files to override your internally located ones, in which case you could use the default jar mechanism and not need to worry about excluding your template config files. For example, with Spring you can do something like this:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:/myproject/defaults.properties</value
            <value>file:${user.home}/myproject.properties</value>
        </list>
    </property>
</bean>

This will load your internal 'defaults.properties' from the jar, and then load 'myproject.properties' from your home directory. Any values set in myproject.properties will override the values set in defaults.properties.

I hope that helps.

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