How to package a project with a specified main function in Maven?

In order to specify the main class for Maven packaging, you need to configure the maven-compiler-plugin in the pom.xml file and specify the mainClass property.

Add the following code under the build node:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
            <mainClass>com.example.MainClass</mainClass>
        </configuration>
    </plugin>
</plugins>

Replace com.example.MainClass with the package path and class name of your main class.

Next, when packaging with Maven, execute the following command:

mvn clean package

This will designate the main class as the entry class and generate an executable JAR file.

bannerAds