Maven - Configuring MainClass

4

Problem:

When I create a JAR by eclipse it works quietly, but I'm trying to take advantage of the JAR that Maven is creating and I noticed that it does not execute, because my main class is not in Manifest .

Use this setting in POM.XML to inform my class Main :

        <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]

So I understood this warning and referring to the code I posted above, but I can not find a solution to the warning disappear. And even informing where class Main JAR is not running.

Doubt

If the code I am reporting in POM.XML is incorrect to inform the class Main which one is right? How to disappear with WARNING appearing whenever I make a mvn install ?

    
asked by anonymous 05.03.2014 / 19:26

2 answers

3

If you have external dependencies from other JARs, just use the maven-jar-plugin and put the full reference for your class that has the public static void mais(String[] args) method 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>

Run:

mvn install package
java -jar build/meu_main.jar 

If error java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException occurs, it is because there is 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 type of problem is to use the maven-assembly-plugin plugin that expands the declared dependencies in POM.xml as classes and then groups everything in the resulting JAR. So 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 execute (on MAC OS or Linux):

java -jar target/*jar-with-dependencies.jar
    
05.03.2014 / 20:08
1

Your main class should be declared this way:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <archive>
        <manifest>
            <addClasspath>true</addClasspath>
        <mainClass>com.exemplo.system.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    </plugin>

And the command to use would be mvn package

    
05.03.2014 / 19:33