Calling third party jar functions inside maven based spring boot application

Viewed 464

I have an external jar file which I created from another project and use it inside a spring boot Rest API application.

I have tried installing, adding manually, importing it but I am unable to access the jar or unable to use it.

For eg. I have a jar called myjar.jar. If it is run on its own :

java -jar myjar.jar one two there

I would get my desired out put.

I want use this jar inside a Spring Boot Rest API application which takes in a json input and sends the input data to this and this jar gives the out put which is again returned in a json format.

But I am unable get this jar imported or be able to use it inside my Spring boot applications. Is there a way to access the functions/methods inside the jar file and call them.

Any ideas/suggestions?

1 Answers

You could put your jar inside your project and use its class and methods from built jar

Put myjar.jar inside project

/src/main/resources/libs-external/myjar.jar

Adding myjar.jar in pom dependencies

<dependency>
    <groupId>myjar</groupId>
    <artifactId>myjar</artifactId>
    <systemPath>${project.basedir}/src/main/resources/libs-external/myjar.jar</systemPath>
    <version>1.0</version>
    <scope>system</scope>
</dependency>

Adding on build to use myjar.jar in spring application when building

    <build>
        <!-- exclude jars in resources to avoid duplicated libs on built classpath   -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/libs-external/*.jar</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- Allow jar external on src/main/resources/**/libs-external/*.jar -->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

packaging springboot app using myjar.jar like dependency

mvn clean install

This way you can use all class inside jar in your springboot rest app

The other way is using parent pom maven and working on modular projects.

Related