mavenLocal() functionality alternatives - gradle

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.

Related

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

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')

Downloaded path for Gradle comple statement [duplicate]

How does Gradle store downloaded jar files on the local file system? Maven stores them in the .m2 directory under USER_HOME, but where does Gradle store them? I checked the .gradle folder there, but saw only compiled scripts.
On Mac, Linux and Windows i.e. on all 3 of the major platforms, Gradle stores dependencies at:
~/.gradle/caches/modules-2/files-2.1
Gradle caches artifacts in USER_HOME/.gradle folder. The compiled scripts are usually in the .gradle folder in your project folder.
If you can't find the cache, maybe it's because you have not cached any artifacts yet. You can always see where Gradle has cached artifacts with a simple script:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:12.0'
}
task showMeCache doLast() {
configurations.compileClasspath.each { println it }
}
Now if you run gradle showMeCache it should download the dependencies into the cache and print the full path.
In Windows 10 PC, it is saved at:
C:\Users\%USERNAME%\.gradle\caches\modules-2\files-2.1\
Gradle's local repository folder is:
$USER_HOME/.gradle/caches/modules-2/files-2.1
Defined dependencies will be loaded from remote repositories into gradle's local repository folder. For each loaded file, gradle will be create a new folder named with md5 value of the original file (pom,jar,..). Full path for the dependency file is made up from :
groupid + artifactid + version + FILE_MD5_VALUE + FILE_NAME
If our defined dependency is:
compile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
Then the library will be loaded into :
/$USER_HOME/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jdbc/4.3.4.RELEASE/42175d194cf6aa7c716c0887f30255e5c0a5262c/spring-jdbc-4.3.4.RELEASE.jar
In fact the cache location depends on the GRADLE_USER_HOME environment variable value.
By default, it is $USER_HOME/.gradle on Unix-OS based and %userprofile%.\gradle on Windows.
But if you set this variable, the cache directory would be located from this path.
And whatever the case, you should dig into caches\modules-2\files-2.1 to find the dependencies.
If you want your dependency files to be in some specific folder you can simply use a copy task for it. For Eg.
task copyDepJars(type: Copy) {
from configurations.compile
into 'C:\\Users\\athakur\\Desktop\\lib'
}
I am on windows,
You should be able find the dependencies inside
$USER_HOME.gradle\caches\artifacts-24\filestore
Many answers are correct!
I want to add that you can easily find your download location with
gradle --info build
like described in https://stackoverflow.com/a/54000767/4471199.
New downloaded artifacts will be shown in stdout:
Downloading https://plugins.gradle.org/m2/org/springframework/boot/spring-boot-parent/2.1.7.RELEASE/spring-boot-parent-2.1.7.RELEASE.pom to /tmp/gradle_download551283009937119777bin
In this case, I used the docker image gradle:5.6.2-jdk12.
As you can see, the docker container uses /tmp as download location.
You can use the gradle argument --project-cache-dir "/Users/whatever/.gradle/" to force the gradle cache directory.
In this way you can be darn sure you know what directory is being used (as well as create different caches for different projects)
I just stumbled onto this while searching for this answer. If you are using intellij, you can navigate to the file location, but opening the external lib folder in the project explorer, right clicking on the jar, and select Open Library Settings.
It took me a while to realize this, hence the additional answer. Hopefully it can save folks time. Note that if you are running sudo gradle the dependencies may not be in your home directory, even if sudo echo $HOME returns /Users/<my-non-root-user>/. On my Mac, Gradle was caching the dependencies in /private/var/root/.gradle/caches/.
In case it is an Android gradle project - you can find the android libraries below your $ANDROID_HOME/extras/android/m2repository folder
In android studio do the following steps to check the gradle downloaded jar file.
Set project structure view to "Project"
At bottom External library section available, expand it.
Here you can see downloaded jar files.
On my windows machine with "Buildship 2.0.2" plugin installed in eclipse, dependencies are stored :
$USER_HOME.gradle\caches\modules-2\files-2.1
For my case, I was using an Ivy repository, and my Gradle dependencies were stored in ~/.ivy2/.

How to utilize buildSrc directory with gradle?

In the gradle documentation says:
Builds which utilize a buildSrc directory will generate a second
profile report for buildSrc in the buildSrc/build directory.
How can we do that (utilize build/Src) via the gradle sript, couldn't you help me?
You may put your helper scripts/classes to various places. One of them is buildSrc directory.
See below quote from gradle documentation.
When you run Gradle, it checks for the existence of a directory called
buildSrc. Gradle then automatically compiles and tests this code and
puts it in the classpath of your build script. You don't need to
provide any further instruction. This can be a good place to add your
custom tasks and plugins.
Your qoute only tells that if you use buildSrc directory, you will have second profile report.

how to handle more then one init.gradle file

In Gradle 2.0 documentation
there are some options to provide the 'init.gradle' file
command-line
init.gradle file in USER_HOME/.gradle/ folder
init.gradle file in USER_HOME/.gradle/init.d/ folder
init.gradle file in USER_HOME/init.d/ folder
and 'If more than one init script is found they will all be executed'.
My use case is that we have some project that uses old Gradle version(1.7) and the syntax is not supported in Gradle 2.0 for example.
We usually provide repos info in the init.gradle file.
How can I provide only one init.gradle file to my project build?
Or how to disable execution of all init.gradle files (and I will provide all the given parameters inside my build file)?

How to load a specific build.gradle/gradle.properties for default build process

I have three build.gradle with different name under the same directory
dev.build.gradle
uat.build.gradle
prd.build.gradle
I have 4 issues
"gradle build" will just use build.gradle only to start the java plugin build task, but "gradle -b dev.build.gradle" will not start the java plugin build task
gradle --help seems not having an option to load a specific gradle.properties. There is another way that creating three directories(dev, uat, prd) under the project root and putting a responding build.gradle version in it. finally, start the java plugin build process. I dont like this because I just want build.gradle or gradle.properties files in the same directory
how to copy files in gradle without explicitly specify task name in the command line(gradle build copy).
ad 1. The correct command is gradle -b dev.build.gradle build.
ad 2. If you want to use properties files other than build.gradle, you'll have to do it on your own (e.g. using the java.util.Properties class). There is also a third-party properties plugin.
ad 3. This doesn't seem to be a question.
ad 4. You should turn this into a separate question.

Resources