How would you add a library downloaded as a folder to gradle? - java

I'm new to Gradle and finding the documentation confusing.
I've downloaded the Jaunt webscraping API, however I can't seem to add it to my project. When downloaded, Jaunt is a folder that contains the jauntx.x.x.jar file inside it as a root for the library. In a normal IntelliJ project, I would simply add a reference to that .jar file.
However, I'm using Gradle as this project requires being built into an executable jar
I need to add the Jaunt library to the project but if I do it the normal way my project recognises the library while I'm developing it but as soon as I try to run it, it tells me that the com.jaunt package is not found.
I assume that it needs to be added to the build.gradle file, however I'm not sure how.
Can anyone advise?
Build.gradle as follows:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories {
mavenCentral()
}
dependencies {
implementation files("libs/jaunt1.6.0.jar")
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
jar {
manifest {
attributes 'Main-Class': 'org.openjfx.Main'
attributes 'Class-Path': 'org/openjfx/ '
}
}
application {
mainClassName = 'org.openjfx.Main'
}

Related

JavaFX FatJar using Gradle and IntelliJ

I've tried everything by now, so I hope someone in here can tell me more...
Im trying to produce an executable .jar from a IntelliJ Gradle JavaFX project. I used the standard setup that IntelliJ provided, I changed the Gradle.build file however.
The new file I got from here: Non-Modular Gradle (openjfx.io)
I have a main class that has some basic code in it and a launcher class that does not extend Application and is specified as the Main class in the jar manifest.
For now I only use javafx.controls and basically everything is as the example they provided here.
When doing the ./gradlew jar command I get the error:
no module-info.java found
Which - as I understand - is not required if I use the Non-Modular approach?
However if I add it I get the error:
Entry module-info.class is a duplicate but no duplicate handling strategy has been set.
I tried every other option out there, all of them lead to either the 2. error or the jar was produced but not executable due to the fact that it can't find the Application class...
Any help is greatly appreciated.
I just want to point out that I've never really used Gradle before and have never formally learned any coding, but can fiddle my way around usually.
For the sake if it my build file:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9' // this is old right?
}
repositories {
mavenCentral()
}
dependencies {
/* uncomment for cross-platform jar: */
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
}
javafx {
version = "16"
modules = [ 'javafx.controls' ]
}
mainClassName = 'main.class.with.Code'
jar {
manifest {
attributes 'Main-Class': 'main.class.with.Launcher'
}
from {
// this is what causes the module duplicate error I think (at least it did in my other tries)
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

gradle exclude external resource folder from all jars

Using gradle 5.4 with a project dependency from external folder in a netbeans project.
The external folder contains resources like images, xml and custom objects that can only be created by this netbeans project. These external assets are then used to create binary files that get packed into a separate jar by that netbeans project.
These same external resources are also used during runtime for development in the gradle project. While I need the resources for development in the gradle project, I do not need or want them to be included in any jars anywhere for any reason when using the task build command because only the binaries are needed for distribution.
How to exclude the external resources from any and all jar files in the gradle project but allow them to be used for the classPath so I can run the project?
Some code examples of failure.
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
sourceSets {
main {
resources {
srcDir '../ExternalResourceFolder/assets'
}
}
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
dependencies {
runtimeOnly files('../ExternalResourceFolder/assets')
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
distributions {
main {
contents {
exclude '../ExternalResourceFolder/assets/**'
}
}
}
Tried many more things like adding to classPath and exclude but it would just be clutter to add them. Changing from sourceSet to dependency only moves the problem around from "build/lib" folder to "build/distributions" folder.
Had to exclude per file type in the end.
sourceSets {
main {
resources {
srcDir '.'
exclude ('**/*.j3odata','**/*.mesh','**/*.skeleton',\
'**/*.mesh.xml','**/*.skeleton.xml','**/*.scene',\
'**/*.material','**/*.obj','**/*.mtl','**/*.3ds',\
'**/*.dae','**/*.blend','**/*.blend*[0-9]','**/*.bin',\
'**/*.gltf')
}
}
}

Gradle compile dependency outside of project

I have a frontend and a backend repository for my app. The backend is written in Go and serves the API over gRPC. The generated gRPC Java files end up in backend-repo/proto-gen/java/ (so backend-repo/proto-gen/java/com/myApp/Users.java).
In my frontend android repo I have submoduled the backend repo to a folder called server. I want to modify my build.gradle to compile the .java files from the backend.
android-repo/
app/
build.gradle
server/proto-gen/java/com/myApp/
Users.java
AnotherService.java
I'm very new to Android development and am struggling to figure out the right approach.
This is a snippet from my app/build.gradle but it fails because it can't find the package com.google.protobuf.
task compileGrpc (type: JavaCompile) {
source = fileTree(dir: '../server/proto-gen/java/', include: '**/*.java')
destinationDir = file('build/classes')
classpath = files('../server/proto-gen/java/')
options.compilerArgs = ["-sourcepath", "$projectDir/../server/proto-gen/java/"]
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0-alpha-2'
compileGrpc.execute()
}
You can add your dependencies like
dependencies {
classpath files('server/proto-gen/java/com/myApp/')
}
This will accept relative path.
Simply including the path to the src sourceset for the project would include them within the compilation:
android {
...
sourceSets {
main {
java.srcDirs = [java.srcDirs, 'server/proto-gen/java']
}
}
}
You'll be able to see the src files/directories included within the Android View/Project Explorer on the right side and edit the files directly.

Android Studio reuse module library at other project

I have a module with some POJO classes that is marked at gradle as apply plugin: 'java' .Is there a way to reuse it at another project ? Everything i tried failed . (I dont want to copy pasta it)
I was facing the same issue recently.
This is how I resolved the code redundancy problem:
Create a new Android Studio project 'libs' and add all my APIs in a 'library' module.
In build.gradle of your library module, add code to upload the artifact to local Maven repo.
`
apply plugin: 'maven'
group = 'com.<example>'
version = '1.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///Users/<myuser>/.m2/repository")
}
}
}
`
The archive is uploaded in your maven repo as aar file.
Use this aar file in any other project as a dependency.
Hope this helps.
Here are two other questions on the same matter.
How do I add a library project to Android Studio?
How to create a library project in Android Studio and an application project that uses the library project
Personally I didn't use any of the two provided methods.
I built my project as a JAR, added it to the 'libs' folder, right clicked on it and clicked 'Add as library' and then finally added the dependency in the gradle file like so:
dependencies {
compile files('libs/MyJAR.jar')
}

How can I add a linked source folder in Android Studio?

In Eclipse I can add a source folder to my Android project as a "linked source folder". How do I achieve the same thing in Android Studio?
Or is it possible to add an external folder to build in Gradle?
In your build.gradle file, add the following to the end of the Android node:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
The right answer is:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
Furthermore, if your external source directory is not under src/main, you could use a relative path like this:
sourceSets {
main.java.srcDirs += 'src/main/../../../<YOUR DIRECTORY>'
}
You can add a source folder to the build script and then sync. Look for sourceSets in the documentation here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basic-Project
I haven't found a good way of adding test source folders. I have manually added the source to the .iml file. Of course this means it will go away everytime the build script is synched.
While sourceSets allows you to include entire directory structures, there's no way to exclude parts of it in Android Studio (as of version 1.2), as described in Exclude a class from the build in Android Studio.
Until Android Studio gets updated to support include/exclude directives for Android sources, symbolic links work quite well. If you're using Windows, native tools such as junction or mklink can accomplish the equivalent of symbolic links on Unix-like systems. Cygwin can also create these with a little coercion. See: Git symbolic links in Windows and How to make a symbolic link with Cygwin in Windows 7.
Here’s a complete Java module Gradle file that correctly generates and references the built artefacts within an Android multi-module application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: "java-library"
apply plugin: "idea"
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
dependencies {
// Dagger 2 and Compiler
compile "com.google.dagger:dagger:2.15"
apt "com.google.dagger:dagger-compiler:2.15"
compile "com.google.guava:guava:24.1-jre"
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
This is for Kotlin DSL (build.gradle.kts):
android {
sourceSets["main"].java.srcDirs("src/main/myDirectory/code/")
sourceSets["main"].resources.srcDirs("src/main/myDirectory/resources/")
// Another notation:
// sourceSets {
// getByName("main") {
// java.srcDirs("src/main/myDirectory/code/")
// resources.srcDirs("src/main/myDirectory/resources/")
// }
// }
}
If you're not using Gradle (creating a project from an APK, for instance), this can be done through the Android Studio UI (as of version 3.3.2):
Right-click the project root directory and pick Open Module Settings
Hit the + Add Content Root button (center right)
Add your path and hit OK
In my experience (with native code), as long as your .so files are built with debug symbols and from the same absolute paths, breakpoints added in source files will be automatically recognized.

Categories

Resources