0

I have a very simple spring boot project and I use a 3rd party jar named jsoup.

When I run the project from Eclipse, the code is working fine. But when I use mvn clean package command and export it to an executable jar; project still works, but the parts that i use jsoup throws an exception that jsoup cannot be found. So my executable jar does not contains jsoup.

I searched and tried some methods but they did not work. If you can help me i will appreciate it.

Here is my pom file,

<modelVersion>4.0.0</modelVersion>

<groupId>com.tools</groupId>
<artifactId>parser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>parser</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <scope>system</scope>
        <version>1.0</version>
        <systemPath>${basedir}\src\lib\jsoup-1.10.1.jar</systemPath>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Batuhan
  • 87
  • 2
  • 9
  • 1
    Better aproach is to add following configuration which will download artifacts by maven when you build it. Is there any specific reason to mention system path? org.jsoup jsoup 1.10.1 – Nagaraddi Dec 16 '16 at 12:52
  • 1
    The issue is because the 3rd party classes are not compiled into your generated JAR. What you want to do is create what is commonly known as a 'Fat JAR' and which has all your classes + all 3rd party classes. There are various mechanisms: see here for starters. https://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/ – Alan Hay Dec 16 '16 at 12:58
  • @AlanHay you might want to check on Spring Boot... That is exactly what spring boot does. – M. Deinum Dec 16 '16 at 13:17
  • You are using `system` and that isn't included in the artifact (maven ignores it just like provided dependencies). You should be using the jsoup version from maven central (like all your other dependencies). – M. Deinum Dec 16 '16 at 13:18
  • I didn't notice jsoup has maven support. I just downloaded the jar and added as dependency. And fat jar also is a solution for my problem. Thanks. – Batuhan Dec 16 '16 at 13:22
  • But when i cannot find a jar in maven central repo, I must use fat jar right? – Batuhan Dec 16 '16 at 13:24
  • Spring Boot already creates that jar... Seems like you missed that point of spring boot... – M. Deinum Dec 16 '16 at 13:25

2 Answers2

1

You are using JSoup version 1.10.1 but use it with <scope>system</scope>. Maven doesn't include those jars in the artifacts build, just like <scope>provided</scope>. See also this Stackoverflow question and answers.

However JSoup is a normal jar like your other dependencies and is in Maven Central. As you can see here. Just add it as a regular dependency.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.1</version>
</dependency>

Instead of what you have now. This will make it be included into your executable jar that Spring Boot will create for you. (And saves you from manually looking for that jar and updates etc.).

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
1

In case when your 3-rd party jar has no Maven support and is not in any Maven repository, from where you can download it you need to make a Maven artifact for it and then deploy it into some Maven repository with Maven deploy plugin.

It can be your local repository, your company repository or any public repository you'd like to use.

please see it at: Guide to deploying 3rd party JARs to remote repository

that is a Maven command format from it:

mvn deploy:deploy-file -DgroupId=<group-id> \
 -DartifactId=<artifact-id> \
 -Dversion=<version> \
 -Dpackaging=<type-of-packaging> \
 -Dfile=<path-to-file> \
 -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
 -Durl=<url-of-the-repository-to-deploy>
Vadim
  • 4,027
  • 2
  • 10
  • 26