6

当使用 Maven 组装插件(版本 2.2-beta-5)时,组装的 jar 似乎将包含来自依赖项的资源,而不是正在组装的项目(如果它们具有相同的路径)。具体来说,我试图弄清楚如何使用项目的 log4j 配置文件,而不是来自依赖项的配置文件。

项目1

-src

- 主要的

- -资源

----log4j.xml

如果 Project1 有一个依赖项——称之为 Project2——在 src/main/resources 中也有一个 log4j.xml 文件,那么在运行程序集插件之后,组装的 jar 包含 Project2 的 log4j.xml 文件而不是 Project1 的。我相信这是因为首先解压缩了所有依赖项,因此当它尝试解压缩顶级项目时,log4j.xml 文件已经存在,因此不会被覆盖。

是否有一个使程序集插件使用项目的文件而不是依赖项的文件?

4

2 回答 2

2

像这样的东西应该工作:

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

你会在你的 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>

这是一个重要的部分:

<appendAssemblyId>true</appendAssemblyId>

没有它,如果您运行mvn assembly:assembly自定义程序集,将被覆盖。

于 2010-12-28T02:09:35.753 回答
1

一种可能性是从程序集创建中明确排除相关文件。

    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>${project.groupId}:Project2</include>
        </includes>
        <unpack>true</unpack>
        <unpackOptions>
            <excludes>
                <exclude>**/log4j.xml</exclude>
            </excludes>
        </unpackOptions>
    </dependencySet>
于 2010-12-23T02:20:30.200 回答