Unable to apply plugin in gradle init script - gradle

Here is my gradle init script.
initscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.5.1")
}
}
allprojects {
apply(plugin="java")
apply(plugin="application")
apply(plugin="org.springframework.boot.gradle.plugin.SpringBootPlugin")
repositories {
gradlePluginPortal()
mavenCentral()
}
}
However, I am getting Plugin with id 'org.springframework.boot.gradle.plugin.SpringBootPlugin' not found. error.
I have tried the solutions from the following questions but none of them solves the problem.
Add Android plugin to gradle init script

I have found my solution after googling around.
In order to apply plugins in gradle init script that is written in kotlin DSL,
one has to use the following
initscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.5.1")
}
}
allprojects {
apply(plugin="java")
apply(plugin="application")
apply<org.springframework.boot.gradle.plugin.SpringBootPlugin>()
repositories {
gradlePluginPortal()
mavenCentral()
}
}
In order to apply third party plugins in the init script, one has to use the form apply<path.to.classname>()

Related

Add dependency and plugin in seperate file using "apply from" in gradle

My build.gradle inside app directory contains:
apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
allprojects {
repositories {
mavenCentral();
jcenter()
}
}
def hasBuildExtras = file('build-extras.gradle').exists()
if (hasBuildExtras) {
apply from: 'build-extras.gradle'
}
And this is my build-extras.gradle file:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.github.triplet.gradle:play-publisher:1.2.2'
}
}
apply plugin: 'com.github.triplet.play'
Am I correct to assume that build-extras.gradle should be "merged" inside original file? No matter how I move it around I get Error:Plugin with id 'com.github.triplet.play' not found.
If i move classpath 'com.github.triplet.gradle:play-publisher:1.2.2' up to the main file and leave just apply plugin: 'com.github.triplet.play' inside build-extras.gradle then it seems to work fine. So am I wrongly defining dependencies?
A build.gradle file can only ever have one buildscript block. You are correct with the paragraph explanation. Add the plugin dependency to your main build file's dependencies block within the buildscript, then conditionally apply the plugin using whatever logic you'd like.

Import buildscripts in gradle

I am pretty new with gradle, I want to include a plugin globally, and I don't know how.
In my project(wish I DO NOT OWN), there are multiple build.gradle, and there is a commons.gradle folder. In there is created a script like this
myscript.gradle
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.13.1"
}
}
Then in of the build.gradle I have this:
buildscript {
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
apply from: "$path-to/myscript.gradle", to: buildscript
}
apply plugin: "com.github.hierynomus.license"
but I keep getting a Plugin with id com.github.hierynomus.license" not found. I am using Gradle 3.1. any help or hint would greatly help.
Thanks
I was able to make this work by doing the following:
myScript.gradle
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.13.1"
}
}
then in the build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
apply from: "$path-to/myScript.gradle", to: buildscript
}
plugins{id "com.github.hierynomus.license" version "0.13.1"}
apply plugin: "com.github.hierynomus.license"
I was not able to find anything online, so I hope this will help someone one day.
ps: Sorry for my bad English

Plugin with id 'com.wonbin.myplugin' not found

I wrote a demo for learning gradle for android . i used the JAR
1) project root dir build.gradle
buildscript {
repositories {
jcenter()
flatDir {dirs 'build_libs'}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.wonbin.myplugin:MyPlugin:1.0'
}
}
apply plugin: 'com.wonbin.myplugin'
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I have leanrd that the plugin id is the properties filename in the plugin project. But still when I run ./gradlew assemble , it happens:
Build file '/home/wonbin/MyApp/build.gradle' line: 13
What went wrong: A problem occurred evaluating root project 'MyApp'.
Plugin with id 'com.wonbin.myplugin' not found.
what should i do ?
Probably your build_dir doesn't correspond to the correct file structure. You can reference your jar in the classpath directly instead, like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath file('/path/to/your/plugin.jar')
}
}
apply plugin: 'com.wonbin.myplugin'

Gradle Custom buildScriptRepository methods

I would like to make a custom buildScript repository method so I can easily reference our internal maven repo. Right now I'm required to declare a maven block everywhere we use our plugin.
Here is the current setup
buildscript {
repositories {
jcenter()
maven { url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
What I would like to do is something like this
buildscript {
repositories {
jcenter()
myReleaseRepo()
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
How can I make a method available to create a repository anywhere we may use the plugin in the future?
Another solution is to add custom methods on RepositoryHandler using some Groovy goodness. Just chuck this in ~/.gradle/init.gradle
RepositoryHandler.metaClass.myReleaseRepo = {
delegate.maven {
name 'myReleaseRepo'
url 'http://myNexus:8081/nexus/content/repositories/My-Release'
}
}
After that, you can use it just as you described:
buildscript {
repositories {
myReleaseRepo()
}
}
Metaclasses in Groovy are just great. The delegate in this case is pretty much like the javascript this. This code is essentially using the RepositoryHandler instance (delegate keyword) and just calling repositoryHandlerInstance.maven(name, url).
It is possible to add a repo to an init script which would then apply to all gradle invocations that use the init script - without having to individually declare your maven repo in each build.gradle.
Solution 1:
Partial solution, does not do exactly what you're asking for. In init.gradle:
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
Then your build.gradle can skip buildscript repo declaration entirely:
buildscript {
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
Solution 2:
In fact, you can even move your buildscript classpath declaration to init and have the plugin apply to all projects that use the init script:
beefier init.gradle
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
}
gives you a lighter build.gradle
apply plugin: 'my-plugin'
I tried to, but apparently you cannot move the apply line to init.gradle as well. see this defect.
Solution 3:
I retract what I said in the comment above, I figured out how to do exactly what you're asking for. Apparently you can create extensions for the buildscript block using the initscript. However I still prefer solution2, because it gives you a cleaner build.gradle.
To create a buildscript extension, in your init.gradle:
class customRepos {
def buildscript
customRepos(buildscript) {
this.buildscript = buildscript
}
void addMyRepo() {
buildscript.repositories {
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
allprojects {
extensions.create('myRepo', customRepos, buildscript)
}
which then allows you to do this in your build.gradle
buildscript{
myRepo.addMyRepo()
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
apply plugin: 'my-plugin'

Publish Java artifact to Maven Local with Gradle

I am facing a problem when trying to install a generated jar into my local Maven Repository. The message error just show me 'task 'publish' is not found'
I am using this Gradle Script:
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'
jar {
baseName = 'mongofoundry'
version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Do you have some idea Why I am reading that error message?
Thanks.
UPDATED
Running the command as #RaGe mentioned, solved the problem:
gradle publishToMavenLocal
The correct task to publish artifacts to local maven is
gradle publishToMavenLocal
Check Maven locally
For developing and testing it is useful to check library locally
gradle settings for apply plugin: 'com.android.library' not apply plugin: 'java-library'(where you can use it by default)
apply plugin: 'maven-publish'
//simple settings
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
//setGroupId groupId
setGroupId "com.company"
//setArtifactId artifactId
setArtifactId "HelloWorld"
version "1.1"
artifact bundleDebugAar
/* add a dependency into generated .pom file
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'com.company')
dependencyNode.appendNode('artifactId', 'HelloWorld-core')
dependencyNode.appendNode('version', '1.1')
}
*/
}
}
}
}
to run it using command line or find this command in Gradle tab
./gradlew publishToMavenLocal
Location
artefact will be added into .m2 folder
//Unix
~/.m2
//Windows
C:\Users\<username>\.m2
//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>
build folder
<project_path>/build/outputs/<extension>
other repositories location
~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>
//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a
To use it add mavenLocal(). It is important to place it as a first item for prioritise it, which is useful for internal dependencies
buildscript {
repositories {
mavenLocal()
}
allprojects {
repositories {
mavenLocal()
}
}
and
dependencies {
implementation 'com.company:HelloWorld:+'
}
*Also remember if you use a kind of shared.gradle file (via apply from) you should set path which is relevant to project.gradle (not shared.gradle)
[iOS CocoaPod local]
This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:
plugins {
id("maven-publish")
// OR simply
// `maven-publish`
// ...
}
publishing {
repositories {
// Local repository which we can first publish in it to check artifacts
maven {
name = "LocalTestRepo"
url = uri("file://${buildDir}/local-repository")
}
}
publications {
// ...
}
}
You can create all the publications with the following command:
./gradlew publishAllPublicationsToLocalTestRepoRepository
Or just a single publication with this command:
./gradlew publishReleasePublicationToLocalTestRepoRepository
See Gradle documentations: Maven Publish Plugin for more information.
Add maven plugin to your project and then:
gradle clean install
Here is an alternative skeleton for Gradle 7.5.1 with Java 17
build.gradle
plugins {
id 'org.gradle.java'
id 'org.gradle.maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
withJavadocJar()
withSourcesJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'your-group'
artifactId = 'your-artifact'
version = "0.0.1"
from components.java
}
}
repositories {
mavenLocal()
}
}
Publishing
You can see more details on the publishing steps with --info
./gradlew --info publishToMavenLocal
Output Directory
Linux/macOS
/Users/<username>/.m2/repository/your-group/your-artifact/0.0.1
Windows
C:\Users\<username>\.m2\repository\your-group\your-artifact\0.0.1

Resources