java – Maven – Configuring the MainClass

Question:

Problem:

When I create a JAR through eclipse it works fine, but I'm trying to take advantage of the JAR that Maven is creating and I realized that it doesn't run, because my main class is not in Manifest .


Use this configuration in POM.XML to tell my Main class:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>ClasseMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

When I use mvn install I get:

[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.app:ClasseMain:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 52, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

As I understand this warning and referring to the code I posted above, but I can't find a solution for the warning to disappear. And even informing where the Main class is, the JAR does not run.


Doubt

If the code that I am informing in the POM.XML is incorrect to inform the Main class which one is correct? How to disappear with the WARNING that appears whenever I do an mvn install ?

Answer:

If you don't have external dependencies on other JARs, just use the maven-jar-plugin and put the complete reference to your class that has the public static void mais(String[] args) as in the example below.

<build>
    <finalName>meu_main</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>qualquer.que.seja.seu.pacote.ClasseMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Execute :

mvn install package
java -jar build/meu_main.jar 

If the error java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException occurs, it is because there is an external dependency.

This approach mentioned above does not work if your application depends on an external JAR such as Log4J.jar, for example. One of the alternatives to solve this kind of problem is to use the maven-assembly-plugin which expands the dependencies declared in POM.xml as classes and then groups everything in the resulting JAR. That way you get all the classes and resources (images, property files, etc) available in the final generated JAR. In this case you get rid of java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException errors

Example with maven-assembly-plugin

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>qualquer.que.seja.seu.pacote.ClasseMain</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

In this case you should invoke the JAR build like this:

mvm assembly:assembly

To find the generated Jar run (on MAC OS or Linux):

ls target/*jar-with-dependencies.jar

And to run run (on MAC OS or Linux):

java -jar target/*jar-with-dependencies.jar
Scroll to Top