Maven - Configuring the Mainclass

Asked

Viewed 2,530 times

4

Problem:

When I create a JAR for eclipse it works smoothly, but I’m trying to enjoy the JAR that the Maven is creating and I realized that it does not perform, because my class main is not in the Manifest.


Use this setting on 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 the mvn install receiving:

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

From what I understand this warning and referring to the code I posted above, but I cannot find a solution for the warning to disappear. And even reporting where class is Main the JAR does not run.


Doubt

If the code I am reporting on POM.XML is incorrect to inform the class Main What is the right one? How to disappear with the WARNING that appears whenever I make a mvn install?

2 answers

3


Should you did not have external dependencies of other Jars just use the Maven-jar-plugin and put the complete reference to your class that has the method 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 occurs java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException 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 plugin maven-assembly-plugin which expands the dependencies declared 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 generated final JAR. In this case you get rid of the mistakes java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException

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 (on MAC OS or Linux) :

java -jar target/*jar-with-dependencies.jar
  • Interesting. I noticed that mvn package or install generate a JAR in the briefcase target. Use . or / along those lines <mainClass>com.aplication.ClasseMain</mainClass> also has no influence, the JAR generated works. The strange thing is that my JARs which use as dependency are not within the JAR generated. How to put them there using Maven?

  • hi @Eduardobentorochajunior responded with an example from maven-assembly-plugin

  • Another alternative is to use the maven-dependency-plugin to copy the files to the desired place.

  • The maven-assembly-plugin works, but it unpacks dependencies and includes the JAR. The process I need to do is simply add the .jar of the dependencies the eclipse does it. That maven-dependency-plugin do it? (I’ll test)

  • use the Maven-dependency-plugin then

  • João Paraná or @Eduardobrj, know how to run the maven-dependency-plugin straight through the eclipse interface instead of using command line to do mvn assembly:assembly?

  • 1

    @Math, I might be talking nonsense, using mvn clean and then mvn install, automatically the it calls the maven-dependency-plugin. For direct use via eclipse interface, Botão direito no projeto > Run > Depois tem as opções mvn and if you need to customize go Run Configurations.

Show 2 more comments

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 be used would be the mvn package

Browser other questions tagged

You are not signed in. Login or sign up in order to post.