2

Until now i made runnable jars with Ant and there were no problems with it.
However i now try to mavenize my project and i realy can't figured out how to do runable jar with this tool.
I've read tons of tutorials (also here, on Stackoverflow), helps, advices and... nothing. In my case all of them don't work which probably means i don't understand some basics.
I have such simple project:

my project

This is app, witch use mysql-connector-java-5.1.24-bin.jar (placed in 'lib' dir) to connect to MySQL database.
I want to include this jar into final jar (DBPreformatter.jar). I used assembly and shaded plugins in many configurations, but they NEVER added this jar into DBPreformatter.jar.

This is my pom.xml:

<modelVersion>4.0.0</modelVersion>
<groupId>com.icd4you</groupId>
<artifactId>DBPreformatter</artifactId>
<version>1.0.0</version>
<name>DBPreformatter</name>
<description>DB processing and cleaning tool</description>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>mysql-connector-java-5.1.24-bin</groupId>
        <artifactId>mysql-connector-java-5.1.24-bin</artifactId>
        <version>5.1.24</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/mysql-connector-java-5.1.24-bin.jar</systemPath>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- WHAT SHOULD I USE HERE? -->

    </plugins>
</build>

How to solve this problem?

rainbow
  • 1,161
  • 3
  • 14
  • 29
  • 1
    It's not very maven-like to have JARs included in 'lib' folders. Where has this JAR come from? If you authored it, you can deploy it to a local repository and then reference it normally (i.e. without `systemPath`). If it's a 3rd-party JAR, trying searching for it online - it's maybe in a standard repository already. – Duncan Jones Sep 23 '14 at 11:57
  • In fact, this looks just like it: http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.24 – Duncan Jones Sep 23 '14 at 12:02
  • Connector has been taken from official mysql site. Remember this is old app which i try to adjust to maven projest. Anyway I will fix it. – rainbow Sep 23 '14 at 12:10
  • Sure, I understand. I'm just pointing out that one of the steps you'll need to do when "mavenising" is to try and hunt down dependencies in the standard repositories wherever possible. "Lib" folders are frowned upon in Maven projects. – Duncan Jones Sep 23 '14 at 12:11
  • An almost duplicate is [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/q/574594). But since we had to steer you around the `system` scope issue, I won't close it as a duplicate. – Duncan Jones Sep 23 '14 at 12:36

2 Answers2

4

There is a maven plugin Apache Maven Shade Plugin that will build an uber jar for you

Ion Cojocaru
  • 2,583
  • 15
  • 16
3

Add the Maven Assembly plugin with the descriptor jar-with-dependencies:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.pany.your.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Note that this doesn't add the JAR; instead it unpacks all JARs which are listed as dependencies and adds their content to the resulting JAR (so you'll see all the class files from the MySQL JAR in the result instead of the MySQL JAR itself).

EDIT There is a caveat, though: Maven ignores JARs with scope=system for many operations. See also: How to include external jars in maven jar build process?

If Maven doesn't add the JAR to the output, then you must install all JARs with this scope into your local maven repo ($HOME/.m2/repository) using the mvn install:file-install command. See http://maven.apache.org/plugins/maven-install-plugin/usage.html how to do that.

Note: Installing libraries in your local repo is the preferred way; you should really consider it. For one, the scope=system will no longer confuse you (since many plugins handle them in a special way). Plus you need to do this only once. Afterwards, you can use this library in many Maven projects.

Before installing, you should check http://search.maven.org/ to see if the dependency isn't already known to Maven.

MySQL is: http://search.maven.org/#artifactdetails%7Cmysql%7Cmysql-connector-java%7C5.1.32%7Cjar

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Indeed i expected some jars. Can i steer somehow destination of this unpack? Eg. if i have more than one jar file in /lib directory i would like to unpack them into DBPreformatter.jar/lib. – rainbow Sep 23 '14 at 12:02
  • @rainbow Why do you care how the files appear inside the JAR? This answer describes the normal way of producing an executable JAR. Do you have other requirements you haven't shared with us? – Duncan Jones Sep 23 '14 at 12:03
  • In this project: no. But if i will sucesfully mavenize this one i will try with another which have many additional libs in his 'lib' directory. – rainbow Sep 23 '14 at 12:08
  • @rainbow It still doesn't matter though. Why do you care how those JARs are represented inside the final JAR? It is normal for those to be unpacked, for Maven projects. Is there a problem with that approach for you? – Duncan Jones Sep 23 '14 at 12:10
  • No. I will try this solution in a minute. – rainbow Sep 23 '14 at 12:11
  • 2
    @rainbow: Don't use `scope=system`. See my edits for a better solution. – Aaron Digulla Sep 23 '14 at 12:19
  • @AaronDigulla Note that http://search.maven.org/#artifactdetails|mysql|mysql-connector-java|5.1.24|jar is the correct link to match the version used in the project. – Duncan Jones Sep 23 '14 at 12:20
  • 1
    @Aaron Yes! This is critical remark. Indeed after removing scope=system/using repo from maven repository (just like Duncan mentioned), shade plugin has been embedded mysql-connector properly into DBPreformatter.jar. Uhh, so close but so far... Thanks for everybody. – rainbow Sep 23 '14 at 12:26
  • Where do I need to add these lines? in the pom.xml file inside the build tag? nothing is changed when exporting the jar and no dependencies are added. – CodeMonkey Sep 17 '15 at 15:54
  • @YonatanNir: Yes, it goes inside of ``. – Aaron Digulla Sep 21 '15 at 13:15