How to include reference to library in java gradle project - java

I am coming from a C# background. I am used to NuGet and Visual Studio project references so the Java ecosystem has confused me quite a bit.
I have a gradle library project. I want to import org.apache.commons.codec.binary.Base64;
However I keep getting cannot resolve errors.
I am using VSCode as my IDE and I would like to include the codec dependancy. How would I achieve this in VSCode/gradle.
I have downloaded the commons-codec-1.14.jar file, but don't know where to put it in the project.

Gradle is a tool that, among other things, manages your dependencies. This means that, you do not need to manually download and add dependencies to your project. Gradle solves this for you.
See the official documenation on how to handle dependencies with Gradle.
You probably have a build.gradle file, in which you need to include your dependency. It would look something like:
dependencies {
implementation 'commons-codec:commons-codec:1.14'
}
This lets Gradle know that you have a dependency to version 1.14 of commons-codec which your codes need to build and run.
This will automatically be downloaded from a remote repository, which you also can specify in your build.gradle file:
repositories {
mavenCentral()
}
This tells gradle to download the dependencies from Maven Central, which probably is the most typical Maven/Gradle repository and most likely hosts most dependencies you would need.

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.

How to specify dependency in a JAR library

I am creating a JAR library and publishing it to nexus using Gradle for other projects to use. How can I specify that my JAR has a dependency on another library (Commons Lang 3)?
I strongly recommend you follow the guide on building a Java library with Gradle. It contains the information you are looking for.
In short, your build file needs something like:
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.7'
}
And the publication to a maven repository will take care of adding that information to the Maven pom file that will be used by consumers of your library.

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.

Install Library and Its Dependencies From Maven Repository Into Internal Repository

Question
I have an internal maven repository in a shared folder say s:\mvnrepo – it's just a convention for a directory layout after all. Now I want to install a library com.example.lib:lib:1.0.0 and its dependencies from another external maven repository into s:\mvnrepo with the hypothetical command
mvn installlib com.example.lib:lib:1.0.0 -Dinto=file:///s:/mvnrepo
That would be a dream! Is such a thing possible?
(I don't have high hopes seeing as everything maven related is always so unapproachable and complicated...)
Supplemental Information
Now, I know there are maven repository management systems like Nexus but I really do think my use case should not require them. I also know about the deploy-file goal but it doesn't install transitive dependencies into the specified repository.
I also thought about simply creating a dummy project that has the specific library listed in its pom.xml. Then just execute mvn install -Dinto=file:///s:/mvnrepo. The problem is that the install goal apparently does not have an option to specify the repository directory (i.e. -Dinto is purely hypothetical).
I found out that it is possible to download a library and all its dependencies with Intellij. That's great but Intellij really only downloads the jars and does not create a maven compatible directory structure (i.e. there are no poms downloaded nor are the jars in a group subfolder etc.).
Background information
For work we want to have an internal maven repository such that it is possible to build an application on a freshly installed server that does not have an internet connection while still being able to specifiy dependencies in the gradle file as if we would use an external maven repository like jcenter (resp. bintray). So the gradle file we use would look something like this:
repositories {
// jcenter() // we don't want that
mavenUrl 'file:///s:/mvnrepo'
}
dependencies {
compile 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
}
Now, I downloaded the sdkmanager plugin jar and its pom file from mvnrepository.com and used something like
mvn deploy:deploy-file -Dfile=sdkmanager.jar -Dpom=sdkmanager.pom -Durl=file:///s:/mvnrepo
All fine and dandy, the sdkmanager plugin is installed into s:\mvnrepo with the correct directory layout and gradle even picks it up! But gradle is not quite satisfied, you see. It demands the jarchivelib which is a dependency of com.jakewharton.sdkmanager:gradle-plugin:0.12.0 (see the page on mvnrepository.com where jarchivelib really is listed as a dependency of the sdkmanager plugin). Now, I could repeat the fun and download jarchivelib and its pom from mvnrepository.com, deploy it and so on. Thinking about it... that does not sound like fun at all! So now you see my concrete use case and maybe you could suggest an even better approach than what I seek for in my question.

What are the options of joining the external library in Android using Gradle?

For example: there is a project on GitHub https://github.com/chrisbanes/ActionBar-PullToRefresh, it uses Gradle, so you can add to a project using:
compile 'com.github.chrisbanes.actionbarpulltorefresh: library: +'
And there is a project https://github.com/ahorn/android-rss, where Gradle is not used.
What are the ways to connect using Gradle this library to my project?
This need not to store external libraries in my git repository.
If you have a project that is not a simple Java project and is not made for gradle Im afraid you have to download the source convert an eclipse adt project manually/automatically to gradle:
You cann import it as a module through Android Studio (New-> Module -> Import Existing Project) see http://developer.android.com/sdk/installing/migrate.html
You could also just write the build files yourself teaching you a little gradle on the way, just look at the examples and docs how to do it
This project doesn't use gradle but uses maven so dependency to this project can also be handled. You just need to find repository with public access where this project is deployed and add appropriate address in repositories section in build.gradle file. If there's no such repository You can download the project and install it in the local repository - the downside is that no other developer that works with your project can download this dependency until You make Your repo public.
Furthermore the fact that some project doesn't use gradle or maven doesn't mean that dependency to this project can't be handled with gradle. If this project has fixed versioning scheme and is accessible over the net gradle can be configured to use such dependency. Gradle can deal with multiple types of repositories (e.g. flat files).

Categories

Resources