1

I am having a problem with deploying jar files created by the maven-shade-plugin. It used to work, but it has stopped working. From my research the problem seems to be that the install step is overwriting the shaded jar.

If I use the following setting:

<shadedArtifactAttached>true</shadedArtifactAttached>

It will create a fat jar for me, but If I change that to

<shadedArtifactAttached>false</shadedArtifactAttached>

installing/deploying will remove the dependencies and the manifest entry for the main class.

I don't want to use the "true" setting, because the file rename will break all my tooling that depends on a generic script that will a deployed artifact via cURL.

So, when I run

$ mvn -s ../.m2/settings.xml --batch-mode --errors --fail-at-end --show-version clean package shade:shade

I get the correct files: ontology-merger-0.3.31.jar (big file) and original-ontology-merger-0.3.31 (small file)

when I run

$ mvn -s ../.m2/settings.xml --batch-mode --errors --fail-at-end --show-version clean package shade:shade install

the install step will trash the shaded jar. I am assuming that this happens because it's running the maven-jar-plugin over it, although I don't mention the maven-jar-plugin anywhere in my pom.xml. I have no idea how to switch this off.

Important bits from the log:

[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\dev\allotrope\source\ontology-qa-tools\merger\target\ontology-merger-0.3.31.jar with C:\dev\allotrope\source\ontology-qa-tools\merger\target\ontology-merger-0.3.31-shaded.jar
[INFO] Dependency-reduced POM written at: C:\dev\allotrope\source\ontology-qa-tools\merger\dependency-reduced-pom.xml
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ontology-merger --- [INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ ontology-merger --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ontology-merger --- [INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 30 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ ontology-merger --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ ontology-merger --- [INFO] Skipping execution of surefire because it has already been run for this configuration [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ ontology-merger --- [INFO] Building jar: C:\dev\allotrope\source\ontology-qa-tools\merger\target\ontology-merger-0.3.31.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ ontology-merger --- [INFO] Installing C:\dev\allotrope\source\ontology-qa-tools\merger\target\ontology-merger-0.3.31.jar to C:\Users\User\.m2\repository\com\osthus\ontology-merger\0.3.31\ontology-merger-0.3.31.jar
[INFO] Installing C:\dev\allotrope\source\ontology-qa-tools\merger\dependency-reduced-pom.xml to C:\Users\User\.m2\repository\com\osthus\ontology-merger\0.3.31\ontology-merger-0.3.31.pom

The section in my pom.xml currently looks like this:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.3</version>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.osthus.ontology_merger.MergerMain</mainClass>
                            <manifestEntries>
                                <Main-Class>com.osthus.ontology_merger.MergerMain</Main-Class>
                            </manifestEntries>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
                            <addHeader>false</addHeader>
                        </transformer>
                    </transformers>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <!-- Some jars are signed but shading breaks that. Don't include 
                                    signing files. -->
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>

My various fix attempts are in this branch:

https://gitlab.com/allotrope-open-source/ontology-qa-tools/-/tree/deployment-test

Path to the pom.xml:

https://gitlab.com/allotrope-open-source/ontology-qa-tools/-/blob/deployment-test/merger/pom.xml

A bit of history:

My deployment that had been working for years without a hitch already broke recently and I fixed it last month via

https://gitlab.com/allotrope-open-source/ontology-qa-tools/-/commit/7080d16179c2ccf7ab47208716b1bf1b3a05be29

and

https://gitlab.com/allotrope-open-source/ontology-qa-tools/-/commit/0d231d691ffd48347e34f9da9373d092939e69e2

where I had to add some boilerplate to the pom.xml files and change

$ mvn -s ../.m2/settings.xml --batch-mode --errors --fail-at-end --show-version clean deploy

to

$ mvn -s ../.m2/settings.xml --batch-mode --errors --fail-at-end --show-version clean package shade:shade deploy

Thanks for your time

karinc
  • 11
  • 2
  • Set it to false and rename it manually after building it? – dan1st Jun 29 '20 at 04:48
  • That will be my last resort, because then I'll have to scrap Maven deployment completely and write my own code instead. The whole point of Maven is not to have to do that, and my Maven has worked for years before it decided to break on its own. Thanks for the suggestion though :) – karinc Jun 29 '20 at 19:07
  • There may be some kind of `appendAssemblyId` configuration like in the assembly plugin... – dan1st Jun 29 '20 at 19:19
  • There is - `false`, but then the deployed jar will have a different name and all the consuming systems will have to be reprogrammed. So, I am trying to get back to the old behavior that Maven used to have until ca. 1 month ago. – karinc Jul 01 '20 at 04:08

1 Answers1

0

I have found a solution. Add this to the pom-xml:

<plugin>
    <groupId>maven</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

This will shift the execution of the maven-jar-plugin from the install phase to the package phase, so it will no longer destroy the output of the maven-shade-plugin. Deployment can then be done by calling:

mvn clean package shade:shade deploy
karinc
  • 11
  • 2
  • According to this https://maven.apache.org/plugins/maven-jar-plugin/usage.html the maven-jar-plugin runs in the package phase by default. – leftbit Jun 15 '21 at 14:38
  • Actually, my solution stopped working a while ago. deploy triggers install triggers the jar plugin again, so it's run twice and will overwrite the previous packaging. Since I deploy very rarely, I have decided to deploy manually, as it's more time efficient than spending a day of research every single time because something broke again. – karinc Jun 17 '21 at 07:45