Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven executable jar with libraries on external path

Tags:

maven

jar

My jar is not running, I can tell it tries to run as the log4j file manage to create the log folder but then nothing happens and the log is in blank.

My problem is I have the jar file in a folder called bin and the libraries in a folder called lib

I'm triying this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <outputDirectory>${staging.dir}/bin</outputDirectory>
      <archive>
        <manifest>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          <addClasspath>true</addClasspath>
          <mainClass>com.Main</mainClass>
          <classpathPrefix>../lib/</classpathPrefix>
        </manifest>
      </archive>
    </configuration>
  </plugin>

and

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${staging.dir}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

I also tried with maven-assembly-plugin, but it packs everything on the jar and I really need to have the folders bin and lib

What do I need to setup to make it work correctly?

EDIT: META-INF file

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: me
Build-Jdk: 1.6.0_26
Main-Class: com.Main
Class-Path: ../lib/ojdbc6-11.2.0.jar ../lib/sqljdbc4-4.2.0.jar ../lib/
mysql-connector-java-5.1.17.jar ../lib/hibernate-core-3.6.5.Final.jar
../lib/antlr-2.7.6.jar ../lib/commons-collections-3.1.jar ../lib/dom
4j-1.6.1.jar ../lib/hibernate-commons-annotations-3.2.0.Final.jar ../
lib/hibernate-jpa-2.0-api-1.0.0.Final.jar ../lib/jta-1.1.jar ../lib/s
lf4j-api-1.6.1.jar ../lib/hibernate-entitymanager-3.6.5.Final.jar ../
lib/cglib-2.2.jar ../lib/asm-3.1.jar ../lib/javassist-3.12.0.GA.jar .
./lib/slf4j-log4j12-1.6.1.jar ../lib/log4j-1.2.16.jar ../lib/commons-
codec-1.5.jar ../lib/lablib-checkboxtree-3.3-20110114.141734-3.jar

SOLUTION

turns out the META-INF file is incorrect. The reason is that maven-archiver-plugin renames SNAPSHOT libraries with a timestamp as default behaviour

to override that use this, as instructed by the Maven Archiver doc:

<plugins>
  <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <configuration>
       <archive>
         <manifest>
           <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          <addClasspath>true</addClasspath>
          <useUniqueVersions>false</useUniqueVersions>
          <mainClass>com.Main</mainClass>
          <classpathPrefix>../lib/</classpathPrefix>
         </manifest>
       </archive>
     </configuration>
  </plugin>
</plugins>

other than that, I hope people find useful the maven code at the start because it does work, just beware the SNAPSHOTS in your projects

like image 271
javaNoober Avatar asked Oct 25 '22 04:10

javaNoober


1 Answers

The above all seems OK. Here are some things/questions you may want to try/confirm/answer:

  • Are you running this from command line? I.e. using java -jar <your.jar> or are you starting it e.g. by double-clicking the file, etc.? If not, try running it from the command line to see what happens
  • Try using mvn exec:java to see if that starts your app (maven-)regularly. See http://mojo.codehaus.org/exec-maven-plugin/usage.html if you are not familiar with exec plugin
  • Can you use regular Java System.out.println instead of logging to confirm that it actually starts? Having a zero-size log might be a logging configuration issue
  • I see you have some DB libraries above. Can you put some println (or better logging, but only after you confirm your logging actually works) statements around basic initialization, to confirm that you are not just stalling there (provided that's what's happening - you don't mention any exceptions or other issues in specific)

A lot depends on the actual application code, but hope some of the above might help you pinpoint the issue.

As a side note, is your main class really com.Main? If yes, may I suggest to change that to something more appropriate - e.g. com.yourdomain.yourapp.Main or something along these lines. Not that this will change the above result, just a stylistic comment.

like image 145
icyrock.com Avatar answered Oct 31 '22 11:10

icyrock.com