How to generate an executable .jar using Maven?

14

Is it necessary to do some extra configuration to generate an executable .jar by Maven? As I've never used this technology in development desktop I'm a bit lost.

When I build the project (the logs show that I built it successfully) and try to run it, nothing happens. To see if any exceptions or something similar were being thrown, I tried to execute them by command line and here's the message:

  

Failed to find or load main class

Mypom.xmlfilelookslikethis:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>kitty.project</groupId> <artifactId>Kitty</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <name>Kitty</name> <dependencies> <!-- Dependências do projeto --> </dependencies> </project>

Is there any configuration missing? How can I fix this and generate a .jar executable as if it were a normal Java application?

    
asked by anonymous 11.06.2015 / 23:20

3 answers

17

Yes, you are missing out on specifying your main class. For this you can use this plugin:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>sua.classe.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
    
11.06.2015 / 23:32
10

A Afonso's answer worked as I expected and generated a .jar that runs my application.

Until then I did not know there were any plugins for Maven, and by searching a little more I found Maven Assembly Plugin which (in addition to doing the same thing) still allows you to build a single .jar with all dependencies packaged.

In the case of my application, this one seemed more interesting to me in examples there are some other situations where the plugin might be useful. The settings I used in my pom.xml file were:

<plugins>

   <!-- ... -->

   <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <configuration>
          <archive>
              <manifest>
                  <mainClass>br.com.MinhaClasseMain</mainClass>
              </manifest>
          </archive>
          <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
       </configuration>
       <executions>
          <execution>
              <phase>package</phase>
              <goals>
                 <goal>single</goal>
              </goals>
          </execution>
       </executions>
   </plugin>

   <!-- ... -->

</plugins>
  

Note : You can omit the artifact group because Maven has org.apache.maven.plugins as the default group. And in this case, it's the same group as plugin .

    
12.06.2015 / 18:27
6
Renan also does not know such technology either, but about Java (not Maven) if I am not mistaken the java command looks for a .class file, is not it?

To run jar files, you would need the -jar parameter, as in the example:

java -jar Kitty-1.0.jar
    
11.06.2015 / 23:25