简体   繁体   中英

Spring Boot project jar as dependency in another project

**New to gradle

I have a requirement to use an existing spring boot project (It is installed in local maven repository) as a dependency in another project (Spring boot gradle project).

Using mavenLocal i am able to successfully run and build gradle project, but when i try to use any Class that is present in other project then i am unable to import it.

How can i use maven project classed in this gradle project?

<?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>tech.thegamedefault</groupId>
    <artifactId>demo-lib</artifactId>
    <version>0.0.1</version>
    <name>demo-lib</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

demo-main build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'tech.thegamedefault'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'


configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'tech.thegamedefault:demo-lib:0.0.1'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

在此处输入图像描述

在此处输入图像描述

**** UPDATED ****

End goal is to able to autowire the class that is present in the demo-lib project by using @Component scan.

Note: If i create a fat spring boot jar and import it in another maven project than i am able to do that.

package tech.thegamedefault.demomain;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import tech.thegamedefault.demolib.utility.SuperUtility;

@SpringBootApplication
@ComponentScan("tech.thegamedefault.utility")
public class DemoMainApplication {

    @Autowired
    SuperUtility superUtility;

    @PostConstruct
    public void callSomethingSuper() {
        System.out.println(superUtility.getSomethingSuper());
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoMainApplication.class, args);
    }

}

Facing below error while using default maven build plugin在此处输入图像描述

*********** UPDATED **********

My bad package in Component scan was wrong.

Its working fine with @ComponentScan("tech.thegamedefault.demolib")

Since you are using spring-boot-maven-plugin to build the runnable jar of demo-lib, its packaging the classes slightly different than a normal jar. Your classes are inside BOOT-INF/classes.. not at the root.

You can simply remove the spring-boot-maven-plugin and rely on the default plugin to create the jar and everything should be fine.

Other solution would be to use profiles to optionally use the spring-boot-maven-plugin. eg: by default it would produce the runnable jar. And use a custom profile to skip it.

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM