Generate Runnable Jar from a Maven project

Asked

Viewed 1,585 times

3

I am having difficulty generating a Jar file from my application from Maven, I followed the following tutorial.

Mkyoung.com

It comes to generate the Jar but with a super reduced size and at the time of running by CMD it presents the following message. inserir a descrição da imagem aqui

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>

Pom file session 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>
  • How are the scopes, "Scope", of your dependencies defined? I say that probably the problem is in them, they must be as "Provided", which in this case would be provided by which executes the project. Post the section of dependencies.

  • Celso, I’m new to Spring ] Hibernate / Maven, I just developed the project following Spring guidelines. If you can detail the problem that is occurring I will be very grateful. I added in the question what you asked me.

  • 1

    See that from the tutorial item 4 that you are following, the same error occurs, that you had. Follow the tutorial item 5 that should solve your problem. As for dependencies, everything seems to be ok.

  • I followed your comment based on the Mkyoung tutorial and the system did not find the main, as I declare the main? the following is the image of the error:http://prntscr.com/8e12z9

1 answer

1


According to your own tutorial, Jar generated by Maven will not include dependencies.

The solution proposed by the tutorial is to use the plugin One-Jar 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 have a second jar with all the dependencies.

To rotate it use:

java -jar SisAcademia.one-jar.jar

Alternatively you can also use the Maven Shade Plugin.


P.S.: I’m not a fan of Uber Jars, I prefer to copy the dependencies to a folder lib and adjust the class path in agreement. See that reply from Soen for a cake recipe using the Maven Assembly Plugin and the Maven JAR Plugin.

  • I followed your comment based on the Mkyoung tutorial and the system did not find the main, as I declare the main? the error image is as follows: http://prntscr.com/8e12z9

  • Anthony, I’m a square mule for this kind of thing, could you tell me what’s the best way to generate Jar already with the dependencies?

  • Assuming the path is correct, your setting to the maven-jar-plugin would be enough (<mainClass>br.com.tamarozzi.app.Main</mainClass>). This creates an entry in META-INF\MANIFEST.MF with the content Main-Class: br.com.tamarozzi.app.Main (you can open the JAR with any zip tool to check).

  • Also note that you are having conflicts between your dependencies. In a first analysis I would guess that they are transitive dependencies of Spring and Hibernate (e.g., one of the libraries is depending on the javassist 3.7 and another of javassist 3.18.1-GA). To solve this problem I would use the GOOD OF THE SPRING and avoid specifying versions manually.

  • i went behind such 'onejar' I took a look that it has some tag to declare the main class, the jar is now finding the main problem is that it does not find the folder Resources, i used some images and even properties file are inside this folder 'src/main/Resources' and the system can not find, follow the image of my tree of directories http://prntscr.com/8e4nhk . How to point the paths of the Resources folder?

  • I think it is worth opening a different question with the structure, code that is trying to open the resources and the Stack trace. By default everything that is in Resources should be copied into the jar (again, you can check with any zip tool)

Show 1 more comment

Browser other questions tagged

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