6

When using the Maven assembly plugin (version 2.2-beta-5) it appears that the assembled jar will contain the resources from a dependency rather than the project being assembled if they have the same path. Specifically I'm trying to figure out how to use the project's log4j configuration file rather than one from a dependecy.

Project1

-src

--main

---resources

----log4j.xml

If Project1 has a dependency--call it Project2--that also has a log4j.xml file in src/main/resources then after running the assembly plugin the assembled jar contain Project2's log4j.xml file instead of Project1's. I believe this is because all the dependencys are unpacked first, and so when it tries to unpack the top level project the log4j.xml file is already present and so it is not overwritten.

Is there a make the assembly plugin use the project's files instead of the dependency's?

blong
  • 2,815
  • 8
  • 44
  • 110
Kris
  • 61
  • 1
  • 2
  • Related (similar in fact) question with additional detail in it's answer: [Custom maven assembly](http://stackoverflow.com/q/3493381/320399) – blong Aug 08 '13 at 16:37

2 Answers2

2

Something like this should work:

<assembly>
    <id>without-log4j-config</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <unpackOptions>
                <excludes>
                    <exclude>**/log4j.xml</exclude>
                    <exclude>**/log4j.properties</exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
    </dependencySets>
    <files>
        <file>
            <source>pom.xml</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <useDefaultExcludes>false</useDefaultExcludes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/java</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <moduleSets>
        <moduleSet>
            <includeSubModules>false</includeSubModules>
            <sources>
                <outputDirectoryMapping>/</outputDirectoryMapping>
                <excludeSubModuleDirectories>false</excludeSubModuleDirectories>
                <fileSets>
                    <fileSet>
                        <directory>src/main/java</directory>
                        <outputDirectory>/</outputDirectory>
                    </fileSet>
                    <fileSet>
                        <directory>src/main/resources</directory>
                        <outputDirectory>/</outputDirectory>
                    </fileSet>
                </fileSets>
            </sources>
            <binaries>
                <dependencySets>
                    <dependencySet>
                        <unpack>true</unpack>
                    </dependencySet>
                </dependencySets>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

And you'll need this in your pom:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>true</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/resources/whatever-you-name-the-snippet-above.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>

This is an important part:

<appendAssemblyId>true</appendAssemblyId>

Without it, if you run mvn assembly:assembly your custom assembly will be overwritten.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
1

One possibility is to explicitly exclude the relevant files from assembly creation.

    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>${project.groupId}:Project2</include>
        </includes>
        <unpack>true</unpack>
        <unpackOptions>
            <excludes>
                <exclude>**/log4j.xml</exclude>
            </excludes>
        </unpackOptions>
    </dependencySet>
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • 1
    This has to go in the assembly right? No way to just have one maven pom file with the right thing in it. : – ggb667 Apr 10 '14 at 16:42