Maven adding mainClass in pom.xml with the right folder path

我的未来我决定 提交于 2019-11-29 02:52:51
Martin Baumgartner

First, your main class doesn't include src/main/java. Look at the package declaration in that Java file. For example, package org.jis;, then add the main class to that. In other words, it's only org.jis.Main.

You need to configure the maven-jar-plugin instead the of the maven-compiler-plugin. The jar-plugin is the one which is responsible for packaging and creating the manifest.MF.

From http://maven.apache.org/shared/maven-archiver/examples/classpath.html

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>

You have to use the fully qualified name of your main class:

<mainClass>org.jis.Main</mainClass>

Basically Maven is a framework which have a collection of maven-plugin for each and every action performed to build a project.

Each Build Phase is performed by corresponding maven-plugin using project's description Project Object Model(POM)

So maven-compiler plugin is just responsible to compile the project and it won't create the manifest file.

maven-jar-plugin is responsible for creating the manifest file of the project.

You may mention as below if it's a spring boot application.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.test.MainClass</mainClass>
            </configuration>                   
        </plugin>
    </plugins>
</build>

In case, You are using Spring Boot Application. Add the below in pom.xml

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

This will add the Main-Class entry in MANIFEST.MF file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!