Maven shade plugin can not find 'mainClass' exception?
Asked Answered
B

3

5

There are a very strange build with maven-shade-plugin:2.4.3 in a standard maven project. It was throw can not find mainClass exception always when execute maven package command. I just simulate an maven-shade-plugin example, see below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.core.utils</groupId>
    <artifactId>dateUtils</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>dateUtils</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jdk.version>1.7</jdk.version>
        <jodatime.version>2.5</jodatime.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${jodatime.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>dateutils</finalName>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>

            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <!-- Maven Shade Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
          </execution>
                </executions>
        <configuration>
            <shadedArtifactAttached>true</shadedArtifactAttached>
            <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                    </excludes>
                </filter>
            </filters>
            <transformers>
              <transformer
                      implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.mkyong.core.utils.App</mainClass>
              </transformer>
          </transformers>
        </configuration>

            </plugin>

        </plugins>
    </build>

</project>

This one can run success. Then just change the main Class name, and other like this:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
                <configuration>
                    <transformers>
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>xxxxxxx</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </plugin>

the parent pom.xml file about the plugin line:

<pluginManagement>
    <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>
                </execution>
            </executions>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </plugin>
    </plugins>

</pluginManagement>

So the modules will extends parent, but when package it throws exception, said:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.4.3:shade (default) on project analytics-sdk-storm-local: Unable to parse configuration of mojo org.apache.maven.plugins:maven-shade-plugin:2.4.3:shade for parameter mainClass: Cannot find 'mainClass' in class org.apache.maven.plugins.shade.resource.AppendingTransformer -> [Help 1]

But why it was happened.There are the maven dependencies tree show:

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ xxxxx ---
[INFO] local:jar:1.0.1-SNAPSHOT
[INFO] +- org.apache.kafka:kafka_2.10:jar:0.8.1.1:compile
[INFO] |  +- com.yammer.metrics:metrics-core:jar:2.2.0:compile
[INFO] |  +- org.xerial.snappy:snappy-java:jar:1.1.2:compile
[INFO] |  +- net.sf.jopt-simple:jopt-simple:jar:3.2:compile
[INFO] |  +- org.scala-lang:scala-library:jar:2.10.1:compile
[INFO] |  \- com.101tec:zkclient:jar:0.3:compile
[INFO] +- org.apache.storm:storm-core:jar:0.9.3:provided
[INFO] |  +- org.clojure:clojure:jar:1.5.1:provided
[INFO] |  +- clj-time:clj-time:jar:0.4.1:provided
[INFO] |  +- joda-time:joda-time:jar:2.8.2:provided
[INFO] |  +- compojure:compojure:jar:1.1.3:provided
[INFO] |  +- org.clojure:core.incubator:jar:0.1.0:provided
[INFO] |  +- org.clojure:tools.macro:jar:0.1.0:provided
[INFO] |  +- clout:clout:jar:1.0.1:provided
[INFO] |  +- ring:ring-core:jar:1.1.5:provided
[INFO] |  +- commons-fileupload:commons-fileupload:jar:1.3.1:provided
[INFO] |  +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] |  +- hiccup:hiccup:jar:0.3.6:provided
[INFO] |  +- ring:ring-devel:jar:0.3.11:provided
[INFO] |  +- clj-stacktrace:clj-stacktrace:jar:0.2.2:provided
[INFO] |  +- ring:ring-jetty-adapter:jar:0.3.11:provided
[INFO] |  +- ring:ring-servlet:jar:0.3.11:provided
[INFO] |  +- org.mortbay.jetty:jetty:jar:6.1.26:provided
[INFO] |  +- org.mortbay.jetty:jetty-util:jar:6.1.26:provided
[INFO] |  +- org.clojure:tools.logging:jar:0.2.3:provided
[INFO] |  +- org.clojure:math.numeric-tower:jar:0.0.1:provided
[INFO] |  +- org.clojure:tools.cli:jar:0.2.4:provided
[INFO] |  +- commons-io:commons-io:jar:2.4:provided
[INFO] |  +- org.apache.commons:commons-exec:jar:1.1:provided
[INFO] |  +- commons-lang:commons-lang:jar:2.5:provided
[INFO] |  +- com.googlecode.json-simple:json-simple:jar:1.1.1:provided
[INFO] |  |  \- junit:junit:jar:4.12:provided
[INFO] |  |     \- org.hamcrest:hamcrest-core:jar:1.3:provided
[INFO] |  +- com.twitter:carbonite:jar:1.4.0:provided
[INFO] |  +- com.esotericsoftware.kryo:kryo:jar:2.21:provided
[INFO] |  +- com.esotericsoftware.reflectasm:reflectasm:jar:shaded:1.07:provided
[INFO] |  +- org.ow2.asm:asm:jar:4.0:compile
[INFO] |  +- com.esotericsoftware.minlog:minlog:jar:1.2:provided
[INFO] |  +- org.objenesis:objenesis:jar:1.2:compile
[INFO] |  +- com.twitter:chill-java:jar:0.3.5:provided
[INFO] |  +- org.yaml:snakeyaml:jar:1.16:provided
[INFO] |  +- commons-logging:commons-logging:jar:1.1.3:provided
[INFO] |  +- commons-codec:commons-codec:jar:1.9:provided
[INFO] |  +- com.googlecode.disruptor:disruptor:jar:2.10.1:provided
[INFO] |  +- org.jgrapht:jgrapht-core:jar:0.9.0:provided
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.1.5:provided
[INFO] |  +- ch.qos.logback:logback-core:jar:1.0.13:provided
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] |  +- org.slf4j:log4j-over-slf4j:jar:1.7.16:provided
[INFO] |  \- jline:jline:jar:2.11:compile
[INFO] +- org.apache.storm:storm-kafka:jar:0.9.3:compile
[INFO] |  \- org.apache.curator:curator-framework:jar:2.5.0:compile
[INFO] |     +- org.apache.curator:curator-client:jar:2.5.0:compile
[INFO] |     +- org.apache.zookeeper:zookeeper:jar:3.4.6:compile
[INFO] |     |  \- io.netty:netty:jar:3.7.0.Final:compile
[INFO] |     \- com.google.guava:guava:jar:19.0:compile
[INFO] +- so.sao.analytics:analytics-sdk-common:jar:1.0.1-SNAPSHOT:compile
[INFO] |  +- com.esotericsoftware:kryo:jar:3.0.3:compile
[INFO] |  |  +- com.esotericsoftware:reflectasm:jar:1.10.1:compile
[INFO] |  |  \- com.esotericsoftware:minlog:jar:1.3.0:compile
[INFO] |  \- org.mapdb:mapdb:jar:2.0-beta12:compile
[INFO] \- so.sao.analytics:analytics-sdk-storm-common:jar:1.0.1-SNAPSHOT:compile

I real don't known why it was failed. Now just annotation the code. Is you had meet this one, show me, thanks.

Boorer answered 30/3, 2016 at 6:28 Comment(1)
C
8

I had this same issue. Turned out it was caused by some config Spring Boot brings in. I am using spring-boot-starter-parent as my parent pom.

I resolved the issue by setting the start-class property and removing the ManifestResourceTransformer configuration from the shade plugin in my pom.xml.

<properties>
  <start-class>package.Main</start-class>
</properties>

I found this property by searching the effective pom for ManifestResourceTransformer.

Consumerism answered 30/4, 2018 at 20:26 Comment(0)
R
0

Based on what you have posted you have removed to much from the plugin configuration. In the <executions> section you need to specify the goal.

amend your pom.xml as below

<project>
    ...
    <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>
                    </execution>
                </executions>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>your.Main</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Executing mvn package will create in ./target the shaded Jar. In the log you should see something similar

[INFO] Replacing ../target/..-SNAPSHOT.jar with ../target/..-SNAPSHOT-shaded.jar

As you don't show your pom.xml have a look at following discrepancy.

in the posted pom.xml snippet you have as implementation class

org.apache.maven.plugins.shade.resource.ManifestResourceTransformer

but the exception complains about

org.apache.maven.plugins.shade.resource.AppendingTransformer
Rachaba answered 30/3, 2016 at 7:13 Comment(3)
yeah, there had same configure in my parent module pom.xml file. Also had refer to the official usage maven-shade-plugin/examples/resource-transformers. But it was still failed. and in my other project these setting run well. It's so strange.Boorer
@Boorer Have a look at my updated answer. Maybe in your actual used pom.xml there is some kind of typo.Rachaba
Thanks, the core in my pom.xml file were same as demo, you can see my upgrade question. Also I feel is strange same as your,why it throws AppendingTransformer exception. Maybe there other some error setting in my project?Boorer
S
0

For anyone that also has spring-boot-starter-parent as a parent module: check your effective pom. If you need to have a separate execution, unlike @Dima's answer, find out what execution id has Spring configured for theirs. If there's none, then it's 'default' and you can disable it for your child pom by changing the execution phase as follows:

<execution>
  <id>default</id>
  <phase>never (or any nonexistent phase)</phase>
</execution>
Scapula answered 26/7, 2023 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.