Failed to load Main-Class manifest attribute while

2020-02-07 16:27发布

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?

8条回答
时光不老,我们不散
2楼-- · 2020-02-07 16:43

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.

查看更多
我只想做你的唯一
3楼-- · 2020-02-07 16:45

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

查看更多
ら.Afraid
4楼-- · 2020-02-07 16:47

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>
查看更多
我想做一个坏孩纸
5楼-- · 2020-02-07 16:51

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> 
查看更多
Fickle 薄情
6楼-- · 2020-02-07 16:58

You might be missing Spring Boot Maven Plugin.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
查看更多
手持菜刀,她持情操
7楼-- · 2020-02-07 16:58

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!

查看更多
登录 后发表回答