Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Assembly Plugin And Resources

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?

like image 837
Kris Avatar asked Dec 22 '10 21:12

Kris


People also ask

What is the Maven Assembly plugin?

The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.

How do I add resources to Maven?

You can easily do that using the maven resource plugin. Basically, what it does by default (invoking the resources:resources goal) is moving the content of all directories listed in the build/resources section using the declared filter into the ${project.

What is fileset in Maven?

Defines the rules for matching and working with files in a given base directory. Element.


2 Answers

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.

like image 105
javamonkey79 Avatar answered Oct 11 '22 12:10

javamonkey79


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>
like image 42
Raghuram Avatar answered Oct 11 '22 12:10

Raghuram