Files from different maven modules with the same name cannot coexist in a jar file created using the maven assembly-plugin

If there are two files with different content, but with the same name in two different maven-modules , which are both combined into one jar file using the maven assembly-plugin , only one file becomes part of the .jar file.

Question Is there a way to ensure that the contents of files are assembled into a single file when creating a jar file?

Obviously, I do not want to collect information manually, as I try to avoid this by breaking the project in different modules.

EDIT: I have a custom Assembly-Descriptor that I would like to save even if I start using another plugin. This descriptor basically excludes every language except English for resources and error texts.

<id>jar-with-dependencies</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> <unpackOptions> <excludes> <exclude>**/*Resources_*</exclude> <exclude>**/*ErrorsText_*</exclude> </excludes> </unpackOptions> </dependencySet> </dependencySets> 
+5
source share
2 answers

As stated in the maven-assembly-plugin documentation:

If your project wants to pack your artifact in uber-jar, the build plugin provides only basic support. For more control, use the Maven Shade Plugin .


Using the maven-shade-plugin , you can have a fat jar (for example, using the build plugin) and solve similar problems with file merging using Resource Transformers . In your case, AppendingTransformer will merge files with the same name, but with different content.

Some banks contain additional resources (such as property files) that have the same file name. To avoid overwriting, you can choose to merge them by adding their contents to a single file.

A simple configuration would look like this:

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>path/to/file/file-name-here</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> 

Update
You do not need an external build descriptor for the hue plugin; you can directly configure your requirements as a plugin configuration.
In your case, to exclude resources from collected cans, you can use hue filters .

A simple configuration (which will be combined with the above) will look like this:

 <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>**/*Resources_*</exclude> <exclude>**/*ErrorsText_*</exclude> </excludes> </filter> </filters> </configuration> 
+5
source

Also ran into this problem, I needed to filter resource files with the same name from dependency modules, for example:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>artifact1</artifact> <excludes> <exclude>application.yml</exclude> <exclude>logging.yml</exclude> </excludes> </filter> <filter> <artifact>artifact2</artifact> <excludes> <exclude>application.yml</exclude> <exclude>logging.yml</exclude> </excludes> </filter> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> 

pay attention . The lines below are for avoiding a possible exception:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for main manifest attributes

 <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> 

For more information, refer to Selecting Content for the Uber JAR

0
source

Source: https://habr.com/ru/post/1246553/


All Articles