10

I have two Maven modules, A and B. A is a dependency of B. Both modules have a resource file named default.properties located in src/main/resources. I need to keep the filenames the same and the location of the file the same in both projects because both A and B are using code which expects the file to be named and located where it is. When building B, A's default properties is in the final jar. I wish to have B's properties when I build B. How can I do this?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
user1040535
  • 201
  • 1
  • 3
  • 14

2 Answers2

8

I know this is 3 years old but I had the same problem and this is the closest question I found, but still without correct answer so maybe someone will find it useful.

Example maven-assembly descriptor based on jar-with-dependencies (fixes overriding of log4j.properties by dependencies):

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>true</unpack>
            <unpackOptions>
                <excludes>
                    <exclude>log4j.properties</exclude>
                </excludes>
            </unpackOptions>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

The key is to provide different rules for dependencies and the actual project (top of hierarchy). Those can be split by using <useProjectArtifact>false</useProjectArtifact> and providing separate rules in fileSets for the project. Otherwise none of log4j.properties would be packed, including the top one.

mckulpa
  • 374
  • 4
  • 9
6

Ok, Maven Resources Plugin and Assembly plugin did not cut it, so I dug some more.

It seems this is doable with Maven Shade plugin.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <!-- Main class -->
                                <mainClass> <!-- fully qualified package and class name --> </mainClass>
                                <manifestEntries>
                                    <Class-Path>.</Class-Path>
                                </manifestEntries>
                            </transformer>
                        </transformers>

                        <filters>
                            <filter>
                                <artifact>org.something:SomeDependency</artifact>
                                <excludes>
                                    <exclude>*.properties</exclude>
                                </excludes>
                            </filter>
                        </filters>

                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

So, inside the <configuration> ... </configuration> -tags I've defined two things: a transformer-implementation that takes care of modifying the jar-manifest to be runnable and use the current directory as classpath root, and excluding all the files ending with .properties from inside of dependency org.something:SomeDependency.

The actual filtering part is where you can exclude the files you don't want to end up in the final jar built by shade. You can exclude the files from all the dependencies and the current project using <artifact>*:*</artifact> inside the defined <filter>, or you can select only certain dependency using <artifact>dependcyGroupId:dependencyArtifact</artifact>, for example <artifact>junit:junit</artifact>, or even using wildcards for one or the other (<artifact>*:junit</artifact>). The excluded files are then defined inside the <excludes>...</excludes> -tags. Again, you can use exact filenames or wildcards. This should get you going with your current problem, although I'd suggest reading the documentation from the plugin-site, because shade can do a lot more than this.

esaj
  • 15,875
  • 5
  • 38
  • 52
  • Great, so how do I exclude a resource from a dependency? The link you provided only applies to the current project, not the dependency. – user1040535 Nov 11 '11 at 02:05
  • Sorry, guess I read your question wrong, I thought you meant excluding from the current project. Do you have A's sources and pom available? – esaj Nov 12 '11 at 08:00
  • I checked the new link you provided; no concrete answer was provided. I do have A's sources and pom available. – user1040535 Nov 13 '11 at 02:13
  • Third time's the charm, I hope, I tried maven shade with a couple of dummy projects, and got it to exclude exactly what I wanted. Also, if both A and B contained the same file in same path and there were no exclusions configured, it seems shade overwrote the one from the dependency with the one from the current project, but to be sure I'd suggest using the configured filters. – esaj Nov 13 '11 at 11:25
  • Shade's own examples for this kind of problem: http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html – fnl Apr 05 '16 at 13:32