3

I have added some Maven dependencies with SystemPath tag:

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

Ok, so following one of the stackoverflow thread Add external library .jar to Spring boot .jar internal /lib

I updated to this

<plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>

But when I generate the .war file, these dependencies are not included in the WEB-INF folder and a separate lib-provided folder is generated but still I get this error java.lang.NoClassDefFoundError.

And due to this, when I deploy these files on the Tomcat server. I get java.lang.NoClassDefFoundError error.

My Spring Boot version <version>2.2.11.RELEASE</version>

Please help out with the correct approach and preferred some example

John
  • 276
  • 1
  • 9
  • 29
  • 1
    SystemPath scoped dependencies are deprecated. Furthermore the documentation of [spring-boot-maven-pugin](https://docs.spring.io/spring-boot/docs/2.4.5/maven-plugin/reference/htmlsingle/#goals-repackage-parameters-details-includeSystemScope) needs to be changed... – khmarbaise Apr 21 '21 at 13:06
  • @khmarbaise can you show me, how to add these dependencies then.... code snippet – John Apr 21 '21 at 13:24
  • 2
    https://stackoverflow.com/questions/19065666/how-to-include-system-dependencies-in-war-built-using-maven – crizzis May 08 '21 at 15:36
  • My experiences with system scope was that it broke in mysterious ways at interesting times and I would recommend that you avoid it completely and simply install the necessary jar instead locally. – Thorbjørn Ravn Andersen May 14 '21 at 09:08
  • @ThorbjørnRavnAndersen Ok thanks for your valuable suggestion, will keep in mind. – John May 14 '21 at 12:45

2 Answers2

1

If you have a company Nexus/Artifactory, put the dependency there.

Otherwise install it into the local repository with mvn install:install-file.

Then you can reference it without using system scope.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
1

As mentioned in comments System scope is deprecated. best approach is use an artifact manager like jFrog artifactory or install your preferred dependency in your local repository {home-dir}\.m2.

To do this you can use Maven install command:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

suppose we have a my.jar file located in project root directory src\lib\my.jar

Then add it as regular dependency to dependencies of my project:

 <dependency>
            <groupId>a.b</groupId>
            <artifactId>my</artifactId>
            <version>9.0.0</version>
 </dependency>

But if you want an automated way to install your jars that are located in src/lib into your local repository can utilize Maven install plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-external-non-maven-jar</id>
            <phase>clean</phase>
            <configuration>
                <repositoryLayout>default</repositoryLayout>
                <groupId>a.b</groupId>
                <artifactId>my</artifactId>
                <version>9.0.0</version>
                <file>${basedir}/src/lib/my.jar</file>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

By executing mvn clean command, my.jar file will be installed in local repository and then by mvn package or mvn install command execution, your war file will be created.

final pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>a.b</groupId>
            <artifactId>my</artifactId>
            <version>9.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-external-non-maven-jar</id>
                        <phase>clean</phase>
                        <configuration>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>a.b</groupId>
                            <artifactId>my</artifactId>
                            <version>9.0.0</version>
                            <file>${basedir}/src/lib/my.jar</file>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Saeed Alizadeh
  • 1,417
  • 13
  • 23