Override ~/.gradle/init.gradle repository location - java

Is there a way to change the location of the default repositories in a projects build.gradle? I'm using a company PC which has a ~/.gradle/init.gradle file that globally specifies the default gradle repos like:
allProjects {
buildscript {
repositories {
mavenLocal()
maven { url "<company url>"}
}
}
repositories {
mavenLocal()
maven { url "< company repo url>"}
}
}
I can only access this while on the company network which means for any side project the build fails if I don't log on. Is there a way I can override this repo in the project to point to a different (public) repo? I've tried various things like changing the project build.gradle to have:
repositories {
maven {
url "https://maven.springframwork.org/release"
}
}
But it still tries to point to the company repo and fails to resolve it.

If you need to add some repositories for dependencies - you can do it right in the build.gradle.
If you want to override Gradle plugin repositories, defined in global init script, only for a specific project(s), you need to:
Create an init script, which would be executed after init.gradle in USER_HOME/.gradle/ directory (for instance, init.gradle.kts in the USER_HOME/.gradle/init.d/ directory)
Use some if-condition to specify what projects will be covered with this override (for instance, in some specific folder). Gradle scripts are not only a DSL, you can use all power of Turing-complete Groovy/Kotlin.
I'll use Kotlin here (probably, it's a valid Groovy code too):
settingsEvaluated {
if (rootProject.projectDir.toString().startsWith("path/to/my/side/projects/folder")) {
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
}
}
}
}

Related

How to build and consume gradle plugin inside a multi-module project?

The big picture
I have a library which is used as a dependency in other projects. The library has some configuration requirements related to test suites for each project depending on it.
This is why I have created a plugin that configures those for me so that I could just add the plugin and be done with it:
plugins {
id("org.my.gradle.plugin") version "internal"
}
Project structure
+ root
+ my-api/... (self-sustained; doesn't depend on anything, pretty much interfaces)
+ my-implementation/... (depends on my-api and `my-gradle-plugin` via `plugins { id("org.my.plugin") }`)
+ my-gradle-plugin/... (the plugin itself)
+ build.gradle.kts
plugins {
`java-gradle-plugin`
`maven-publish`
}
gradlePlugin {
plugins {
create("org.my.gradle.plugin") {
id = "org.my.gradle.plugin"
group = "org.my.gradle.plugin"
implementationClass = "org.my.gradle.plugin.MyGradlePlugin"
version = project.version
}
}
}
+ build.gradle.kts // a bunch of shared task configurations and some repository configuration (mavenLocal, maven(xyz), mavenCentral())
+ gradle.properties - contains only "version=0.0.3-SNAPSHOT"
+ settings.gradle.kts
pluginManagement {
repositories {
mavenLocal()
maven { url = uri("https://xyz") }
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
val version: String by settings
eachPlugin {
if (requested.id.id == "org.my.gradle.plugin") {
useVersion(version)
}
}
}
}
The problem
The plugin works fine if it's a separate stand alone project. This, unfortunately, complicates my workflow so I thought I could possibly have it within the same multi-module project.
Unfortunately I am running into problems, because gradle doesn't seem to know it needs to build the plugin in order to use it inside one of the modules and therefore I get:
* Where:
Build file '/workspace/my-implementation/build.gradle.kts' line: 3
* What went wrong:
Plugin [id: 'org.my.gradle.plugin', version: '0.0.3-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.my.gradle.plugin:org.my.gradle.plugin.gradle.plugin:0.0.3-SNAPSHOT')
Searched in the following repositories:
MavenLocal(file:/home/jenkins/.m2/repository/)
maven(https://xyz)
Gradle Central Plugin Repository
MavenRepo
Why does it fail?
I know that spring-boot-gralde-plugin is part of the spring boot multi-module gradle project and is consumed within the project itself. Unfortunately I haven't been able to figure out what's the "magic trick" to make it work.
Any ideas what I'm missing here?
Well, this took me the better half of the day, but I finally figured it out.
In order to consume the plugin inside the same repository, you must place a settings.gradle.kts file inside your plugin. This separates the plugin build from the rest of your modules. Then you can use includeBuild() inside the pluginManagement to enforce the building of the plugin before everything else:
// ./root/settings.kotlin.kts
rootProject.name = "root-project"
include("my-api", "my-implementation")
pluginManagement {
includeBuild("my-gradle-plugin")
repositories {
mavenLocal()
maven { url = uri("https://xyz") }
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
val version: String by settings
eachPlugin {
if (requested.id.id == "org.my.gradle.plugin") {
useVersion(version)
}
}
}
}
Then you can just use the plugin without any issues:
plugins {
id("org.my.gradle.plugin") version "internal"
}
I'm using gradle 7.5+ so not sure if this works for older versions.
The docs also mention this here:
https://docs.gradle.org/7.6/userguide/composite_builds.html#included_plugin_builds

Java Dependency Bad Gateway 502 - fi.jasoft.plugin.vaadin 1.0.1 is missing

Inherited a project and am trying to run the build.gradle but the dependency is no long on maven... and I have googled and can't find any other active repos. There's a vaadin-spring 1.0.1 but I don't know if that's the same thing. Any body else run into this issue?
buildscript {
repositories {
//jcenter()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
classpath('fi.jasoft.plugin.vaadin:fi.jasoft.plugin.vaadin.gradle.plugin:1.0.1')
}
}
apply plugin: "fi.jasoft.plugin.vaadin"
The error:
Could not get resource 'http://dl.bintray.com/johndevs/maven/fi/jasoft/plugin/gradle-vaadin-plugin/1.0.1/gradle-vaadin-plugin-1.0.1.jar'.
> Could not HEAD 'http://dl.bintray.com/johndevs/maven/fi/jasoft/plugin/gradle-vaadin-plugin/1.0.1/gradle-vaadin-plugin-1.0.1.jar'. Received status code 502 from server: Bad Gateway
I tried to do a > build gradle and got the error. I have also tried importing a cache on a teammate that has it working but it does not recognize the cache I have imported by replacing my ~/.gradle with my teammates files
It looks like you've added a dependency on the wrong Maven coordinates.
Looking in the Gradle Plugin Portal I can see that the dependency should be classpath("fi.jasoft.plugin:gradle-vaadin-plugin:1.0.1")
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
// wrong coordinates:
// classpath('fi.jasoft.plugin.vaadin:fi.jasoft.plugin.vaadin.gradle.plugin:1.0.1')
// correct coordinates:
classpath("fi.jasoft.plugin:gradle-vaadin-plugin:1.0.1")
}
}
The coordinates you had used, fi.jasoft.plugin.vaadin:fi.jasoft.plugin.vaadin.gradle.plugin:1.0.1, do actually exist in the Gradle Plugin Portal Maven repo, but there's no JAR. Why is this?
The reason for this is that Gradle plugins require a marker artifact, so that Gradle can identify plugins using an ID in the plugins block DSL.
For this reason, I recommend you replace using the buildscript {} block to define plugins, and instead use the new plugins {} block.
plugins {
id "fi.jasoft.plugin.vaadin" version "1.0.1"
}

Gradle init not using custom repo when converting maven project

I am trying to convert a maven project to gradle. Our maven project uses a custom repository for dependencies, and we can not download anything from ordinary repos. When running command
gradle init -I ./init.gradle
I am getting errors and I can see on the command line that it is trying to connect to the https://repo.maven.apache.org/maven2. I have tried using the init.gradle script from userhome and gradlehome, but there was no change. I also added the custom repo to pom.xml since I saw someone say gradle uses those if available, but it didn't. This is my init.gradle for reference:
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.settingsEvaluated { settings ->
settings.pluginManagement {
repositories {
maven {
url "custom_repository_url"
}
}
}
}
}
}
I also tried this at first:
allprojects {
buildscript {
repositories {
mavenLocal()
maven { url "custom_repository_url"}
}
}
repositories {
mavenLocal()
maven {
url "custom_repository_url"
}
}
}
I have also tried adding a build.gradle with the following task and running it:
tasks.register('showRepos') {
doLast {
println "All repos:"
println repositories.collect { it.name }
}
}
This only printed out the custom repo. However deleting this and running gradle init still connects to the https://repo.maven.apache.org/maven2.
Edit:
It seems this question won't be solved. In case you found this when looking for an answer, I have found this solution that might work for you. I wasn't so lucky.
https://github.com/gradle/gradle/issues/19884
I have also crated an issue on github. Maybe there will be an answer there.
https://github.com/gradle/gradle-native/issues/1097

How to create my own android library and host it

I'm working on creating a log-in screen to be used with multiple different android applications. What would be the best way to package it so that other people could use my log-in function on their apps. It would be preferred that it would auto-sync for them in-case we were to make changes.
***EDIT****
It seems packaging it into a library module is the best option. How does one go about uploading this module so that if we make an update to this module it will seamlessly update without having to pull from github for example.
Thanks!
If you've pushed your code to GitHub then sharing the library (aar) is easy with JitPack.
Your users will just need to add the repository to their build.gradle:
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
and then your GitHub repository as dependency:
dependencies {
// ...
compile 'com.github.YourUsername:Repo:Release'
}
The nice thing is that you don't have to upload your library. Behind the scenes JitPack will check out the code from GitHub and compile it. As you publish a new release on GitHub it becomes available for others to use.
There is also a guide on how to prepare an Android project.
Make the relevant classes into a library module - you already seem to know how to do that - and then use the Gradle Bintray plugin to upload it to JCenter.
Let's say you set group in build.gradle to com.ryan-newsom, version to 1.0 and the project name is android-log-in-screen.
(part of) android-log-in-screen/build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6"
}
}
apply plugin: 'com.jfrog.bintray'
group = 'com.ryan-newsom'
version = '1.0'
bintray {
// Omitted for brevity, refer to the examples on GitHub.
}
You (or anyone else) can then use it in your project by adding the following:
(part of) other-project/build.gradle:
repositories {
jcenter()
}
dependencies {
compile "com.ryan-newsom:android-log-in-screen:1.0"
}
The library will then be pulled from JCenter and added to the classpath.
You can package the library into an AAR format. It will also contain the resources you used in your login module. After that you can push the AAR library format to bintray (which is free, and allows you to setup your own repository).
Your collaborators can then access the library using a dependency that looks like:
compile 'com.newsom:awesome-login-screen:0.5'
Check this starter tutorial if you are using AndroidStudio/Gradle and would like to push it to bintray. https://github.com/jimcoven/android-bintray-kit
The best way to create a lib and make it available to other developers is creating a AAR so that developers can import it in their project using
dependencies.
The process is quite long.
These are the main steps you should follow to publish your lib:
Register an account and create a new ticket
(https://issues.sonatype.org)
Download (if you use OS X) GPGTools
(http://www.gpgtools.org/)
Modify project gradle files
Create signing key Build
sign and publish your files to the Staging repository
I wrote a post about it and to have more details you can look here.
This is a piece of gradle file called maven_push.gradle:
apply plugin: 'maven'
apply plugin: 'signing'
def sonatypeRepositoryUrl
if (isReleaseBuild()) {
println 'RELEASE BUILD
sonatypeRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
: "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
println 'SNAPSHOT BUILD'
sonatypeRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
: "https://oss.sonatype.org/content/repositories/snapshots/"
}
def getRepositoryUsername() {
return hasProperty('nexusUsername') ? nexusUsername : ""
}
def getRepositoryPassword() {
return hasProperty('nexusPassword') ? nexusPassword : ""
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
pom.artifactId = POM_ARTIFACT_ID
repository(url: sonatypeRepositoryUrl) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}
}
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.allJava
classpath += project.files(android.plugin.getRuntimeJarList().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
//basename = artifact_id
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
//basename = artifact_id
from android.sourceSets.main.allSource
}
artifacts {
//archives packageReleaseJar
archives androidSourcesJar
archives androidJavadocsJar
}
}
while gradle.properties is:
VERSION_NAME=
VERSION_CODE=1
GROUP=
POM_DESCRIPTION=
POM_URL=
POM_SCM_URL= POM_SCM_CONNECTION=
POM_SCM_DEV_CONNECTION=scm:git#github.com:
POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=
POM_DEVELOPER_NAME=
There is another way but i did not try it and it seems to be easier.
Give a look at jitpack.
Hope it helps you.
make the package or jar depending on your source, and post it on git hub the you can refer to the git from your ide to import or check for updates.

Gradle dependency resolution

I have an issue with how gradle resolves my dependencies.
I have four repositories that I need to investigate for different jars, five counting Maven central. Thus my repo statment in gradle.build looks like this:
repositories {
maven {
url 'urltoRepoA'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoB'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoC'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoD'
artifactUrls mavenLocal()
}
mavenCentral()
}
What I want to acheive:
Look for dependencies both in the remote repositories and the local maven repository.
But I get this error below, that is a jar that should be resolved from repoA (repoA is a mirror of maven central, and I have verified that this jar can be found there)
[16:43:10][Step 1/3] > Could not resolve all dependencies for configuration ':runtime'.
[16:43:10][Step 1/3] > Artifact 'junit:junit:4.11#jar' not found.
According to what I've read in gradles manual is that it tries to resolve all the dependencies from the same repo. Is that what I'm running in to here? Or have I failed to configure gradle properly?
I suspect there is something wrong elsewhere in your gradle configuration. I think you are misunderstanding how gradle resolves artifacts.
According to the gradle docs (see section 8.5)
A project can have multiple repositories. Gradle will look for a
dependency in each repository in the order they are specified,
stopping at the first repository that contains the requested module.
In fact, it's rather common to have multiple repositories in a gradle script.

Categories

Resources