Search code examples
javabuildmaven-3

MaVeN build takes, most of the times, wrong file


I use the org.apache.maven.plugins maven-assembly-plugin version 2.2-beta-5 to build a jar-with-dependencies. One of the jars I include contains a persistence.xml file. The project that I build also has a persistence.xml file. The build finishes well.

The problem however is that most of the times the wrong persistence.xml file ends up in the jar-with-dependencies. If I, without changing anything, rebuild (and sometimes a few more times) then the correct persistence.xml is present.

I searched for a solution but I could not find a working example. How can I define in my pom.xml that I want my project's persistence.xml file in the jar-with-dependencies and not the one from the included jar?

My MaVeN version is Apache Maven 3.6.3 and all builds are done with mvn clean package.


Solution

  • Use maven-shade-plugin instead of mavenassembly-plugin to exclude the persistence.xml file from your dependencies

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>my:other:jar</artifact>
                            <excludes>
                                <exclude>persistence.xml</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>        
    

    More info on this approach in the Apache Maven Shade Plugin documentation