How to specify dependency in a JAR library - java

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.

Related

How to use Google Maven Dependency in android projects?

How should i change build.gradle and other files in android projects to add dependencies offline?
I downloaded Google Maven Dependency from developer.android.com but i don't know how should i use it?
The official documentation covers very useful topics, including the one you're asking about.
dependencies {
// Dependency on a local library module
implementation(project(":mylibrary"))
// Dependency on local binaries
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
}
https://developer.android.com/studio/build/dependencies

dependencies for a library jar using gradle

I am using gradle 6.7 and creating a library project which is compiled to a jar that is placed in my own s3 artifacts repository.
In my project I have dependencies to other artifacts and I use implementation dependency.
The jar is created (not a fat jar) and uploaded to the s3 repository.
When in another project I am using my library by fetching it as implementation dependency I am getting errors NoClassDef for other dependencies I used in my library, which means that no runtime is found for the dependencies I was using in my library.
My question is, whether it is a good idea to create a fat jar? I don't think that other libraries (e.g. springboot and others) are using fat jars, right? however when I use them as dependency other dependencies are found on runtime.
Does it mean that using implementation in my project for other dependencies is not the right way? shall I use something else? Could you please contribute a bit more about what is the right way?
Thank you
Check out the Java Library Plugin for gradle. It exists for this exact situation.
If a dependency of your library needs to be exposed to the consumer of your library, then you would use api instead of implementation. There is a nice section within the plugin documentation here that can be used to help you identify when to declare a dependency as api vs implementation.

How to include reference to library in java gradle project

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.

Gradle Java Library Test Jar Using Maven Plugin

In the past, I've had great success in providing test helpers in a core or common project, and having those classes available to other sub-projects using the following:
Common Project A
task testJar(type: Jar) {
classifier = "tests"
from sourceSets.test.output
}
artifacts { testRuntime testJar }
Dependent Project B
testCompile(project(path: ":project-a", configuration: "testRuntime"))
However, I have the same requirement between two projects that do NOT share a Gradle multi-project hierarchy. To clarify, these are completely separate projects. They are in different repositories, using different build scripts. They are not sub-modules within a parent project.
To futher complex matters, I'm using the Java Library plugin and the Maven plugin to install libraries locally. In other words, I'd love for the second project to include a dependency as such:
testImplementation 'com.mycompany:common-test:1.0.0'
What should the first "common" project look like to a) generate that test jar and b) allow it to be installed to Maven?
Many thanks for all help.
I've found that it's best to put any test utilities in their own separate project rather than building a "test" jar from src/test/java and src/test/resources.
It seems that netflix feel the same as me. Previously they mentioned their test-jar plugin as deprecated in their documentation, now it doesn't even get a mention in the readme

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