2

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.

Rookie007
  • 1,229
  • 2
  • 18
  • 50
  • if I export it as `fatJar` its working. – Rookie007 Aug 22 '22 at 03:04
  • That sounds like the dependencies of the library are not retrieved. Are you distributing the library as a simple jar file, or as a maven artifact with pom.xml in a repository? – slindenau Aug 22 '22 at 07:14
  • @slindenau please see my question, I added library`build.gradle` – Rookie007 Aug 22 '22 at 12:52
  • And how do you add this library to your project that is using it? – slindenau Aug 22 '22 at 13:32
  • @slindenau `implementation 'com.dexter:streaming-client:0.0.1-SNAPSHOT'` like this – Rookie007 Aug 22 '22 at 14:05
  • @slindenau weird part is I am able to connect to streaming server using this library and able to publish as well, but just this class `StreamingServerProperties` is not able to grab super class methods and its members. – Rookie007 Aug 22 '22 at 14:06
  • So only an issue in the IDE? Maybe it's not synced with the gradle dependencies? – slindenau Aug 22 '22 at 14:11
  • @slindenau I am not sure if its IDE issue, or its actually not able to export those classes. how do we try without IDE involvement ? – Rookie007 Aug 22 '22 at 14:21
  • I'm no gradle expert sadly, but if the code is executing without problems it looks like the library is loaded just fine. Did you try something like this? https://stackoverflow.com/questions/32652738/how-can-i-force-update-all-the-snapshot-gradle-dependencies-in-intellij – slindenau Aug 22 '22 at 14:35
  • I tried that too, it didnt work either. – Rookie007 Aug 22 '22 at 16:39
  • I am able to solve it by making dependencies in `streaming-server-client` as `api ` instead of `implementation` – Rookie007 Aug 24 '22 at 03:06
  • Great, if you want you can write up your solution and [answer your own question](https://stackoverflow.com/help/self-answer). Try to explain why it works with these settings. – slindenau Aug 24 '22 at 07:02

2 Answers2

0

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>
0

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.

Rookie007
  • 1,229
  • 2
  • 18
  • 50