Best way to reference a file path with Gitlab, Gradle and Bash? - gradle

I have a bash script in my Gitlab pipeline that execute a gradle command. Within that gradle command, I reference a file. I keep getting issues with the file not being found and I assume it's because of how the relative pathing is working.
Snippet of Gradle Command
path {
from = file("folder")
into = '/targetFolder/'
}
Shell Script
gradle :${APP_PATH}:jib
I've tired projectDir and I've tried relative path like ../../etc, but no luck. What's the best way to reference the file directly? It's underneath rootproject/config/folder

it appears that you are trying to call sub-project target when you call below in the Shell script gradle :${APP_PATH}:jib
as you mentioned you need config folder relative at the root-project
Use paths from rootDir of rootProject.
files(project.rootProject.rootDir + '/config/folder')

Related

mavenLocal() functionality alternatives

mavenLocal() currently serve my need by searching at my local m2 for my dev builds.
Is there other way to tell gradle to search for local m2 without changing build.gradle or other source code file?
You can use Initialization Scripts.
There are several ways to use an init script:
Specify a file on the command line. The command line option is -I or
--init-script followed by the path to the script. The command line option can appear more than once, each time adding another init
script.
Put a file called init.gradle in the USER_HOME/.gradle/ directory.
Put a file that ends with .gradle in the USER_HOME/.gradle/init.d/
directory.
Put a file that ends with .gradle in the GRADLE_HOME/init.d/
directory, in the Gradle distribution. This allows you to package up a
custom Gradle distribution containing some custom build logic and
plugins. You can combine this with the Gradle wrapper as a way to make
custom logic available to all builds in your enterprise.
Example init script
initscript {
repositories {
mavenLocal()
}
}
Check out docs for details.

nohup: failed to run command `sh`: No such file or directory

I have the following pipeline script in Jenkins:
node {
withMaven(globalMavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml', jdk: 'JDK 1.8.0u92', maven: 'apache-maven-3.2.2', mavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml') {
sh '/my/path/apache-maven-3.2.2/bin/mvn clean install'
}
}
For this, I am getting:
nohup: failed to run command `sh`: No such file or directory
ERROR: script returned exit code -2
Why is this?
I am sure that the path to my Maven installation is correct. When I run a job without the pipeline, Maven builds with no errors and I can see that it uses the same path.
This might be the result of modifying PATH.
Check your script and Global Properties and remove modifications to PATH. It is now recommended to use PATH+extra instead. It would still be picked up, but without breaking actual PATH.
Related issue on Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-41339
In the end, I used shell instead of sh and it worked. No idea why, they don't have a proper API.
I would suggest to use it like this:
withMaven(
maven: 'M3',
mavenSettingsConfig: 'maven-settings-for-the-task',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
}
Apart from that I would not use absolute paths to global settings.xml nor to user settings.xml. I would prefer using usage of "Config File Provider Plugin" which has the advantage to have the settings.xml on Master and available on all nodes.
See also the documentation: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Maven+Plugin
This error comes when you are trying to run script copied from windows machine to unix machine.
YOU need to change the format to unix using : dos2unix <scriptname.sh> and the run your script in unix ./<scriptname.sh>

Executing gradle scripts from external directories

I need to execute groovy script with gradle, but the thing is that these scripts are located in external directory (let's say it's Desktop). I've heard, that in previous versions of Gradle (currently working on 3.2.1) it was not possible, since it is not part of the gradle project. I wonder if it is possible now, or do I have to copy all these scripts into some folder located in gradle project, to execute it.
User story:
I found script in external directory (based on some properties passed to console) - I have absolute path to the script
Now I want to execute this script (build.gradle) without copying it into my gradle project
I'm pretty green with gradle and I hope I made my point clear.
Thanks for help and every attempt :)
Is that what you're looking for? To run the script clone the repository, navigate to 42556631/project folder and run the command:
gradle -b ../script/build.gradle clean build
yes you need to move build.gradle file into project/Build Script folder and then run it.

How to get maven repo location in pom file

I have a maven goal which will execute a groovy file. This groovy file uses a path which is like below
System.getProperty("user.home");
This root path is used to build the another path which points to a jar file inside the local repository.
Unfortunately, I don't have permissions in C drive and I am using D drive. So my repository is also in D drive.
Because of this my build failed. To pass my build I am passing a command line arguments to maven like below
mvn clean install -Duser.home=D:\users\krishna
I want to know if there is a pom variable/place-holder something like {m2RepoHome} so that I can use the same variable in my groovy file.
Thanks in advance
Maven provides a convenient place-holder settings.X where X is any element in settings.xml file.
I used ${settings.localRepository} and it worked.
Properties section in https://maven.apache.org/settings.html explains how to use it.

install4j how to specify install dir on Windows Gradle

I have trouble using install4j on Windows for the project tripleA.
See source at: https://github.com/gaborbernat/triplea/blob/installer/build.gradle
I'm using the Gradle Plugin however I'm unable to specify the install 4j home directory.
If I try using absolute path that is 'C:/Program Files/install4j6' i get a normalization error as it gets translated to:
"C:/data/tripleA/'C:/Program Files/install4j6'"
what am I missing here? why is file('C:/Program Files/install4j6') referring to "C:/data/tripleA/'C:/Program Files/install4j6'"?
Now if I use a relative path it says the folder does not exists.
Thanks,
Due to your build.gradle, the path is retrieved from properties file. It seems, that this path in your properties is placed within single quotes, as 'C:/Program Files/install4j6', and your gradle script logic get the value with this quotes.
That is the reason, file() doesn't recognize it as absolute path and tries to parse it as relative. Here is some code, which reproduces your exception:
task testFile {
File ff = file('\'d:/test.xml\'')
println ff.absolutePath
}
This will cause an error
Could not normalize path for file 'D:\path\to\your\project\'d:\test.xml''
So, you should try to change the property, to make it a plain string without quotes.

Resources