JAR added to Gradle project but no entry in build.gradle - java

I am new to the Java tools and stuff, so please be gentle.
I see that someone has added the Simple Logging Façade 4 Java (SLF4J) logging capability to a project I am looking at in IntelliJ IDEA. The project is a Gradle project and when I see the Project Structure -> Module -> Dependencies, I do see a dependency for the SLF4 jars.
I also see these listed in the External Libraries node in the treeview in the Project window.
However, when I open the build.gradle file, I see no entry for slfj. How is that? How would this library have been added to the project?

slf4j is a transitive dependency of some other library.
Use ./gradlew dependencies on the root folder of your project to see the dependencies graph.

slf4j ist most likely a transitive dependency of one of the declared dependencies.
To list the dependency tree, you can use gradlew dependencies.
To list the dependency tree for a specific configuration, you can use gradlew dependencies --configuration runtime.
In your situation you can also use the other way around and use dependencyInsight task instead like gradlew dependencyInsight --configuration runtime --dependency org.slf4j:slf4j-api to see which declared dependencies depen on the given dependency in the given configuration.

Related

Publish jar to maven local and use this dependency in root project

Update
I would like to use external library that is not available on maven repository.
Currently, I have subproject called libs and within CLIPSJNI.jar
In Gradle.build of root project I declare dependency like this
dependencies {
implementation files('/libs/library.jar')
}
but in order to run java application I also have to add file with .jnilib.
My question is it possible to publish to maven local this jar and reuse this dependency. So that .jnilib is redundant.
Initial question was resolve. If you wanna use CLIPSJNI in java application there are only 2 steps you should follow, which are described in comments down below.

How version of lib defined in classpath?

i have hirarhy of modules.
I set up version of hibernate-validator as 6.1.5.Final
But when i build project version of library another
maven dependencies: tree ouput
org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
I can not understand how it works.
I put all dependency tree here https://paste2.org/CwB2H4W2
Problems with dependencies should always bring you to the "Dependency Hierarchy" Tab of your POM.xml. There you will see your projects dependencies, and dependencies of that dependencies.
If I had to guess I'd say there you will find a module providing the dependency you think you don't use.
Further information to maybe change this:
First declared dependencies get used first. So if you define your dependency before the Module that brings the other dependency in, Maven should select yours.

Gradle Plugin dependency

What is the exact dependency I need to develop a Gradle Plugin in Java? Ideally I would like to get it from a well-known repository such as Maven Central or similar.
I have a Maven project with a core functionality and I just added two extra plugins, one for Ant, one for Maven. They are already tested and working; easy! Now, I wanted to add a third module for a Gradle plugin to make this functionality also available from any Gradle project.
However, I can't find the exact dependencies I need to develop a Gradle plugin.
The Gradle docs (such as https://docs.gradle.org/current/userguide/java_gradle_plugin.html) are not very well written to say the least. They mention:
the gradleAPI() dependency
or the java-gradle-plugin dependency
But they are quite unclear... no group, no version (really?).
If anyone can enlighten me to where I can get these dependencies from, I would be very thankful.
Gradle's public and internal APIs, aka gradleApi(), are bundled with the Gradle distribution and not independently published and therefore not easily consumable by Maven builds. There's the pending epic #1156 (Ensure plugin cross-version compatibility by allowing a user to depend on gradlePublicApi()) that might help here.
Since Gradle plugins are best to be built with Gradle, a pragmatic solution is to invoke the Gradle build from Maven and attach the produced artifact to the Maven build. Andres Almiray (aalmiray) once described this in the blog post Running Gradle Inside Maven (Web Archive Link). He describes the following high level steps:
Create a new Maven module (e.g. gradle-plugin) and add attach it to the parent POM
In the POM of gradle-plugin add a dependency to your core module. Use the maven-dependency-plugin to store dependencies to the Maven build folder, e.g. target/dependencies.
Create the build.gradle, add a Maven repository that points to target/dependencies (step 2) and let it depend on the core module as well as gradleApi(). Implement the Gradle plugin.
Use the exec-maven-plugin to invoke the Gradle build.
Use the maven-resources-plugin to copy the Gradle built plugin jars to the standard Maven build folder.
Use the build-helper-maven-plugin to attach the copied jars to the Maven build.
Sample project to be found here (gradle-in-maven).
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
In here it is mentioned that it is gradleApi() and I know that this works (from experience). The localGroovy() on that page is only needed if your plugin code uses groovy (does not apply if you only use groovy in the build.gradle of your plugin).
java-gradle-plugin is a library that makes it a bit simpler to make plugins, it is not required though. I personally prefer using gradleApi only.
EDIT:
It appears I've misunderstood the question. Here are the steps to get gradleApi jar:
Create a Gradle project with your desired Gradle version.
Add implementation gradleApi() dependency.
Import/run the project once.
Go to your .gradle folder (located in home folder in Linux-based operating systems).
Open caches folder
Open the version folder you want, e.g. 6.0.1
Open generated-gradle-jars folder.
Copy the jar to wherever you want and use it.
For me the 6.0.1 jar is at ~/.gradle/caches/6.0.1/generated-gradle-jars/gradle-api-6.0.1.jar
Please note that I have not tested this, I know the jar is there but I haven't tried using it.

How to find if I need to exclude dependencies in a maven java project?

I use both Intellij IDEA (2018.3.5) & Eclipse IDEs, but I prefer Intellij. I have a maven based Java project with multiple poms. I added some dependencies to one of the pom files. I need to find out if there are any dependency conflicts which could prevent the build from running when its deployed, and then exclude them. I tried the steps given below to find conflicts which could cause problems. Are they enough or do I need to do more ?
Check if there are any compile time dependency conflicts with mvn clean install -DskipTests. Build was successful with no errors.
Check if Intellij shows no problems under File > Project Structure > Problems. There are no problems.
I also saw the dependency tree with mvn dependency:tree -Dverbose. It has a lot of "omitted for duplicate" and "omitted for conflict with" items, but the build was successful. I don't see any errors though. Does this mean that everything is okay or do I have to do something more about these conflicts ?
The best way to tell if everything is fine with your application is to have good tests.
However normally one doesn't exclude transitive dependencies from project's <dependency> libraries. Doing it can potentially break the dependency in a subtle and hard to notice way. It's usually safer to remove the whole <dependency>.
There are few scenario when one should use <exclude>:
Dealing with incompatible transitive dependencies between different libraries e.g. A requires library C-1.0 but library B requires library C-2.0 while C-1.0 and C-2.0 can't coexist on the classpath.
Having transitive dependencies already provided by system e.g. deploying to Tomcat with additional JARs in the TOMCAT_HOME/lib directory.
If you decide to exclude a dependency it's important that you check the final artifact because sometimes plugins do weird things e.g. there were versions of maven-assembly-plugin affected by a bug that resulted in different dependencies being resolved during shaded JAR creation than maven-dependency-plugin used for compilation.

How to bring all dependencies jars with Gradle?

I have simple java (Gradle) project wich depends on several JARs, obtained automatically from Maven repositories.
Now I wish to call my jar with external application (Matlab). Unfortunately, application does not see any dependencies after I call build, jar and similar goals.
Me myself also don't know dependencies, since they are managed automatically with Gradle and may contain nested dependencies, not listed in build.gradle.
Is it possible to collect all required JARs in one place to run them freely?
You can write a custom task to collect your runtime dependencies into a jar
task copyToLib(type: Copy) {
into "$buildDir/libs"
from configurations.runtime
}
You could also look into the application and distribution gradle plugins, that provide similar packaging and distribution functionality

Categories