1

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.

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
  • Its not entirely clear what your goal is. Do you want to a directory containing all of the build contents of your project except the config files? Or do you want to strip some files out of a dependency jar? – JBCP Dec 30 '13 at 17:21
  • 1
    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. – Afshin Moazami Dec 30 '13 at 17:27

1 Answers1

0

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.

JBCP
  • 13,109
  • 9
  • 73
  • 111
  • I still have a problem. I am using jar-with-dependencies descriptorRef. I don't know how to configure jar-without-config to include every dependencies and exclude config files – Afshin Moazami Dec 30 '13 at 19:12
  • I found it here: http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies – Afshin Moazami Dec 30 '13 at 19:15
  • 1
    Actually, I didn't use exactly what you said, since the dir copied the resources in the src/main/resources. However, using the jar-without-config, excluding the resources folder, and maven-resources-plugin, I got what I want :) – Afshin Moazami Dec 30 '13 at 19:28
  • You could use the config-dir and exclude the files in src/main/resources, but I'm glad you got it working. Thanks for accepting my answer. – JBCP Dec 30 '13 at 19:36