Java – no main manifest attribute, in jar

jarjavamanifestmavenmaven-shade-plugin

I am trying to run a jar created by the maven shade plugin. I am configuring the main class the following way:

<project>
...
<build>
<plugins>
  <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>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <manifestEntries>
                <Main-Class>org.comany.MainClass</Main-Class>
                <Build-Number>123</Build-Number>
              </manifestEntries>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

But when I try running the jar using java -jar app.jar it gives the following error

 "no main manifest attribute, in  app.jar"

EDIT:
I checked the contents of the jar using jar tf app.jar and I see a MANIFEST.MF file. BUt it does not have the entry for main class. How do I make sure the manifest file in jar has this entry apart for adding it in the shade plugin configuration?

Best Solution

Maven's shade plugin uses the JAR generated by the jar plugin and adds dependencies on it. Since it seems like the transforms on the shade plugin do not work properly, you just need to set the configuration for the jar-plugin like so:

<build>
  ...
  <plugins>
    ...     
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.mypackage.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
  <plugin>
    [Your shade plugin definition without the transformers here]
  </plugin> 
</build>
Related Question