Maven overwrites resource file depending

I have two modules Maven, A and B A is a dependency on B Both modules have a resource file called default.properties located in src/main/resources . I need to keep the file names the same, and the file location is the same in both projects, because both A and B use code that expects the file to be named and located where it is. When building B , A , the default properties are in the last jar . I want to have properties B when building B How can i do this?

+7
source share
2 answers

Ok, the Maven Resources Plugin and Assembly plugin didn't cut it, so I dug up a little more.

This seems to be doable with the Maven Shade plugin .

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

So, inside <configuration> ... </configuration> -tags, I defined two things: a transformer implementation that modifies the jar-manifest to be run, and use the current directory as the root of the classpath and exclude all files ending with with .properties from within the org.something: SomeDependency dependency.

The actual part of filtering is where you can exclude files that you don’t want to end up in the final shade-created jar. You can exclude files from all dependencies and the current project using <artifact>*:*</artifact> inside a specific <filter> , or you can select only a specific dependency using <artifact>dependcyGroupId:dependencyArtifact</artifact> , for example <artifact>junit:junit</artifact> , or even using wildcards for one or the other ( <artifact>*:junit</artifact> ). Excluded files are then defined inside <excludes>...</excludes> -tags. Again, you can use exact file names or wildcards. This should make you go with your current problem, although I would suggest reading the documentation from the plugin site, because the shadow can do a lot more.

+6
source

I know that this is 3 years, but I had the same problem, and this is the closest question I found, but still without the right answer, so maybe someone will find it useful.

An example maven-assembly descriptor based on jar-with-dependencies (fix log4j.properties overriding over dependencies):

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

The key is to provide different rules for dependencies and the actual project (top of the hierarchy). They can be separated using <useProjectArtifact>false</useProjectArtifact> and provide separate rules in fileSets for the project. Otherwise, none of log4j.properties will be packaged, including the top one.

+6
source

All Articles