Home > Back-end >  Extended class method are not available when exported as library
Extended class method are not available when exported as library

Time:08-25

I am trying to build a java library (streaming-client.jar) and use it in client microservice.

Within this jar file I have one POJO class which is extending third party POJO classs

 @ConditionalOnClass({Options.class})
 @ConfigurationProperties(prefix = "nats.spring")
 public class ServerProps extends NatsProperties {
 
   public ServerProps(){ super(); }
  }

Now this NatsProperties class is coming from third party library and this class extends another autoconfiguration class.

When I use ServerProps locally within this libary I can access the methods from NatsProperties super class.

But When I export this library as jar file. and use it in client application.

NatsProperties methods are not available via ServerProps class

At client

  ServerProps props = new ServerProps();
  props.server("sdfsfsdfsdf")//server method cant be resolved, 

EDIT

I tried adding all the methods from NatsProperties class to my custom Class StreamingServerProperties

then when I tried to access them in other project like below

   StreamingServerProperties props = new StreamingServerProperties();
   props.server("serverUrl") //Intellij complains that Cannot access io.nats.spring.boot.autoconfigure.NatsProperties

build.gradle of streaming-client.jar

  plugins {
    id 'java-library'
    id 'maven-publish'
    id "com.github.johnrengelman.shadow" version "7.1.2"
  }

 group = 'com.dexter'
 version = '0.0.1-SNAPSHOT'
 sourceCompatibility = '11'

 repositories {
    mavenCentral()

  }

publishing {
     publications {
          mavenJava(MavenPublication) {
                from components.java
                versionMapping {
                    usage('java-api') {
                        fromResolutionOf('runtimeClasspath')
                    }
               usage('java-runtime') {
                   fromResolutionResult()
               }
            }
         }
     }
   }
  tasks.named('compileJava') {
        inputs.files(tasks.named('processResources'))
  }
 dependencies {
     implementation 'io.nats:nats-spring:0.5.6'
     testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.0'
     testImplementation 'org.springframework.boot:spring-boot-starter-test:2.6.3'
     testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
     testImplementation 'org.junit.platform:junit-platform-engine:1.9.0'
        testImplementation 'org.junit.platform:junit-platform-launcher:1.8.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    implementation('np.com.madanpokharel.embed:nats-embedded:2.1.0')
   }

  tasks.named('test') {
   useJUnitPlatform()
 }

I am using ./gradlew build task which generates a jar and with this jar I am not able to get the NatsProperties class methods.

But if I use fatJar task from this shadow plugin I am able to get the methods but thing is its creating all of the libraries and size is too big.

CodePudding user response:

Can you add this dependency so intellij will generate the configuration metadata for you in target

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

CodePudding user response:

I was able to solve it by adding dependency as an api

instead of

   implementation 'io.nats:nats-sprin:0.5.6' // this adds to runtimeclass path

I tried this

   api 'io.nats:nats-sprin:0.5.6' //this adds to both compile and runtime

As I am using java-library gradle plugin.

  • Related