How to manage external dependencies of a Gradle Plugin - java

Gradle Version: 1.12/2.0 (restricted due to Org policies)
JDK: 1.8
I have created a custom gradle plugin that performs some installation using code that has been defined in another sub-project of our source-code. The build.gradle for the plugin is
dependencies {
compile project(path: ':installlib', configuration: 'libConfig')
compile project(path: ':tools', configuration: 'toolConfig')
}
jar {
from {
// LINE#1 source-code of plugin
sourceSets.main.output
zip64 = true
// LINE#2 dependencies
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
baseName = 'myCustomPlugin'
version = '1.0'
destinationDir = new File(project.libDir)
}
The above plugin is consumed as follows - consumer.gradle
buildscript {
repositories { flatDir name: 'libs', dirs: System.env.CODESOURCE + '/lib/')
dependencies {
classpath: ':myCustomPlugin:1.0'
}
}
apply plugin: 'java'
apply plugin: 'myCustomPlugin'
...
...
//rest of the items of this gradle
Case-A
If I run this, I hit > Plugin with id 'myCustomPlugin' not found.
NOTE: I have META-INF/gradle-plugins/myCustomPlugin.properties created correctly with implementation-class pointing to my plugin code and this works fine if I don't get into creating the fat/uber-jar business and just include the sourceSets.main.output statement in my jar task. But since our entire project depends on file based artifacts, I am attempting to create a fat/uber-jar and that's where I start running into these issues.
Case-B
If I move the LINE#2 above the LINE#1, which looks like -
from {
// LINE#2 dependencies
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
// LINE#1 source-code of plugin
sourceSets.main.output
zip64 = true
}
If I run this, I hit ClassNotFoundException for one of the classes coming from the project 'installlib'. I can see the *.class file in the uber jar created but even then the class loader complains about this class.
Could anyone provide some pointers on how to resolve this ? The uber/fat jar creation (or the way I am doing it) is not helping with the plugin-source-code and dependencies of the plugin.
If not a fat jar, could anyone provide some inputs on how to resolve the dependencies in the buildscript of the consuming gradle ?

Related

Gradle task to create fat runnable jar

I have a java application with Main class which has dependencies to couple of other library jars. I need to create a runnable jar in gradle with all dependant libraries copied to the jar. The gradle plugin "application" or "java" does not provide this. I am using latest gradle version 6.7
I could achieve this by creating a task which does the following things
Update manifest file with the Main-Class attributes
Copy all compile time dependencies to the jar task
plugins {
id 'application'
}
repositories {
mavenCentral()
//jcenter()
}
dependencies {
compile group: 'org.json', name: 'json', version: '20200518'
testImplementation 'junit:junit:4.13'
implementation 'com.google.guava:guava:29.0-jre'
}
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.example.gradle.App'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
} with jar
}
More details can be read in this article A simple java project with Gradle

How to build jar file with gradle with dependencies

I'm building a jar file with Gradle. This jar file is being used as a library in another project. But when the project tries to use the jar file, a ClassNotFoundException is returned.
Caused by: java.lang.NoClassDefFoundError: com/auth0/jwt/JWT
I've included the jwt library in the gradle file building the jar:
compile group: 'com.auth0', name: 'java-jwt', version: '3.4.0'
The project using the jar can't seem to find this jar dependency in the jar.
I built the jar with the gradle command:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Jar File creation',
'Implementation-Version': version,
'Main-Class': 'com.group.me.name.MyJarClass'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
How do I include the missing dependency in the jar?
You, generally, should not pack the dependencies inside a published JAR. It's better to declare them as dependencies in pom.xml and let the user of you JAR fetch them. It's just a good practice that should be enough.
If it's not enough, use Gradle Shadow:
plugins {
id 'com.github.johnrengelman.shadow' version '4.0.2'
}
Fat JAR is produced with shadowJar task in this case. Publishing is easy as well:
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
url "http://your.repo"
}
}
}

Gradle building old version of code

I'm trying to build a fat jar with gradle but every time I do I get a really old version of the program. Running the program from main directly in IntelliJ works fine so it is something with the gradle build itself that is not working. When i check the jar in (project path)/build/libs the date and time of the file has changed so it did indeed build but when i start it i get a month old build. I suspect there might be some cache that is causing this but i do not know where that is located.
build.gradle
version '1.0.2'
apply plugin: 'java'
repositories {
mavenCentral()
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Example',
'Implementation-Version': version,
'Main-Class': 'com.example.Main'
}
baseName = project.name
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
jar {
manifest {
attributes(
'Main-Class': 'com.example.Main',
)
}
}
dependencies {
compile 'com.intellij:forms_rt:6.0.5'
compile project(':common')
testCompile group: 'junit', name: 'junit', version: '4.11'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
settings.gradle
rootProject.name = 'example'
include ':common'
project(':common').projectDir = new File(settingsDir, '../common')
Command
./gradlew fatjar
Well, I discovered the problem and it was a combination of things. The only thing that was actually outdated was the form ui. The reason for this was that IntelliJ started using binary class files for the forms instead of java source files.
To fix it go to settings then editor and after that GUI designer. Press Java source code instead of binary class files. Regenerate the design (might have to delete the generated code and run it again). Then build it with gradle, it should now work.

OpenCV with Java Gradle Project as fat jar

I am actually Trying to use OpenCV in Java based Gradle Project. Since, OpenCV needs native library and Jar File for execution. I am trying to wrap native library and Jar together using gradle, but I am facing errors in doing so.
When I try to run project, project is not able to find native library for opencv jar and giving me below error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java340 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at Library.(Library.java:9)
Although, I know how to set native library manually in Gradle project but I am not sure how to do it via Gradle and wrap native library in fat jar. Here is my build.gradle
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
configurations {
// configuration that holds jars to include in the jar
openCVLibs
}
dependencies {
openCVLibs fileTree(dir: 'libs', include: '*.jar')
openCVLibs fileTree(dir: 'libs', include: '*.so')
configurations.compile.extendsFrom(configurations.openCVLibs)
}
jar {
from {
configurations.openCVLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
}
have also included link of sample eclipse project
So Here is edit
Based on #kerry's comment I tried to crate mvn artifact following openCV Maven, but now I am facing following error while creating mvn build
[ERROR] Failed to execute goal
org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
(set-arch-properties) on project opencv: Properties could not be
loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to
execute goal
org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
(set-arch-properties) on project opencv: Properties could not be
loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties
There is no build.properties file present in build folder. Since build folder is created by maven task only, so build.properties file should be created by maven only.
Following there is a working example of a build.gradle file. Make sure to read the comments and make changes when appropriate.
By running gradle fatJar you can create a working Java Jar of your application with OpenCV inside.
However, apart form including your OpenCV library in your Java Jar, you will need to load the OpenCV native file at the beginning of your code.
They are two ways to do that:
1) Load the file by providing the full path:
System.load("my/full/path/opencv.dll");
2) If your native file is located inside your Java Library Path:
System.loadLibrary("opencv");
Take notice that in the second case you only need to provide the name of your native file (without its extension).
The default Java Library Path depends on OS:
On Windows, it maps to PATH
On Linux, it maps to LD_LIBRARY_PATH
On OS X, it maps to DYLD_LIBRARY_PATH
If you want to set your own Java Library Path:
try {
System.setProperty("java.library.path","YOUR/PATH");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception ex) {
System.out.println("Failed to set Java Library Path: " + ex.getMessage);
}
build.gradle
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = "YourMainClass" // You Main Class name
repositories{
mavenCentral()
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude "build/libs/philipath/**"
}
with jar
}
artifacts {
archives fatJar
}
dependencies {
// Local libraries
compile fileTree('lib') // assuming there is a folder named 'lib' in your project root with your OpenCV jar inside
// other dependencies
}

using Kotlin with Gradle

I'm new to Kotlin and Gradle, and tried to follow these steps, so I got the following 2 files:
after running gradle init I changed the build.gradle to be:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'application'
mainClassName = "hello.main"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Hello.kt:
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
Then I run the gradle build and got the build\classes\main\hello\HelloKt.class
my question is: Why the file generated is .class not .jar and how to get the .jar file and how to run it, I tried running the generated file using kotlin -classpath HelloKt.class main but got an error error: could not find or load main class hello.main
The classes are the direct output of the Kotlin compiler, and they should be packaged into a JAR by Gradle afterwards. To build a JAR, you can run the jar task, just as you would in a Java project:
gradle jar
This task is usually run during gradle build as well, due to the task dependencies.
This will pack the Kotlin classes into a JAR archive (together with other JVM classes, if you have a multi-language project), normally located at build/libs/yourProjectName.jar.
As to running the JAR, see this Q&A for a detailed explanation: (link)
Thanks for #hotkey answer, it helped me going the correct way.
First of all there is a mistake in the main class declaration, as it should follow the new methodology, that is in the below format:
mainClassName = '[your_namespace].[your_arctifact]Kt'
namespace = package name
arctifact = file name
so, considering the names given in the example above where filename is: Hello.kt, and the namespace is hello, then:
mainClassName = `[hello].[Hello]Kt`
using the previous method, that contains:
apply plugin: 'application'
mainClassName = 'hello.HelloKt'
the generated .jar file is not including the kotlin runtime, so the only way to execute it, is by:
d:/App/build/libs/kotlin -cp App.jar hello.HelloKt
but in order to generate a self contained jar that can be self-executed, and contains the kotlin runtime then the build.gradle should be written as:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes 'Main-Class': 'hello.HelloKt'
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
followed by gradle build, the [your_working_folder].jar file will be generated at the build/libs folder, assuming the working folder name is app, then file app.jar will be generated.
To run this file, one of the following 2 commands can be used:
D:\App\build\libs\java -jar App.jar
OR
D:\App\build\libs\kotlin App.jar hello.HelloKt

Categories

Resources