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

7

78

I want to get a working jar file with my maven project.

The build part is:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.14</version>
            <dependencies>
                <dependency>
                    <groupId>com.puppycrawl.tools</groupId>
                    <artifactId>checkstyle</artifactId>
                    <version>6.4.1</version>
                </dependency>
            </dependencies>
            <configuration>
                <consoleOutput>true</consoleOutput>
                <configLocation>${basedir}/src/test/resources/checkstyle_swt1.xml</configLocation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptor>src/assembly/src.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            <addClasspath>true</addClasspath>
                            <mainClass>org.jis.Main</mainClass>                                
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

So my problem right now is that I don'T know how to implement the mainclass properly into my pom.xml and later into my jar file. The Folder Structure is: src/main/java/org/jis/Main.java but if I add the following line

<mainClass>src.main.java.org.jis.Main</mainClass>

It doesn't work.

Hunsinger answered 28/4, 2015 at 13:7 Comment(0)
F
124

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>
        <!-- Here come other details
        ...
        -->
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
        <!-- Here come other details
        ...
        -->
      </plugin>
    </plugins>
  </build>
Freeman answered 28/4, 2015 at 13:11 Comment(4)
@MartinBaumgarter with adding in my pom.xml I get a error. view my pom.xml image with project snapshot.Danyelldanyelle
I guess you should remove the 3 dots which are just a placeholder.Freeman
What changes is required to run the main class from src/test/javaGapin
You can't run a class from the test folder cause it's not part of the jar/warFreeman
A
9

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>
Anhwei answered 7/6, 2019 at 7:0 Comment(0)
L
4

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.

Lyndel answered 12/4, 2016 at 17:44 Comment(0)
R
2

Just leaving this here for individuals who were as frustrated as I was trying to get an incredibly simple example to work.

The issue I was having is that the maven jar archiver had some bad information cached, so if you are giving it the proper main class (or in my case you don't actually need to do that with the proper maven version and settings).

Try blowing away the "target" directory if you are using IntelliJ and have one. The "target" directory being the one maven default places the jar for instance.

Rainey answered 11/2, 2021 at 21:47 Comment(0)
C
1

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

<mainClass>org.jis.Main</mainClass>
Chitin answered 28/4, 2015 at 13:11 Comment(1)
While correct, it needs to be in the proper Maven plugin for it to workKato
A
0

It seems that some time has passed since the question but I'll share the solution I've reached for future readings.

I'm using a Maven Project in the IntellJ IDE. Even using the full path copied from the operation system like: C:\Users\Rogerio\Desktop\Script\src\main\java\Controller\MainClass.java, with or without .java or even trying the com.src... approach the main class was not found while creating the jar file for my application.

The solution was putting the CDATA tag inside the mainClass tag of the maven-jar plugin, as follows:

My file structures:

[A directory imagem structure on IntelliJ][1] [1]: https://i.sstatic.net/LHCWc.jpg

Inside my maven-jar plugin on pom.xml:

`<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass><![CDATA[Controller.MainClass]]></mainClass>
                    </manifest>
                </archive>
            </configuration>

`

Already answered 3/2, 2021 at 15:46 Comment(0)
A
-8

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.

Augustinaaugustine answered 12/9, 2017 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.