60

I have successfully built my Spring MVC project with mvn clean package by following this tutorial.

Now I am trying to run the service with:

mvn clean package && java -jar target/gs-serving-web-content-0.1.0.jar

But I get this error:

Failed to load Main-Class manifest attribute from target/gs-serving-web-content-0.1.0.jar

Am I missing something?

Simone
  • 20,302
  • 14
  • 79
  • 103
  • see http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven –  Oct 27 '13 at 16:31

12 Answers12

157

If you are working with Spring Boot this will solve your problem:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.2.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Reference Guide | Spring Boot Maven Plugin

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Mradul Pandey
  • 2,034
  • 1
  • 14
  • 12
  • 22
    Simply adding the above code and using `mvn package` did not work for me. I had to use `mvn package spring-boot:repackage` ,which created the standalone application. – twobeers Jul 19 '16 at 05:37
  • 4
    When i did this my integrationtests failed due to NoClassDeff... Adding post-integration-test to the execution solved it for me – Gurey Apr 25 '17 at 14:39
  • 2
    `mvn package spring-boot:repackage` resolved my issue. Thanks for the doc attached above. – mainframer May 05 '17 at 03:39
  • 1
    mvn package worked for me. I didn't have to do mvn spring-boot:repackage. I am using spring-boot 1.5.9.RELEASE + java 8 on mac os. If you have do mvn spring-boot:repackage, then there is no need to add the repackage goal in your pom.xml. The whole point of adding the repackage goal in pom.xml is that maven will automatically call spring-boot:package when you call mvn package. – user674669 Apr 05 '18 at 20:27
  • If you properly inherit from [`spring-boot-starter-parent`](http://www.springboottutorial.com/spring-boot-starter-parent), then you won't have to do the `repackage` step. – rustyx Nov 03 '18 at 11:55
  • 1
    It's not an obligation to inherit from spring-boot-starter-parent. Especially in big project developers come with their own parent pom for managing dependencies and controlling jar hell. – Mradul Pandey Nov 03 '18 at 13:48
21

You might be missing Spring Boot Maven Plugin.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Atish Narlawar
  • 227
  • 2
  • 3
3

You have to specifiy it in your pom.xml - This will make your jar executable with all dependencies (replace your.main.class):

<!-- setup jar manifest to executable with dependencies -->
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>your.main.class</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>  
    </execution>
  </executions>
</plugin>
Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
3

check the order of

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

it should be above

dockerfile-maven-plugin else repackage will happen

this solved my problem of no main attribute in manifest.

Musab Qamri
  • 111
  • 1
  • 8
1

You are missing maven-jar-plugin in which you need to add manifest tag.

1

For spring boot, I've created a MANIFEST.MF file inside META-INF folder.

in my STS IDE, I placed the META-INFO folder inside the src/main/resources folder like so:

screenshot from STS IDE (eclipse project)

the content of the MANIFEST.MF file:

Manifest-Version: 1.0
Implementation-Title: bankim
Implementation-Version: 1.5.6.RELEASE
Archiver-Version: Plexus Archiver
Built-By: Yourname
Implementation-Vendor-Id: com.bankim
Spring-Boot-Version: 1.5.6.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bankim.BankimApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://projects.spring.io/spring-boot/bankim/
  1. Every mention of "bankim"/"Bankim" refers to my project name, so replace it with your project name that
  2. take special note of the "Start-Class" value. it should contain the "path" to the class that has your main method.
  3. the row: Main-Class: org.springframework.boot.loader.JarLaunchershould be left as-is.

****the above manifest was created for me by using the "spring-boot-maven-plugin" mentioned above by "Mradul Pandey" (answered Sep 2 '15 at 4:50)

Hope this helps

Dror
  • 5,107
  • 3
  • 27
  • 45
  • You should not do this manually. When configured properly, spring-boot plugin creates this for you. It is puzzling though that not the same solution works for everyone. I had to add the executions part to the plugin e.g. else it failed. – Lawrence Apr 01 '19 at 13:51
  • i didn't create it manually, (see mentioned in step 3). but yes, not sure what caused this. it's been a while ... – Dror Apr 01 '19 at 19:27
1

java -jar target/gs-serving-web-content-0.1.0.jar command assumes the provided jar is executable. In an executable jar, the main method is defined in MANIFEST.MF file. Given that you don't have a MANIFEST.MF file, an additional command-line argument can be passed to specify the class containing the main method.

A command like java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass would serve the purpose.

Gaurav
  • 398
  • 8
  • 23
0

I had the spring-boot-maven-plugin but still I was getting the error regarding main class.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Finally I had to use the maven-jar-plugin and add the mainClass

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
            <mainClass>com.org.proj.App</mainClass>
            </manifest>
          </archive>
        </configuration>
    </plugin>

And it was good to go!

veer7
  • 20,074
  • 9
  • 46
  • 74
0

Use spring-boot-maven-plugin

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

and be sure you set property start-class in the pom.xml

<properties>
      <!-- The main class to start by executing "java -jar" -->
      <start-class>org.example.DemoApplication</start-class>
</properties>

Alternatively, the main class can be defined as the mainClass element of the spring-boot-maven-plugin in the plugin section of your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>             
            <executions>
                 <execution>
                     <goals>
                         <goal>repackage</goal>
                     </goals>
                     <configuration>
                         <classifier>spring-boot</classifier>
                         <mainClass>${start-class}</mainClass>
                     </configuration>
                 </execution>
             </executions>
        </plugin>
    </plugins>
</build> 
tycoon
  • 33
  • 6
0

If your plugin configuration is like mine (below), then, use mvn package spring-boot:repackage.

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Happy learning.

Bandham Manikanta
  • 1,858
  • 21
  • 21
0

You need to set your main class on MANIFEST.MF file:

Main-Class: com.example.MainClassApplication
bryan
  • 3
  • 1
  • 3
0

You can use

mvn package spring-boot:repackage

hit this command before target dir then use jar file

.

Rajat Kumar
  • 1,459
  • 1
  • 11
  • 10