Generate Runnable Jar from a Maven project

3

I'm having trouble generating a Jar file for my application from Maven, I followed the following tutorial.

Mkyoung.com

It even generates the Jar but with a super reduced size and at the time of executing by the CMD it presents the following message.

My Pom.xml Build is as follows:

<build>
    <finalName>SisAcademia</finalName>
    <plugins>

      <!-- download source code in Eclipse, best practice -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <downloadSources>true</downloadSources>
            <downloadJavadocs>false</downloadJavadocs>
        </configuration>
      </plugin>

      <!-- Set a compiler level -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
      </plugin>

      <!-- Make this jar executable -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
           <archive>
             <manifest>
            <mainClass>br.com.tamarozzi.app.Main</mainClass>
             </manifest>
           </archive>
        </configuration>
      </plugin>

    </plugins>

Session of the Pom file where the dependencies are:

<dependencies>
    <!-- JUnit testing framework -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <!-- Outros -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.8.2.RELEASE</version>
    </dependency>       

    <!-- Hibernate resources -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- Apache -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>20030825.184428</version>
    </dependency>

    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>20030825.183949</version>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!-- Tiles Apache -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${tiles.version}</version>
    </dependency>

    <!-- Miglayout -->
    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout</artifactId>
        <version>${miglayout.version}</version>
    </dependency>   

    <!-- JDataChooser -->
    <dependency>
        <groupId>com.toedter</groupId>
        <artifactId>jcalendar</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- SwingX -->
    <dependency>
        <groupId>org.swinglabs</groupId>
        <artifactId>swingx</artifactId>
        <version>1.6.1</version>
    </dependency>

    <!-- Spring Security -->    
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
</dependencies>
    
asked by anonymous 08.09.2015 / 15:22

1 answer

1

Based on your own tutorial , Jar generated by Maven will not include dependencies.

The solution proposed by the tutorial is to use the One-Jar plugin to generate a Uber Jar including your project classes and all dependencies:

<!-- Includes the runtime dependencies -->
<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
      <execution>
        <goals>
            <goal>one-jar</goal>
        </goals>
      </execution>
    </executions>
</plugin>

<!-- One-Jar is in the googlecode repository -->
<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories> 

Running the command:

mvn package

You will get a second jar with all dependencies.

To run it use:

java -jar SisAcademia.one-jar.jar

Alternatively you can also use the Maven Shade Plugin .

Q.: I'm not much of Uber Jars fan, I'd rather copy the dependencies to a lib folder and adjust the class path accordingly. See this SOEn response for a cake recipe using Maven Assembly Plugin and the Maven JAR Plugin .

    
08.09.2015 / 16:57