How to use the library of other projects in Java with Gradle - java

I'm still fairly new to development and don't know how to solve my problem.
I have a project that needs other code for it to work.
I understand that by adding a path n the import section of my java gives access to a library.
I also understand that by adding a compile dependency to my build.gradle file, tells it where that library is.
My question is two part:
What do I do when it fails to resolve dependency?
I have downloaded the source code off of git hub.
I want to be able to add the source to my project.
Now, I don't want to add it directly to my package, but the way gradle does it. Just by pointing to it.
So my real question, how do I get a library into my code without adding the entire source code into the package?
What would I have to do in my import section of my java file?
P.S.
Thanks in advance.
Since I'm still learning the concepts, my question might not be structured in the best manner.

You could create a jarfile from the those sources:
jar cf program.jar -C path/to/classes .
source: https://stackoverflow.com/a/18146453/7625131
further information: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
After that you just have to reference it using gradle:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile name: 'gson-2.2.4'
}
source: https://stackoverflow.com/a/20700183/7625131

As Mett highlighted. Create a jar of the project that you cloned from github. Add it in the buildpath of your desired project . If you are using a build automation tool like maven or gradle , then install the jar in your local repository. Then use it as a dependency in your project.

Related

Including dependencies in Java project on Intellij/Maven?

This sounds dumb but is there anyway for me to specify dependencies for my Java project like how I would in a package.json file so that someone else who was to download the project code from my GitHub repo, would be able to run it without any errors or missing libraries?
I have never tried using external Java libraries before, such as apache commons. The most I ever used was JavaFX but on a personal project level. My main concern is that if I were to push my code up to the repo and have someone else clone it. It might not run properly as the imported libraries are not downloaded.
Is there something similar to package.json dependencies where the person who runs the code would automatically download all dependency libraries and have it run on their system?
You can use Maven or Gradle for this purpose. Maven has pom.xml where you can specify all your dependencies. Similarly gradle has build.gradle which does the same job.

Gradle:Can I use a flatDir repository to include sources and/or javadocs?

I am using IntelliJ and Gradle.
I'm not exactly experienced with Gradle. I've built JCEF on my computer, and using flatDir to include the actual class jar is easy. However, I would like to include source code for JCEF. I'm currently just going into IntelliJ and including the sources through there. Is there a way to declare my source jar using Gradle?
The solution was to create a local Maven repository to put these libraries.
https://discuss.gradle.org/t/how-to-supply-source-jars-from-flatdir-for-eclipse-debugging/35670

How to include an external non-Android Eclipse library project into a Android-Studio project without copying/moving/editing?

How can i use an external, Ecliplse non-android-library-project in my project, without moving or copying or changing it? It is important that that library remains untouched because it is still being edited by others and other projects are referenced to it there aswell.
The library is in the same parent directory as my android studio project.
I did put these lines into my settings.gradle, without success.
include ':abc'
project(':abc').projectDir = new File(rootProject.projectDir, '../abc ')
This apparently works for library-projects which include gradle, but apparently not for libraries without gradle.
The result is this gradle sync error:
Unable to resolve dependency for ':app#debug/compileClasspath:' Could not resolve project :abc
In the dependencies section in build.gradle of your app module, add below.
implementation project(path:':abc' , configuration: 'default')
And in settings.gradle, only include ':abc' should be enough.
For anyone reading this... my conclusion so far: It is apparently not possible to use such a library (from Eclipse) without modifying those a little bit. I did these changes to get them working so far:
inside the src directory, make the directories main/java/ and move the package with the source codes to there.
write a build.gradle file in the root and define the module dependencies in it.
move the AndroidManifest.xml into src/main/

Implement pwinty API using Java library

I'm trying to get to grips with using pwinty with a site i'm developing and seem to be in way over my head. The library file I (think I) need is: https://github.com/OddPrints/pwinty-java-sdk. I can't seem to find any proper clues on how to go about incorporating this.
Do I need to somehow package the zip into another format? If so, how?
Do I link this to my html using a tag? What then?
Any help would be much appreciated - thanks!
Seems like the author did not publish compiled jars to a central repository like JCenter or maven central.
In order to use it you will have to clone and build the jars from sources.
Should be simple, however, the build needed some tweaks to make it work.
See following steps:
git clone https://github.com/OddPrints/pwinty-java-sdk.git
Edit build.gradle in the newly cloned repo:
https://gist.github.com/galusben/967319bdff5ae6fb0bbc64081a547c47
run './gradlew build -x test'
Make sure you find the sdk jars in your m2: '~/.m2/repository/uk/co/mattburns/pwinty/pwinty-java-sdk/2.3.1'
In your project, if you are using maven simply add the dependency. If you are using gradle you will have to add mavenLocal() to your repositories and add apply plugin: 'maven' gradle example: https://gist.github.com/galusben/e0f71f90da3488ebacc0a46c7412d8fa
Otherwise, just add the jars to your project.

Installing Mockito using Gradle

I have installed Gradle by adding the path to it into the system variables. I am quite new to Java and this is the first time that I am trying to install an external library for it. On the Mockito web-page, they say that one can:
Declare a dependency on “mockito-core” library using your favorite
build system. With Gradle one can do:
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
So I have no idea what it means. I changed the directory in cmd to the Gradle folder and tried to execute these commands, but that is not how one is supposed to do it. Can you give me a hand here?
You have to create a build.gradle file where you can insert the dependency. I recommend using an ide like eclipse or IntelliJ which can generate a gradle project for you so you don't have to do this manually. Just install the corresponding Gradle Plugin. This also makes sure you have a correct project structure.

Categories

Resources