Create a hadoop jar with external dependencies using Gradle - build

How do I create a hadoop jar that includes all dependencies in the lib folder using Gradle? Basically, similar to what fatjar does.

Figured it out! Hadoop looks for libraries inside the "lib" folder of the final jar. So, add the following to the "jar" task.
jar {
doFirst {
into('lib') {
from configurations.runtime
}
}
}

Related

Is there a way to make NativeLibrarySpec a part of task in gradle build file

I am trying to generate shared and static libraries in C++ project through gradle. Following is my build.gradle file.
apply plugin : 'cpp'
model{
components {
els(NativeLibrarySpec){
sources {
cpp {
source {
srcDirs "src/grad"
include "**/*.cpp", "**/*.h"
}
}
}
}
}
}
Here libraries are generated as soon as gradle build command is run. I don't want to run from build. I want to make it as a specific task so that, only when I call that task, libraries are generated.
I tried something like this
task libsGen(){
model{
components {
els(NativeLibrarySpec){
sources {
cpp {
source {
srcDirs "src/grad"
include "**/*.cpp", "**/*.h"
}
}
}
}
}
}
}
But here also as soon as gradle build command is run, libraries are generated. I want libraries to be generated only when I run task libsGen() explicitly.
I'm using Gradle 7.4.2 version.
Please help, I'm new to c++ and gradle.

How to include c/cpp header files inside aar?

Android .aar usually only contain *.jar, *.so and asset related resources.
How to include *.so related header files inside .aar?
Try to use AndroidNativeBundle plugin.
Edit your root build.gradle file, add classpath 'io.github.howardpang:androidNativeBundle:1.1.1' to the file
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
//Add androidNativeBundle dependency
classpath "io.github.howardpang:androidNativeBundle:1.1.1"
}
}
Apply plugin to your lib, add following line to your lib build.gradle
plugins {
id 'com.android.library'
id 'com.ydq.android.gradle.native-aar.export'
}
Specify header path that you want to export, add following code segment to your lib build.gradle;
nativeBundleExport {
headerDir = "${project.projectDir}/native/export/header/path"
}

Gluon build as single Jar

I'd like to build my Gluon Project as a SINGLE executable jar file.
Currently there is a startup script included with a bin folder and so on.
Is it possible to build a single jar? Or can I include an own gradle task that accomplishes this?
The current tasks like installApp or distZip will not bundle the jars in one.
If you want to create a 'fat-jar', you can add this plugin to your build.gradle script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'com.github.johnrengelman.shadow'
And then run gradlew shadowJar. It will create under libs an executable jar.
Note: if you are using desktopCompile or desktopRuntime dependencies, those won't be included so you need to change those to compile or runtime.
Since I had to build the jar with javapackager I used this plugin: javafx-gradle-plugin
I am relatively new to Gradle so adding the dependencies is just a temporary solution but it works
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.4.1'
}
}
jfx {
verbose = true
mainClass = "package.path.to.main.class.MainClass"
jfxAppOutputDir = "build/jfx/app" //configurable
jfxMainAppJarName = "ProjectName.jar"
deployDir = "src/main/deploy" //for signing
//many more options, go to link to learn about them
}
jfxJar.doFirst {
//TODO add all desktop dependencies
println("adding desktop dependency")
project.dependencies.add("runtime", "com.gluonhq:charm-desktop:2.1.0")
}

Why doesn't Gradle include transitive dependencies in compile / runtime classpath?

I'm learning how Gradle works, and I can't understand how it resolves a project transitive dependencies.
For now, I have two projects :
projectA : which has a couple of dependencies on external libraries
projectB : which has only one dependency on projectA
No matter how I try, when I build projectB, gradle doesn't include any projectA dependencies (X and Y) in projectB's compile or runtime classpath. I've only managed to make it work by including projectA's dependencies in projectB's build script, which, in my opinion does not make any sense. These dependencies should be automatically attached to projectB. I'm pretty sure I'm missing something but I can't figure out what.
I've read about "lib dependencies", but it seems to apply only to local projects like described here, not on external dependencies.
Here is the build.gradle I use in the root project (the one that contains both projectA and projectB) :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
group = 'com.company'
repositories {
mavenCentral()
add(new org.apache.ivy.plugins.resolver.SshResolver()) {
name = 'customRepo'
addIvyPattern "ssh://.../repository/[organization]/[module]/[revision]/[module].xml"
addArtifactPattern "ssh://.../[organization]/[module]/[revision]/[module](-[classifier]).[ext]"
}
}
sourceSets {
main {
java {
srcDir 'src/'
}
}
}
idea.module { downloadSources = true }
// task that create sources jar
task sourceJar(type: Jar) {
from sourceSets.main.java
classifier 'sources'
}
// Publishing configuration
uploadArchives {
repositories {
add project.repositories.customRepo
}
}
artifacts {
archives(sourceJar) {
name "$name-sources"
type 'source'
builtBy sourceJar
}
}
}
This one concerns projectA only :
version = '1.0'
dependencies {
compile 'com.company:X:1.0'
compile 'com.company:B:1.0'
}
And this is the one used by projectB :
version = '1.0'
dependencies {
compile ('com.company:projectA:1.0') {
transitive = true
}
}
Thank you in advance for any help, and please, apologize me for my bad English.
I know that this specific version of the question has already been solved, but my searching brought me here and I hope I can save some people the hassle of figuring this out.
Bad foo/build.gradle
dependencies {
implementation 'com.example:widget:1.0.0'
}
Good foo/build.gradle
dependencies {
api 'com.example:widget:1.0.0'
}
bar/build.gradle
dependencies {
implementation project(path: ':foo')
}
implementation hides the widget dependency.
api makes the widget dependency transitive.
From https://stackoverflow.com/a/44493379/68086:
From the Gradle documentation:
dependencies {
api 'commons-httpclient:commons-httpclient:3.1'
implementation 'org.apache.commons:commons-lang3:3.5'
}
Dependencies appearing in the api configurations will be
transitively exposed to consumers of the library, and as such will
appear on the compile classpath of consumers.
Dependencies found in the implementation configuration will, on the
other hand, not be exposed to consumers, and therefore not leak into
the consumers' compile classpath. This comes with several benefits:
dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive
dependency
faster compilation thanks to reduced classpath size
less recompilations when implementation dependencies change: consumers would not need to be recompiled
cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that
distinguish exactly between what is required to compile against the
library and what is required to use the library at runtime (in other
words, don't mix what is needed to compile the library itself and what
is needed to compile against the library).
The compile configuration still exists, but should not be used as it will not offer the guarantees that the api and implementation
configurations provide.
Finally, the problem didn't come from the scripts. I've just cleared gradle's cache, and each project's build folder, to make this work.
Put the following line in projectB's dependencies.
compile project(':projectA')

gradle not using compile time dependencies when building

Hi guys I'm new to gradle and have the following issue.
When I use the java plugin and invoke $gradle build on my project it does not put my third party dependencies on the classpath. My build.gradle file looks as follows:
apply plugin: 'java'
sourceSets.main.java.srcDirs = ["src/main/java", "src/main/web"]
repositories {
flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
}
dependencies {
compile files('log4j-1.2.12.jar', 'gson-1.7.1.jar')
}
and the error output from gradle is the following
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:3: package com.google.gson does not exist
import com.google.gson.Gson;
^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol : class Gson
location: class org.gradle.example.simple.HelloWorld2
Gson gson = new Gson();
^
C:\dev\gradling\TestProject\src\main\web\java\org\gradle\example\simple\HelloWorld2.java:7: cannot find symbol
symbol : class Gson
location: class org.gradle.example.simple.HelloWorld2
Gson gson = new Gson();
I have stated where my repo jars live and told it that when it compiles it must include the above mentioned jars.
Please help.
Your dependency declarations are incorrect. You can either specify a flatDir repo and use the usual dependency syntax for external dependencies (group:module:version), or use the files method together with correct file paths (then you don't need to declare a repository). In the former case, you may leave out the dependency's group. For example:
repositories {
flatDir name: 'thirdParty', dirs: 'C:/dev/repo'
}
dependencies {
compile ':log4j:1.2.12'
}
For more information, consult the Gradle User Guide.