Including class files in gradle build script - gradle

I have useful java class files from a old project on my computer and would like to include them in a build.gradle script for a new project. I also am using a library off the Maven repository and a jar file on my computer. I'm only have problem with including the classes and no problem with either of the others. Here is the dependency block.
dependencies {
compile 'no.tornado:tornadofx:1.7.14' //works
compile files('../lib/opencv-320.jar') //works
compile files('../oldProject/com') } //seems ok in IntelliJ, but won't compile
By including the oldProject/com in this way IntelliJ is able to see the dependency and recognize the class. However, when proceeding to run the app, here is the error that suggests the package "util" and class "MultiArray" in oldProject/com are not being seen by the Kotlin compiler.
Information: Kotlin: kotlinc -jvm 1.2.21 (JRE 1.80_71-b15)
Error:(3,8) Kotlin: Unresolved reference util
Error:(18,17) Kotlin: Unresolved reference MultiArray
Here is the include line in the top level settings.gradle file
include ('../oldProject/com')

com is your top-level package and not the directory root. You have to add the dependency
compile files('../oldProject')
IntelliJ can detect directory prefixes, but Gradle does not do that.

Related

subprojects dependencies failure with Gradle

I'm struggling with Gradle and the build configuration of the following project structure (pretty simple...):
/projA
/projB
/projC
projC using classes from projB.
In projA/settings.gradle:
include 'projB'
include 'projC'
In projC/build.gradle:
dependencies{
compile project(':projB')
}
In IntelliJ I have no problem of dependency resolution, but when I'm running a ./gradlew build in projA, I'm facing a compilation error:
ClassC: Unresolved reference: ClassB
(where ClassC is the class of projC which is failing on the use of ClassB which is a class from projB, obviously...)
Notice that the code is in Kotlin language, that I do not have any problem to run the app in IntelliJ (spring boot run), but any build with Gradle give me an error (both in Intellij and command line).
What am I missing?
Regards,
Adrien
It's a common Gradle idiom to have an additional top level directory for your rootProject. That's a special project that's the parent to all other projects in your build, in a multi-project build.
That's where your settings.gradle file goes:
include ':projA:projB'
include ':projA:projC'
Then, I'd recommend having projA as a subdirectory of your rootProject, so your hierarchy would look as follows:
/myProject
settings.gradle
/projA
build.gradle
/projB
build.gradle
/projC
build.gradle
Also, in projC/build.gradle, you'll want instead:
dependencies {
compile project(':projA:projB')
}
That should do it.

gradle fails to compile java classes using kotlin class

I have a gradle 4.1 multiproject containing a "projectA" containing 2 subfolders "api" and "implementation".
The multiproject uses kotlin and java-library plugins defined in the subprojects section of the main build.gradle.
The implementation project avec a API dependency to :projectA:api
In the api folder I have kotlin and java files inside 'src/main/java' and in the implementation project I'm creating a new instance of a kotlin class from the API.
Inside Intellij Idea, I don't have any compilation errors ; but when I compile the whole project using gradle I have an error: cannot find symbol. It is as if the compileJava doesn't have access to the folder kotlin-classes.
Inside the build/kotlin-classes, I see my file.class
The class file is on build/classes dir also
Details of the error :
Task :projectA:api:compileKotlin
Using kotlin incremental compilation
Task :projectA:implementation:compileJava
(...) error: cannot find symbol (the import fails)
Update 1 : removing java-library solved my problem
This is a known issue of the java-library plugin: when used in a project with another JVM language (Kotlin, Scala, Groovy etc.) , it does not register the classes of the other language so that the dependent projects get them as they consume the classes.
Fortunately, it has a workaround as well. Adapted to Kotlin, it would look like:
configurations {
apiElements {
outgoing.variants.getByName('classes').artifact(
file: compileKotlin.destinationDir,
type: ArtifactTypeDefinition.JVM_CLASS_DIRECTORY,
builtBy: compileKotlin)
}
}
If you use Kapt1, it's file: compileKotlinAfterJava.destinationDir, and for Gradle versions lower than 4.0 use builtBy: copyMainKotlinClasses instead.
This issue is also tracked in the Kotlin issue tracker: KT-18497, follow that issue to see when it's fixed on the Kotlin Gradle plugin side, so that the above workaround will be no more necessary.

How do I import an externally built dependency list into a gradle script

I have a build.gradle script that builds a project including a list of dependencies that are generated through an external process. The build works fine if I include the dependencies directly in the build.gradle file:
dependencies {
runtime 'commons-beanutils:commons-beanutils:1.8.0'
runtime 'commons-logging:commons-logging:1.1.1'
runtime 'commons-httpclient:commons-httpclient:3.1'
...
}
However, this list changes and is built by an external process.
I would like to import the dependency list from an external file built separately
dependencies {
import 'deps.gradle'
}
But I don't see how this is possible with anything I'm seeing in gradle.
Another option I've explored is building the dependency list dynamically within the gradle build using a plugin -- but I've not been able to make that work.
All you have to do is using the applycommand. Make sure the path to your file is good and you should be in business!
dependencies {
apply from: "deps.gradle"
}
For more information see point 21.3.1 on the gradle documentation

Problems during build when referencing static lib directory gradle

I use a build.gradle file which needs to reference some jar files which are neither available on a public nor an internal repository.
Because of that I put the jar files into a lib directory which is below my Eclipse project:
dependencies {
...
compile files('lib/*.jar')
}
This works fine as long as I run or export my program from within Eclipse. But when I try gradle build gradle fails with this error:
Could not normalize path for file
'C:\Workspaces\MyProject\project\lib\*.jar'.
I changed the path to lib\*.jar, added a leading ./, removed the \*.jar but to no avail.
When I remove the compile files line from the dependencies and add this to repositories instead
repositories {
...
flatDir { dirs './lib' }
}
the compiler complains about error: cannot find symbol, and all these symbols are in the jars referenced from the lib directory.
When I run gradle --debug I can see from the log that the directory referenced by flatDir is being searched in:
10:02:20.587 [DEBUG] [org.gradle.api.internal.artifacts.repositories.resolver.ResourceVersionLister] using org.gradle.internal.resource.transport.file.FileResourceConnector#48499739 to list all in /C:/...../lib/
10:02:20.592 [DEBUG] [org.gradle.api.internal.artifacts.repositories.resolver.ResourceVersionLister] found 7 urls
7 is the count of files in the lib directory.
Why does this work in Eclipse (STS 3.7 with gradle tooling) and not from the command line ? I tried both gradle 2.2.1 and 2.5.
You need to use a FileTree instead of a FileCollection:
compile fileTree(dir:'lib', include:'*.jar')
The reason why eclipse behaved differently than your gradle is most likely because you didn't run gradle eclipse (a task added by the eclipse plugin).
Eclipse does not interact with you build.gradle at all naturally. The eclipse task updates your eclipse to be in snyc with gradle.
I had a hard time finding this, so I will keep my own question and write down what solved the problem.
Obviously there is some kind of different behavior which makes the command line gradle hickup when combining path and wildcards.
This is the correct syntax, which works for Eclipse and gradle on the command line:
dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
}
I took this from another post here on stackoverflow which I didn't find during my initial research.

Gradle Plugin jar Dependencies

I have a gradle build that relies on a plugin (MyTools) which is compiled in the buildSrc directory. This part is working correctly. The issue I'm having is trying to import a class from an external jar to use in the myTools plugin's source.
My Directory Structure looks like this:
buildSrc
---build.gradle
---MyTools
-----build.gradle
-----settings.gradle
-----libs
-------yuicompressor-2.4.6.jar
-----src
-------main
---------groovy
-----------com
-------------my
---------------MyTools.groovy
---------------MyToolsPlugin.groovy
---------resources
-----------META-INF
-------------gradle-plugins
-------------gradle-plugins/MyTools.properties
The contents of MyTools/build.gradle are:
dependencies {
runtime fileTree(dir: 'libs', include: '*.jar')
}
When I try to import com.yahoo.platform.yui.compressor.CssCompressor from MyTools.groovy, I get this message:
"unable to resolve class com.yahoo.platform.yui.compressor.CssCompressor"
Can somebody please tell me what I'm doing wrong?
You need to add a compile dependency, not a runtime dependency. Also, I don't see how the main build is going to pick up the plugin, given that it's located in a MyTools subdirectory (and buildSrc doesn't have a settings.gradle). Probably best to lift up MyTools into buildSrc.

Resources