-1

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.

amod
  • 3,690
  • 9
  • 43
  • 69
basviccc
  • 35
  • 1
  • 8
  • Are you using Maven or Gradle? Because if you are using Maven and your IDE is set up to use Maven, you just add it in the `pom.xml` and it should pop right up -- though you may need to rebuild your project, depending on particulars – ifly6 Jan 30 '18 at 19:37
  • I am using gradle. I used an online maven to gradle converter to format it to gradle syle. – basviccc Jan 30 '18 at 19:39
  • Maybe this question/ answer on SO helps. (https://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file) – Stefan Freitag Jan 30 '18 at 19:48
  • @StefanFreitag my source is not jar, it looks like most answers use that. – basviccc Jan 30 '18 at 19:53
  • @basviccc Using 3rd party jars is the default. Having only the 3rd party source leads to exactly the point you are currently at (I guess) - you have to compile the code and also have to resolve all the required dependencies. Maybe it is worth to add info on the github repo to your question – Stefan Freitag Jan 30 '18 at 20:02
  • improved formatting and added details to title. – amod Jan 31 '18 at 20:59

2 Answers2

0

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

Gerrit Sedlaczek
  • 1,040
  • 1
  • 11
  • 29
0

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.

Manmohan_singh
  • 1,676
  • 3
  • 19
  • 27
  • Just found out that the source that I have, that it itself depends on other code. How do I then also integrate that? Can I make them both jars and store them in my repository, and will it work the same? (Technically because it's only a pointing problem right? – basviccc Jan 31 '18 at 10:00
  • You have 2 options. First option is to build the last project in chain. Then add it as a dependency. Second option is to build all dependent project jar individually and add them as dependency. You have to figure out the optimal solution according to your project. – Manmohan_singh Jan 31 '18 at 10:03
  • I was reading to the code, and it seems like the order of what gets built first. Do you know what that means? What's option 2? – basviccc Jan 31 '18 at 10:05