user_mda
user_mda

Reputation: 19388

no main manifest attribute, in jar

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?

Upvotes: 12

Views: 13657

Answers (3)

jtalics
jtalics

Reputation: 103

Check if you have more than one main(). If you do, maven needs to know which one to use.

Upvotes: 0

Konstantin Fedorov
Konstantin Fedorov

Reputation: 181

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>

Upvotes: 18

Robert de W
Robert de W

Reputation: 306

Don't forget to add shade as a target:

mvn clean package shade:shade

Upvotes: 6

Related Questions