Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven shade plugin can not find 'mainClass' exception?

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.

like image 325
elkan1788 Avatar asked Mar 30 '16 06:03

elkan1788


1 Answers

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.

like image 157
Dima Avatar answered Nov 15 '22 03:11

Dima