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

5
  • 1
    SystemPath scoped dependencies are deprecated. Furthermore the documentation of spring-boot-maven-pugin needs to be changed... Commented Apr 21, 2021 at 13:06
  • @khmarbaise can you show me, how to add these dependencies then.... code snippet Commented Apr 21, 2021 at 13:24
  • 2
    stackoverflow.com/questions/19065666/… Commented May 8, 2021 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. Commented May 14, 2021 at 9:08
  • @ThorbjørnRavnAndersen Ok thanks for your valuable suggestion, will keep in mind. Commented May 14, 2021 at 12:45

2 Answers 2

2
+50

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

Alizadesh, Thanks!!. And If I have multiple system scoped dependencies then, I need multiple configurations tag inside execution.
Yes, because each jar file has its own meta data for installation
This is a very nice way to do it, but I would not expect this to happen during "maven clean". Perhaps early in the build phase instead.
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.

4 Comments

A temporary solution for shared projects is to have the build install the artifact automatically. See stackoverflow.com/q/10802280/53897
@ThorbjørnRavnAndersen You just have to be careful that on the first run of something like mvn clean install, this will not work because the dependencies are resolved before the install-file is run.
It's quite a while ago,but I don't recall that being a problem.
Dependency resolution happens before any plugin is run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.