Building shadow jar with gradle locally - java

I am using shadow plugin in gradle to build jar file, I have added the build script as follows
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
Instead of asking gradle to fetch shadow-1.2.2.jar from jcenter(), I want to get the shadow-1.2.2.jar from http://jcenter.bintray.com/com/github/jengelman/gradle/plugins/shadow/1.2.2/
I just want to build it locally, I have put the shadow-1.2.2.jar in libs folder and my build script is
buildscript {
//repositories { jcenter() }
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
But it not working please help!

The following piece of code should work:
buildscript {
dependencies {
classpath fileTree(dir: 'libs', include: '*.jar')
}
}
There's no compile configuration for gradle buildscript dependencies.

Related

How to include local jars into a fat jar using Gradle 6.3?

On a Linux machine running Java 8 and Gradle 6.3 I need to build a fat jar made of mix of libraries, some sourced from Maven Central, others from a local libs directory located at the root of my repository, together with my build.gradle and the gradlew:
apply plugin: 'java'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
task copyLibs(type: Copy) {
from configurations.compile
into 'libs'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation "junit:junit:4.12"
}
After running ./gradlew clean build and cd build/libs, if I unzip myproject.jar I can see that no dependencies have been included in the jar.
My end goal is to make my project executable as java -jar myproject.jar.
How can I solve this?
After unzipping your jar file check here for all dependencies.
Your-Project --> BOOT-INF --> libs
by default, if your Gradle build is successful the jar files come here.
you can run java -jar then.
This is a build.gradle that generates a fat jar including local dependencies:
plugins {
id 'java'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.mycompany.foo'
version '1.0'
jar {
archiveBaseName = 'myjarname'
archiveVersion = '0.1.0'
manifest {
attributes(
'Main-Class': 'com.mycompany.foo.Main'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
repositories {
mavenCentral()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.assertj:assertj-core:3.20.2'
}

WildFly Swarm + War + local Jar dependencies in Gradle - NullPointerException

I am trying to build a web application server using WildFly Swarm and the application has to be able to run another Java program inside (I don't want to run it as an external process). I am trying to include the external program as .jar dependency to my web application, however, wildfly-swarm-package task always fails with the following:
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:war
:wildfly-swarm-package FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':wildfly-swarm-package'.
> java.lang.NullPointerException (no error message)
This is my gradle.build file:
buildscript {
version = System.getProperty('swarmVersion') ?: '2016.10.0'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
classpath "org.wildfly.swarm:wildfly-swarm-plugin:$version"
}
}
apply plugin: "io.spring.dependency-management"
apply plugin: 'wildfly-swarm'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'war'
//mainClassName = 'org.siret.prank.webapp.rest.Main'
swarm {
properties {
swarm.http.port = 8181
}
}
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/releases/'
}
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/thirdparty-releases/'
}
flatDir {
dirs 'libs'
}
}
dependencyManagement {
imports {
mavenBom "org.wildfly.swarm:bom-all:$version"
}
}
dependencies {
compile group: 'org.biojava', name: 'biojava-core', version: '4.2.4'
compile group: 'org.biojava', name: 'biojava-structure', version: '4.2.4'
compile "org.wildfly.swarm:jaxrs"
compile group: 'org.wildfly.swarm', name: 'undertow', version: '2016.10.0'
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs/lib', include: ['*.jar'])
}
task debugJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006",
"build/libs/prank-webapp-swarm.jar"
]
}
}
task runJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"build/libs/prank-webapp-swarm.jar"
]
}
}
War plugin works fine, I can find the jars in WEB-INF/lib directory in the archive.
As an experiment, I tried to empty the libs folder and the error still persists.
Thanks,
Lukas
I migrated to Maven to find out that importing jar to local maven repository is the answer to the problem.
Now, both Maven an Gradle work.

Qt - package android.support.v4.app does not exist

I'm using Qt 5.4. I imported SDK & NDK.
Actually, I was trying to use multiple line notification and I used this line in java file:
customMainActivity.java:
import android.support.v4.app.NotificationCompat;
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
I'm getting an error :
package android.support.v4.app does not exist
I read it and it and added android-support-v4.jar and android-support-v7-appcompat.jar but I don't know how to fix it in Qt.
Add Google's Maven repository to build.gradle
https://developer.android.com/studio/build/dependencies#google-maven
allprojects {
repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
}
}
Add dependency to support-v4 library to build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:24.+"
}
You need to add the dependency to your build.gradle file (it's created and added to your project when you create android manifest via Create Templates button).
Something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:23.0.+"
}

how to add a library to the dependencies gradle of LIBGDX project

All is in the question , I've tried all the answers I found in SO and others sites but with no luck , this is what I've tried so far :
adding compile fileTree(dir: 'lib', include: '*.jar') to my build.gradle
adding compile files('lib/tween-engine-api-sources.jar') to build.gradle
the library I want to add is Tween engine .
build.gradle file :
buildscript {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = 'my-gdx-game'
gdxVersion = '1.5.4'
roboVMVersion = '1.0.0-SNAPSHOT'
box2DLightsVersion = '1.3'
ashleyVersion = '1.3.1'
aiVersion = '1.5.0'
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
compile "com.badlogicgames.ashley:ashley:$ashleyVersion"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
compile fileTree(dir: 'lib', include: '*.jar')
}
}
tasks.eclipse.doLast {
delete ".project"
}
In the wiki article Dependency management with Gradle, you can find all the information you need. There's even an extra part about the Tween Engine.
Your approach should work, however, you need to update Eclipse via a Right-Click on your projects -> Gradle -> Refresh Dependencies.
For me it worked better though to install the dependencies in my local repository and then reference it from there, instead of referencing the lib folder. This is described here.

Intellij: Avoiding 'Multiple Dex Files Defined'

Problem:
Up until now I've been using Gradle to handle all of my dependencies, and it seems to take care of any duplicate dependencies between other Gradle modules. However, this does not seem to be the case when a duplicate dependency exists within a jar.
Question:
Considering that I have control over what goes into the jar, What is the best practices for handling these dependency conflicts using Gradle:
Do not include any external dependencies in the jar, include them in the project itself using build.gradle
Include all external dependcies in the jar, remove duplicates as they occur by removing them from the project build.gradle. (NOTE: this does not seem scalable, e.g. if there are duplicates between jars themselves.)
Something better (that hopefully handles this automatically)
EDIT: build.gradle file:
apply plugin: 'com.android.application'
android {
signingConfigs {
release { ... }
debug { ... }
}
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig { ... }
buildTypes {
release { ... }
debug { ... }
}
sourceSets {
instrumentTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile project(':jarModule')
}
When importing external jars that have a dependency that you also have in your local app, you can do two things:
Convert your jar dependency to a Gradle dependency and exclude the dependency
For example:
testCompile('org.robospock:robospock:0.5.0') {
exclude module: "groovy-all" // <-- excludes groovy from robo spock
}
or
Remove the local dependency in your app and rely on the one in the .jar
For example, in your case with Gson:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile project(':jarModule')
// compile 'com.google.code.gson:gson:2.3.1' // <-- removed
}

Categories