groovy script is unable to find file in resources - gradle

I have a gradle application with a structure like this
src\main\
antlr\
groovy\
jvm\
script.groovy
java\
resources\
program.pas
In my script I have a line
CharStream input = new ANTLRFileStream("program.pas")
I run script using this task
task runScript(dependsOn: 'classes', type: JavaExec) {
main = 'jvm.script'
classpath = sourceSets.main.runtimeClasspath
}
But when I do that I get FileNotFound exception for program.pas.
When I move to the root of the project - it is able to find it.
My built structure looks like this
build\
classes\
main\
jvm\
script.class
resources\
main\
program.pas
Why it is unable to locate in resources?

Related

Run go command as a gradle task

I am using gradle as the build tool for a terraform project. I do have unit tests written in go for the project under the ..test/... folder . The way I am running test locally is just on the commandline go test ..test/..' which will run all tests under the test folder. I want to integrate this in the build , so that every build will run this command 'go test ..test/..', How do I achieve this in gradle. Can a custom task be utilized to run a go command?
I am trying to do something like the following
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'go','test'
} doLast {
println "Test Executed!"
}
But I get the error
> A problem occurred starting process 'command 'go''
For what its worth , I tried other commands and get the same erorr for ex
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'echo','${}buildDir'
} doLast {
println "Test Executed!"
}
gives similar error
> A problem occurred starting process 'command 'echo''
You can use the gradle plugin. First you can follow the starting guide and add the plugin:
plugins {
id 'com.github.blindpirate.gogradle' version '0.11.4'
}
golang {
packagePath = 'github.com/your/package' // go import path of project to be built, NOT local file system path!
}
Then you can run the following command to execute all go files that follow the file name convention <name>_test.go:
gradlew goTest
Otherwise you can also create a complete custom task or a custom task with the plugin.
EDIT:
I found the cause of your error. The variable buildDir refers to the build folder in your project: <project_folder>/build. The problem now is that the folder test does not exists and the exception is thrown. Instead, you can use the variable projectDir.

Create a second installDist task?

During development I am using a standard-function installDist (from the application plugin) in build.gradle:
installDist{}
... but I now want to have another task which installs/distributes/deploys a "production" version to the production location, which also incorporates the version into the directory structure. I tried this:
task deployOperativeVersion( type: installDist ) {
destinationDir = file( "$productionDir/$version" )
}
Build failure output:
Build file '/home/mike/IdeaProjects/JavaFXExp2/Organiser/build.gradle' line: 98
* What went wrong:
A problem occurred evaluating root project 'Organiser'.
> class org.gradle.api.tasks.Sync_Decorated cannot be cast to class java.lang.Class
(org.gradle.api.tasks.Sync_Decorated is in unnamed module of loader org.gradle.
internal.classloader.VisitableURLClassLoader #aec6354; java.lang.Class is in module
java.base of loader 'bootstrap')
It appears that installDist is not a "type" as in Test.
How can I achieve this? Incidentally I would be really keen on having two separate tasks: to get installDist to run I've found that you only have to type ./gradlew inst ... with a task called deployXXX it would be sufficient to type ./gradlew depl.
I also tried this:
task deployOperativeVersion{
installDist{
destinationDir = file( "$operativeDir/$version" )
}
}
... which doesn't seem to have done anything. Nor this:
task deployOperativeVersion{
doFirst {
installDist {
destinationDir = file("$operativeDir/$version")
}
}
}
A bit later I thought I had indeed found the answer:
task deployOperativeVersion{
dependsOn installDist{ destinationDir=file("$productionDir/$version")
}
... but to my amazement (will I ever get to a reasonable understanding of Gradle before Hell freezes over?), including this actually appears to influence the "routine" installDist task: specifically, it stops the latter from operating normally, and means that even when I run installDist the deployment/distribution/installation still goes to productionDir/version, rather than the default location.
So then I wondered about two tasks both of which are dependent on installDist:
task deployOperativeVersion{
dependsOn installDist{ destinationDir=file("$productionDir/$version") }
}
task stdInstall{
dependsOn installDist{ destinationDir=file("build/install") }
}
... haha, no joy: I run one and it deploys OK. I then run the other... and get an error:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':installDist'.
> The specified installation directory '/home/mike/IdeaProjects/JavaFXExp2/Organiser/build/install' is neither empty nor does it contain an installation for 'Organiser'.
If you really want to install to this directory, delete it and run the install task again.
Alternatively, choose a different installation directory.
... needless to say, this is NOT the case: under ...Organiser/build/install there is one directory only, Organiser, with /bin and /lib directories under it.
Your task should be declared as a Sync task, which is the actual type of the installDist task. The application plugin is using the distribution plugin. You can grab the content configuration from the main distribution, which is the source, or from the installDist task.
task deployOperativeVersion(type: Sync) {
destinationDir = file("${productionDir}/${version}")
with distributions.main.content
}
or
task deployOperativeVersion(type: Sync) {
destinationDir = file("${productionDir}/${version}")
with installDist
}

gradle main task to zipup sub-projects distribution files

I have a gradle project which at main is using java plugin. At subproj1 - thru subproj4 the build.gradle files are also using application plugin. It works well in sub projects to compile the jars (with a main class) and create a distribution zipfile (using resources and dist files).
Now I want to build a 'main' dist zip file, comprising of the contents of all those subproj contents. I found I can run installDist to unzip to each of the subprojects build/install/subprojN
Now at a loss howto in the main only, have a task and/or dependency to create a "main" dist zip file containing: subproj1/** subproj2/** subproj3/** subproj4/**
My thoughts are to do a copy from('.').include('subproj*/build/install//')
then zip that up. but havent figured out howto add the task only at main level, plus have it not complain: NO SOURCE
thanks in advance
Here's an example that just uses two sub-projects, and does not show the compilation of the main code. That stuff should be easy to add. The key to the solution is the zip and zipfileset via the AntBuilder.
Consider this in the main build.gradle:
task uberBuild(dependsOn: ['subProj1:build',
'subProj2:build']) {
doLast {
ant.mkdir(dir: "${projectDir}/dist")
def PATH = "build/distributions"
ant.zip(destfile: "${projectDir}/dist/uber.zip") {
zipfileset(src: "${projectDir}/subProj1/${PATH}/subProj1.zip")
zipfileset(src: "${projectDir}/subProj2/${PATH}/subProj2.zip")
}
}
}
This will write ~/dist/uber.zip, which has the contents:
$ jar tf dist/uber.zip
subProj1/
subProj1/bin/
subProj1/lib/
subProj1/bin/subProj1
subProj1/bin/subProj1.bat
subProj1/lib/subProj1.jar
subProj2/
subProj2/bin/
subProj2/lib/
subProj2/bin/subProj2
subProj2/bin/subProj2.bat
subProj2/lib/subProj2.jar

Gradle: add config file to classpath

In a simple gradle project I have under the projects root a config folder with a config.json in it. I want to put this file on the path to read it.
def myconfigfile = new File("config", "config.json")
def configset = files(myconfigfile)
task runExample ( dependsOn: 'classes', type: JavaExec ) {
main = 'com.example.LoadConfigExample'
classpath = sourceSets.main.runtimeClasspath
classpath.add(configset)
args 'ARG1'
}
In the Java file I'm using the following to load the file:
URL u = LoadConfigExample.class.getResource("config.json");
Would it be better just to use relative paths and load via the file system? I don't want to put the file under src/main/resources.
If I understood well the best option seems to refer to the config.json via project.file method, for docs see here.
It will be:
project.file("confif${File.separator}config.json")

Gradle JavaExec task failing with library class not found error in Android Studio

I am trying to integrate the running of the ORMLite data 'compiler' utility in my android gradle script. From various sources I have established how to place it in the build flow with the required dependencies to makes sure it runs after the java compile and a second task will update the apk with the generated database definitions file using aapt.
My main issue is that I can't get my OrmLiteConfigUtil extended class to run because the JavaExec task fails to locate the OrmLiteConfigUtil class
Error:Gradle: java.lang.ClassNotFoundException: com.j256.ormlite.android.apptools.OrmLiteConfigUtil
The task definition is as follows
applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
def databaseTaskName = "${variant.name.capitalize()}DatabaseCompile"
def buildDir = getBuildDir()
def javaCompileTaskName = "compile" + variant.name.capitalize() + "Java"
def javaTask = project.tasks.findByName("${javaCompileTaskName}")
task "${databaseTaskName}" (type: JavaExec) {
main = 'com.barclaycard.bespoke.android.data.local.DatabaseConfigUtil'
dependencies {
classpath files("${buildDir}/intermediates/classes/${variant.dirName}")
}
}
if (javaTask != null) {
println "Adding post-compile hook to ${variant.name}"
javaTask.finalizedBy "${variant.name.capitalize()}DatabaseCompile"
}
}
and is placed inside the android section of build.gradle
I have tried adding additional statements in the dependencies section of the task but to no avail.
compile 'com.j256.ormlite:ormlite-android:4.48'
(ClassNotFoundException thrown during task execution)
classpath 'com.j256.ormlite:ormlite-android:4.48'
and
classpath files('com.j256.ormlite:ormlite-android:4.48')
(Cannot convert URL 'com.j256.ormlite:ormlite-android:4.48' to a file.)
I can't see how to get the JavaExec task to use the remote repo to find the required class.
compile 'com.j256.ormlite:ormlite-android:4.48'
is used in the main project dependencies and works fine.
Might be misunderstaning you a little bit here, but could it be that you are using the dependencies wrong? Might be this is suitable
dependencies {
orm 'com.j256.ormlite:ormlite-android:4.48'
}
...
task "${databaseTaskName}" (type: JavaExec) {
main = 'com.barclaycard.bespoke.android.data.local.DatabaseConfigUtil'
classpath configurations.orm + files("${buildDir}/intermediates/classes/${variant.dirName}")
...
}
A litt uncertain if that '+' is working. configurations.orm I think should make an impact.
In my case it related to an environment variable set incorrectly:
export LOCAL_LIB=D:\LIB
vs
export LOCAL_LIB="D:\LIB"
so, the quotes were the problem.

Resources