Adding dependecy on module in gradle Java - java

I have a microservice that is Customer and also I have a common module that is called common. I have been trying to use a file that is called ResponseModel from common module in Customer service.
It says that "add dependency on module 'common main'".
Reference is not added even though I click the option.
I am a beginner in Java and using Intellij Idea.
Can anyone help me?

The problem has been solved by removing .idea folder and then rebuilded the project.
Thank you so much, #FloFromYet

Take a look here:
https://docs.gradle.org/current/userguide/declaring_dependencies_between_subprojects.html
So you can add manually, the common dependency in the customer sub-project.
To do this, you need to adapt the build.gradle file of customer:
dependencies {
implementation project(':common')
}
Do not also forgetto reload your project like described here:
https://www.jetbrains.com/help/idea/work-with-gradle-projects.html#gradle_refresh_project

Related

How to choose which library to use in a class (maven)?

I'm started working on an already existing project. In this project there is some JSON-parsing happening with the following exception being thrown by several methods:
JSONException.class
While I was unit-testing these parsers I couldn't import the right org.json.JSONException library.
The maven library used in the codebase was (package org.json.JSONException):
org.json:json:20160810
And the one that was importing in my tests was (package org.json.JSONException):
com.vaadin.external.google:android-json:0.0.20131108.vaadin1
I think the problem lies in both libraries share the same package names. When the test is execute the JSONException is thrown but the test still fails because its probably the other library. Anybody knows why this problem is happening and how to solve it?
Thanks in advance!
I believe you need to updated your pom.xml file and in the dependency section you can exclude a 3rd party dependency from a declared dependency. I guess in your case there is a dependency to json and vaadin, you'll find vaadin is also pulling the different version of json.
You can try removing one of them. As suggested here:
https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/issues/142

Gradle - one project, multiple packages, multiple jars

I'm asking you about a very basic question but I hope you can find the time to help me:
I'm trying to realise a java-project, that can spit out several different programs which partially have dependencies on other projects of mine.
In order to keep it simple, I want to have all the code in one project, run by Gradle, so if I make changes to a central library (the database connector for example) all the child-programs automatically recieve the changes.
An example could look like this:
project:
program_A
central_library
program_B
output:
program_A.jar (including central library)
program_B.jar (including central library)
Now I'm having serious troubles finding a correct buildscript for this and was wondering if someone here could help me out.
P.S. : Since I'm new to this, if I should realize this through different modules within the Gradleproject instead of different packages in the Gradleprojects sourcefile, feel free to tell me :)
One way to approach this is to have a root project, that holds the three other projects inside of it.
Specify the sub-projects inside its settings.gradle file:
rootProject.name = 'RootProject'
include 'program_A'
include 'central_library'
include 'program_B'
With this in place, program_a can depend on central_library by adding a dependency in its build.gradle:
dependencies {
compile project(':central_library')
}
I have a similar setup in one of my projects, although the "central library" is the root project and the submodules are test environments.
Create a root directory and put each library or program into its own sub-directory.
Create a gradle project in each subproject.
You can for example create a skeleton gradle project by running
gradle init --type=java-library
or
gradle init --type=java-application
Then in the root directory create a gradle multi-module project. Basically
run only
gradle init
and then create a settings.gradle and list all sub-projects there.
This is actually described very well in the gradle documentation:
https://guides.gradle.org/creating-multi-project-builds/
If I understand correctly, what you want to do is, when you change your local projects, you want other projects to see those details. For this you need to publish your projects to some kind of repo, like maven repo. You can do this from command line gradle publishToMavenLocal, or gradle build pTMl. You can also do this in build.gradle file with something like the following:
task sourceJar (type : Jar) {
classifier = constants.extSources
from sourceSets.main.allSource
}
publications {
mavenJava(MavenPublication) {
from components.java
artifact(sourceJar) {
classifier "sources" //classifier = constants.extSources
}
}
}

How can I refrence a method from one library's dependency library in Android Studio?

Assuming that I build a LibraryA and compile it into a LibA.jar, then I create a LibraryB and put the generated LibA.jar into LibraryB/libs, and compile it into a LibB.jar.
The problem is, as I put LibB.jar into a new Android App Module, I can not directly reference methods in LibA.jar. Looks like I can only reference methods in LibB.jar, but the LibB.jar is compiled with a LibA dependency. Is there any way I can walk through this problem?
Assuming that you have all source code access to LibraryA and LibraryB, you can simply solve it by making LibraryA as a seperated module for LibraryB. So when you want to include LibraryB in a project, you must include LibraryA too.
If LibraryA is hidden in LibraryB for external use, you can make a public method where you can get the LibraryA from LibraryB, something like getLibraryA().methodA1().
You can use .aar to wrap the library. The format of .aar allows to "embed" dependencies inside. There is a Gradle lib on GitHub that helps with this. If you use Maven repos, the POM file for LibraryB can describe its dependency LibraryA.

Drool Workbench : Not display all classes for Import in Guided Rule Config Tab

Right now i am trying to do some POC with JBoss Drool Workbench.
I am facing one Problem: I have uploaded on JAR file as a dependency. which contains Bean and some of the business logic service class. which also contains class which have list of static method which need to call from Rule.
My Problem is i am able to get all beans and constant classes in Guided Rule UI in Config tab for Imports. but other then all remaining class, like class contains static method are not available for import. Due to that i am not able to import that class, and not able to use that one.
Please help me out.
Genius Thanks in Advance.
After seen warning i came to know that i also need to upload dependent jar file in workbench and also provide dependency to project, drool itself provide a hint for same. finally the problem has been resolved.
In the project setting tab, you have to add dependency of the maven project containing POJO. It will give you option to either load from central or load from local repository. If you choose, central, the pom.xml will contain the reference to download the dependency while createing the jar. If you choose from local, it will add the class files to your jar.

Gradle Transitive Dependencies from Modules

Disclaimer:
I'm new to Gradle, have read a lot of docs, and I don't know whether my maven-style understanding is tripping me out, or whether it's the sleep dep (kids - don't ask), but I'm still not getting it.
Problem Background:
I have a project that consists of several modules.
One of the modules, let's call it data-structure defines a data structure
Another module, data-structure-fabsearch, defines an implementation for a data source for the data structure, and finally
A third module, fabsearch-common, defines some common data source classes (eg: connection management to a fabsearch data source etc).
The reason I've done it like this is because there's actually another module that also uses the fabsearch-common stuff.
Anyway, my data-structure-fabsearch build.gradle looks something like this:
dependencies {
compile project(:data-structure)
compile project(:fabsearch-common)
}
The fabsearch-common module declares depedencies for the fabsearch api (let's call it fabsearch-api-1.0.0).
So, the dependency tree for data-structure-fabsearch should look like this:
- data-structure-fabsearch
- data-structure
- fabsearch-common
- fabsearch-api-1.0.0
This was all working wonderfully last night. This morning I came to work and all of a sudden those dependencies don't resolve anymore. References to fabsearch-api-1.0.0 classes are no longer found.
What I've Tried
1. In the parent build.gradle:
project(':data-structure-fabsearch'){
apply plugin: 'java'
dependencies {
compile project(path: ':data-structure', configuration: 'compile')
compile project(path: ':fabsearch-common', configuration: 'compile')
}
}
I've tried this with and without the configuration setting.
2. In the data-structure-fabsearch build.gradle file, adding the configuration parameter.
3. Restarting IntelliJ
4. Clicking the refresh icon in the Gradle tool window (repeatedly)
5. Reading all about transitive dependencies in the Gradle user guides
6. Drinking tea (repeatedly)
None of the above work.
What I'm Expecting
I'm expecting that the fabsearch-common dependencies (the fabsearch-api jars) should also be included in the data-structure-fabsearch dependency tree. All references to fabsearch-api classes in data-structure-fabsearch should resolve etc etc.
My Question[s]
Whilst this is possible in Maven, is it possible in Gradle?
What do I have to do to get it to work?
How much sleep dep can you take without dying?
Many thanks for any help.
Turns out the problem wasn't gradle at all. The problem was IntelliJ.
It got it's knickers into a proper twist!
Solution:
1. Close the project in IntelliJ
2. Delete the .idea directory
3. Delete all .iml files and any other IntelliJ cra-useful files
4. Open project in IntelliJ, choose same directory. Problem disappears.

Categories