6

I have a multi-module project and I would like to define a single filter file that will apply properties to all the child modules. For example, I would like to have the following structure:

parent
- filter.properties
- module1
    - resource1
- module2
   - resource2

such that when I build parent, it would apply the filter to resource1 and resource2 when the child modules are built.

If I create the structure above and define the filter in the parent's POM, the build fails because it expects the filter to be defined in each child module... Is there a simple way to do this that I'm overlooking?

skaffman
  • 398,947
  • 96
  • 818
  • 769
David
  • 105
  • 2
  • 7

3 Answers3

4

This answer describes how to extend the properties-maven-plugin to allow properties files to be shared between projects. If you use that plugin the properties would be made available to the build, and therefore can be used when filtering the resources.

Alternatively, you can specify a filter file as an attached artifact on some build, so that it is available in the repository, then use the dependency plugin to download the filter file, finally specify that the filter use the shared file.

To attach the filter to your parent build, use the build-helper-maven-plugin's attach goal:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>src/main/resources/shared-filter.properties</file>
              <type>properties</type>
              <classifier>filter</classifier>
            </artifact>
          </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>

When the project hosting the filter is deployed, the filter will now be attached alongside it.

To download the filter file into your project, use the maven-dependency-plugin's copy-dependencies goal to download the file:

  <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>name.seller.rich</groupId>
              <artifactId>shared</artifactId>
              <version>1.0.0</version>
              <classifier>filter</classifier>
              <type>properties</type>
              <overWrite>false</overWrite>
              <destFileName>shared-filter.properties</destFileName>
            </artifactItem>
          </artifactItems>
          <outputDirectory>
            ${project.build.directory}/filters
          </outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

If the dependency plugin configuration is defined in your parent project, all other projects can inherit the configuration and won't need to redefine it.

Then to use the downloaded filter:

<filters>
  <filter>${project.build.directory}/filters/shared-filter.properties</filter>
</filters>
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
Community
  • 1
  • 1
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • +1 Nice answer Rich. Ever heard about this: http://www.mail-archive.com/announce@maven.apache.org/msg00202.html ? – Pascal Thivent Oct 06 '09 at 22:07
  • Thanks Rich, that should work for me. Not quite a simple as I'd hoped though :) – David Oct 06 '09 at 22:32
  • I tried the above solution, it works partially. when I try to do mvn install it copies the resources but at the end it also tries to deploy them to the repository and during that time it looks for the resource in src/main/resource of the sub module and I get the following exception – user373201 Jan 03 '11 at 04:55
  • Error installing artifact: File /media/.../certificates/src/main/resources/filters/mr.filter.properties does not exist – user373201 Jan 03 '11 at 04:56
  • This is the approach I'm going with, although I ran into a small issue. This answer suggests attaching the .properties file to your parent POM and configuring the maven-dependency-plugin there as well. The problem with this is that when the parent POM is building, it's going to look for that .properties artifact which it has not yet installed. A work-around is to configure the maven-dependency-plugin within the sub-modules instead. This is unfortunate because that configuration will be duplicated. – Joe Snikeris Jan 06 '12 at 20:52
4

I don't know if this is an option but, instead of defining the properties in an external file, you could also define them in the properties section of your pom.xml and you'd get the same effect:

<project>
  ...
  <properties>
    <my.filter.value>hello</my.filter.value>
  </properties>
</project>

So, defining <properties> in the parent pom should allow you to filter child modules easily.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

you can also share the filter by using an absolute path for the filter in your parent pom.xml

Since the path is dependent on environment (eclipse windows / eclipse linux / buildserver) you can put the path prefix or part of it in settings.xml profile