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
Related
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"
}
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()
}
}
}
}
I'm looking for gradle to create a clean pom with just the bare essentials like dependencies so I can upload it along the the jar, sources.jar, and javadoc.jar.
I also don't want to have to manually create the pom.
Have a look at publishing, in particular with the maven-publish plugin, which handles this for you indeed.
But in order to have the minimal publication, this is a simple as:
plugins {
`java`
`maven-publish`
}
group = "org.example"
version = "1.0"
// dependencies declaration omitted
publishing {
publications {
create<MavenPublication>("myLibrary") {
from(components["java"])
}
}
repositories {
maven {
name = "myRepo"
url = uri("file://${buildDir}/repo")
}
}
}
Note: This uses the Kotlin DSL, the Groovy version has a couple differences, see documentation
And then running ./gradlew publish will publish org.example:<project-name>:1.0
I am creating gradle plugin which has dependency on my other local module. Some of its gradle build look like this:
dependencies {
compile gradleApi()
compile project(":myDependencyProject")
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.my.gradle.plugin'
artifactId = 'some-name'
version = '1.0-SNAPSHOT'
from components.java
}
}
}
gradlePlugin {
plugins {
jsonPlugin {
id = 'org.my.gradle.plugin'
implementationClass = 'my.implementation.class'
}
}
}
When I publish my plugin using gradle publishToMavenLocal and after that I try to use that plugin in another project it fails with this error:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':my-project'.
> Could not resolve all artifacts for configuration ':my-project:classpath'.
> Could not find org.my.gradle.plugin:myDependencyProject:1.0-SNAPSHOT.
Searched in the following locations: ...
In simple words it could not find dependency for myDependencyProject project. That is why as a next step I tried to create a fat jar and publish it but I have got the same error (the code for gradle plugin was same except I have changed from components java to artifact shadowJar).
Can someone help me how can I publish gradle plugin with its local dependencies and use it in another project ?
Thank you very much for any help.
We ended up using the Gradle shadow plugin to include our module in the published artifact.
One thing that was important to us though, was to only include the local library in it to prevent our end consumer from having 2 copies of some library (such as Kotlin). So we filtered the dependencies
shadowJar {
dependencies {
include(dependency(':your-module-name:'))
}
}
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.