Lewis Wong
Lewis Wong

Reputation: 269

Maven packing properties file

I have a maven multi-module projects layered as follows.

test-integration 
   - test-integration.properties
test-services
   - test-services.properties
test-persistence
   - test-persistence.properties

There is a property file in each of the project. How should I config maven in a way to excludes all *.properties from the jar, at the same time extract all of them to a config folder?

Upvotes: 1

Views: 1708

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

You can add in your aggregator/parent project (so that it will be applied to all the declared modules) the following:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludes>
                    <exclude>**/test-*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>test-*.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In details:

  • The Maven Jar Plugin is excluding properties files from the packaged jar and it is also configuring the target directory as lib (as you described in your question)
  • The Maven Resources Plugin is copying to the target/conf directory (again, as you described above) any properties file starting by test- and provided by the src/main/resources folder.

I tested in a sample project and it worked fine.

Also note: your IDE may then show the conf directory as part of your project (seeing it as new directory resources, it happened to me for Eclipse): that's not a new folder created, but just the same folder shown in a different view. If you really don't want to have this side effect, then consider using the maven-antrun-plugin instead of the Resources plugin.

Upvotes: 1

Related Questions