Maven依赖于覆盖资源文件

use*_*535 8 resources overwrite maven

我有两个Maven模块,AB.A是一种依赖B.这两个模块有一个名为资源文件default.properties位于src/main/resources.我需要保持文件名中的两个项目,因为两者同和文件的位置相同A,并B使用代码预计将被命名和定位的文件在哪里.构建时B,A默认属性是最终的jar.我希望B在建造时有自己的属性B.我怎样才能做到这一点?

esa*_*saj 6

好吧,Maven Resources Plugin和Assembly插件没有削减它,所以我挖了一些.

看来这对于Maven Shade插件是可行的.

<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>
Run Code Online (Sandbox Code Playgroud)

因此,在<configuration> ... </configuration>-tags中我定义了两件事:一个转换器实现,负责修改jar-manifest可运行,并使用当前目录作为类路径根,并从内部排除所有以.properties结尾的文件.依赖org.something:SomeDependency.

实际的过滤部分是您可以排除不希望最终在由阴影构建的最终jar中的文件的位置.您可以<artifact>*:*</artifact>在已定义的内部使用内部排除所有依赖项和当前项目中的文件<filter>,或者您可以使用<artifact>dependcyGroupId:dependencyArtifact</artifact>,例如<artifact>junit:junit</artifact>,甚至使用通配符来选择某个依赖项(<artifact>*:junit</artifact>).然后在<excludes>...</excludes>-tags中定义排除的文件.同样,您可以使用确切的文件名或通配符.这应该可以帮助您解决当前的问题,尽管我建议您从插件站点阅读文档,因为阴影可以做得更多.


byy*_*yyk 6

我知道这是3岁但我有同样的问题,这是我找到的最接近的问题,但仍然没有正确的答案,所以也许有人会发现它有用.

基于jar-with-dependencies的示例maven-assembly描述符(修复了依赖项覆盖log4j.properties):

<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>
Run Code Online (Sandbox Code Playgroud)

关键是为依赖项和实际项目(层次结构顶部)提供不同的规则.这些可以通过使用<useProjectArtifact>false</useProjectArtifact>并为fileSets项目提供单独的规则来拆分.否则log4j.properties将无法包装,包括顶部的包装.