I'm trying to include EasyModbusJava.jar and commons-lang3-3.8.1.jar in a JavaFX Gluon project using Eclipse. But when I compile, I get this:
/home/dell/Dokument/eclipse-workspace-2018-09/OKIDERAMPC/OKIDERAMPCApp/src/main/java/com/gluonapplication/thread/ModbusConnection.java:6: error: package org.apache.commons.lang3 does not exist
import org.apache.commons.lang3.ArrayUtils;
/home/dell/Dokument/eclipse-workspace-2018-09/OKIDERAMPC/OKIDERAMPCApp/src/main/java/com/gluonapplication/thread/ModbusConnection.java:8: error: package de.re.easymodbus.modbusclient does not exist
import de.re.easymodbus.modbusclient.ModbusClient;
How can I in a very easy and proper way, using Eclipse, to include JAR files into a Gluon project? I don't want to include the JAR files with a non-standard way, like editing an file and copy and paste. It can break my project. It's better to use the tools from the IDE instead.
picture
The only way that I know is to include the respective jars in the build.gradle
dependencies {
compile 'com.gluonhq:charm:5.0.1'
compile files('<Path_to_jar>/EasyModbusJava.jar'
, '<Path_to_jar>/commons-lang3-3.8.1.jar'
)
}
The jars show in eclipse under "Project and External dependencies" and their properties (e.g. path to javadoc) can not be edited.
To achieve this you can add
apply plugin: 'eclipse'
and
eclipse {
classpath {
downloadJavadoc = true // to get the Gluon mobile (charm...) javadocs;
file {
whenMerged { cp ->
// Add other javadoc and sources to classpath entry
def fileReferenceFactory = new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
def defvar1 = cp.entries.find{ defvar1 -> defvar1.path.endsWith('EasyModbusJava.jar') }
// add javadoc path
defvar1.javadocPath = fileReferenceFactory.fromPath('<Path_to_javadoc>')
// add source path
defvar1.sourcePath = fileReferenceFactory.fromPath('<Path_to_javasource>')
}
}
}
to gradle.build
Related
I am trying to figure out the proper way to declare a project dependency in gradle, i have this two subprojects:
A java app
apply plugin: 'java'
sourceSets {main {java {srcDirs = ['./']}}}
dependencies{compile project(':p2')}
jar {manifest.attributes(
'Class-Path': configurations.runtime.files.collect { it.name }.join(''),
'Main-Class': 'Main'
)
}
and a java library:
apply plugin: 'java-library'
sourceSets {main {java {srcDirs = ['./']}}
}
jar {
manifest.attributes(
'Class-Path': configurations.runtime.files.collect { it.name }.join(' ')
)
}
The root project is empty and the settings just include the subprojects.
The dependencies in the p1 project only tells that p2 must be built before p1, but what about configuring p2 as a lib for p1? Right now if a run p1 i got:
Exception in thread "main" java.lang.NoClassDefFoundError: StaticClass
Gradle build is fine:
C:\...>gradle build
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 up-to-date
I have to copy and paste p2.jar to the p1.jar directory for it to run properly, how can i get gradle to do it for me?
If you're trying to pack the classes from p2 into the p1 jar, try adding this to p1's build.gradle:
jar {
from project(':p2:').sourceSets.main.output
}
This is a bit unusual, though. Normally, if you've set up a separate library project, you pack it in a separate jar and add it to the classpath of dependent projects, so it's reusable.
Setting the Class-Path on the jar's manifest won't work the way you've written it. The manifest attribute interprets each entry as a file path relative to the working dir of the JVM, but you're just giving it the name of each jar, and Gradle doesn't do any copying or moving of files when setting up configuration classpaths. It's generally not a good idea to rely on the manifest to set the classpath, as it will only work if your jars are arranged on the filesystem exactly as as the manifest expects it. If this is really what you want, then you need to set up an 'installation' directory containing all the required jars arranged as expected. The Gradle Application Plugin can probably help you to achieve this, or something equivalent, but I've never used it.
Depending on your exact needs, you should have a look at the java-library-distribution plugin or the application plugin.
The first one will package a jar and its dependencies in an archive.
The second one will do the same and allow you to configure the main jar to be executable.
I have a *.jar file in my Gradle / Buildship project that resides in a lib folder. I include it in my build.gradle via:
compile files('libs/local-lib.jar')
I also have a correspondinglocal-lib-sources.jar file that I would like to attach to it. In Eclipse, for manually managed dependencies this works via context menu of build path entry -> Properties -> Java Source Attachment. However, for gradle-managed dependencies, that option is not available.
Does anybody know how what the gradle/buildship way to do this looks like? My dependency is in no repository, so I'm stuck with compile files for now.
If you want to use Buildship with Eclipse then you are out of luck since this is not currently supported by gradle (see https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/8).
If you are ok with not using Buildship and manually generating the Eclipse dot files you can do something like this in your build.gradle:
apply plugin: 'eclipse'
eclipse.classpath.file {
withXml {
xml ->
def node = xml.asNode()
node.classpathentry.forEach {
if(it.#kind == 'lib') {
def sourcePath = it.#path.replace('.jar', '-sources.jar')
if(file(sourcePath).exists()) {
it.#sourcepath = sourcePath
}
}
}
}
}
You would then run gradle eclipse from the command line and import the project into Eclipse using Import -> "Existing Projects into Workspace"
Another (possibly better) option would be to use a flat file repository like this:
repositories {
flatDir {
dirs 'lib'
}
see https://docs.gradle.org/current/userguide/dependency_management.html#sec:flat_dir_resolver
You would then just include your dependency like any other; in your case:
compile ':local-lib'
This way Buildship will automatically find the -sources.jar files since flatDir acts like a regular repository for the most part.
Use an extra folder called lib or similar on the same directory level as src or you build script.
dependencies {
//local file
compile files('lib/local-lib-sources.jar')
// others local or remote file
}
I have this project setup (in Android AIDE):
|---Project1
| |---App
| |---Lib
|
|---Project2
|---App
I want add project 1 Lib as dependency (or linked source, because i only need to access 1 class from the library) to Project2.
Project2 settings.gradle:
include ':Lib'
project(':Lib').projectDir = new File(settingsDir, '../Project1/Lib')
Project2 build.gradle:
compile project(':Lib')
I am getting the following error:
Project dependency 'Lib' not found.
Is there anything wrong with the folder path? I didn't find any good solution for this problem and i am not very familiar with gradle.
If i would instead link the library source folder to Project2 i tried to add the following lines to the build.gradle (under android section):
sourceSets {
main.java.srcDirs += '/../Project1/Lib/src/main/'
}
I could access the source if i used the full path down to the folder with the class file but with the problem that when i instantiated the class from the Lib in Project2 it gave me the error that the class was not found inside the package.
Do both projects need to have the same package names? What would be the best solution to manage this without having the Library as an standalone library?
Right now I have got a Java library which has a test class. In that class I want to access some files located on my hard disk.
The build.gradle looks like this:
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.11'
}
My file is under java_lib/src/test/assets/file.xml and the Java class is under java_lib/src/test/java/<package_name>.java
Therefore I execute
final InputStream resourceAsStream = this.getClass().getResourceAsStream("assets/file.xml");
Unfortunately I get null back. What am I doing wrong?
To get thing rolling you need to add the following to the gradle file:
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
What it basically does is copying all the files in the src/test/resource directory to build/classes/test, since this.getClass().getClassLoader().getResourceAsStream(".") points to build/classes/test.
The issue is already known to Google and they want to fix it in Android Studio 1.2 (since they need IntelliJ14 for that and it seems like it will be included in Android Studio 1.2)
Try placing file.xml under src/test/resources and use this.getClass().getResourceAsStream("file.xml") (without the folder prefix)
The problem appears to be that the assets folder is not part of the test runtime classpath, hence this.getClass().getResourceAsStream("assets/file.xml") wouldn't be able to resolve the path as you expected.
By default, the test resources folder in a Gradle java project is src/test/resources (same as a Maven java project). You can override it to assets folder if you wish by adding this in the project's build.gradle file:
sourceSets.test {
resources.srcDirs = ["src/test/assets"]
}
In build.gradle, add this :
sourceSets.test {
resources.srcDirs = ["src/test"]
}
In your code, access your resource like this :
getClass().getClassLoader().getResourceAsStream("assets/file.xml"));
Works for me.
Thanks for pointing out the Google issue I've been looking all day for this...
In "Android Studio 1.1 RC 1" (gradle build tool 1.1.0-rc1) there is no need to add the work around to the gradle file, but your you have to execute the test from the gradle task menu (or command prompt)!
This worked for me (3 years later, gradle 4.10)
subprojects {
junitPlatformTest.dependsOn processTestResources
}
I want to add this project as library to my project in android studio.
this is what I tried,
I have my project directory as f:/my project/my app/src
and my library in f:/my project/my library/src
I import the module (the library) by going to file > import module > selecting the library
then I got to file > project structure > modules > dependencies tab > select my project > add module dependency apply ok and then done
however when I use the code from the library I get the usual syntax error (the class ... could not be found)
also I noticed this popup (see image)
I am new to android studio or intelliJ, how do I fix this.
Thanks!
Edit the settings.gradle file (in directory f:/my project), it must contains something like this:
include 'my app','my library'
If this file don't exists: create it manually. The settings.gradle contains the list of gradle modules in a multi-module project.
Then you must add the dependency to your library in app. To do so edit the my app/build.gradle and add this line :
dependencies {
compile project(':my library')
}
I also notice that you don't use default structure for your projects (i.e. you put the code in src/ instead of src/main/java) so you will have to overwrite some values of the default fileSet in the build.gradle of your projects. Be sure to have something like this in my app/build.gradle and my library/build.gradle :
android {
sourceSets {
main {
java.srcDirs = ['src']
}
}
}