开发者

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 beca开发者_Go百科use 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?


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜