How can I apply Gradle plugin from directly GitHub repo - github

I tried to apply plugin directly from GitHub repo e.g. as given below
apply from 'https://github.com/gradle/gradle-hello-world-plugin' but on running
./gradlew codeLines
It gives me error as
* Where:
Script 'https://github.com/SurpSG/code-lines-counter-gradle-plugin' line: 7
* What went wrong:
Could not compile script 'https://github.com/SurpSG/code-lines-counter-gradle-plugin'.
> startup failed:
script 'https://github.com/SurpSG/code-lines-counter-gradle-plugin': 7: unexpected token: < # line 7, column 1.
<!DOCTYPE html>
^
How can I directly apply plugin from remote git repo?

The plugin is automatically published to jitpack.
You could apply the plugin in the next way:
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.github.SurpSG:code-lines-counter-gradle-plugin:0.0.1'
}
}
apply plugin: 'com.github.code-lines'
Note! code-lines-counter-gradle-plugin is sample project created as a part of tutorial.
I don't recommend to use it because it far from production ready

I don't think it's possible. The closest think you can do is clone the plugin, build it and point to it
buildscript {
repositories {
[...]
}
dependencies {
classpath files('relative/path/to/gradle-hello-world-plugin.jar')
}
}
apply plugin: org.gradle.plugin.HelloWorldPlugin
Source : https://stackoverflow.com/a/35472676/2003986

Related

Kotlin setup via gradle on eclipse

Struggling to get Kotlin running on eclipse.
I've started new graddle project. Added dependencies as prescribed on kotlin's site.
Build passes without errors.
I've created 'main.kt' file under src/java/main with:
fun main(args: Array<String>) {
println("foo")
}
BUT, I have two problems:
1. anything from kotlin e.g. println highlighted as 'unresolved reference'.
2. I can't run a program - Error: Could not find or load main class MainKt (rightclick on main.kr run as 'kotlin application')
If I create 'new kotlin project' everything works.
my graddle build script:
plugins {
id "org.jetbrains.kotlin.jvm" version "1.1.2-2"
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
//api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-2"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8"
compile "org.jetbrains.kotlin:kotlin-reflect"
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
sourceSets {
main.java.srcDirs = ['src/main/java']
main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
main.resources.srcDirs = ['src/main/resources']
}
What did I do wrong?
I've zero Java knowledge if that helps, so probably I've made some trivial error.
UPDATE:
Installed a Spring plugin and generated a new web app via it including gradle.
But Kotlin behaves unpredictably there too.
At first I was not able to run it as run as Kotlin application and it errored with main could not be found, BUT sometimes it run and crashed immediately. It started to launch and crash after I've deleted and edited classes, tried creating it under other package, removing and adding Kotlin (I can't reproduce sequence to make it work again).
Fun part that gradle boot build launches everything and all works it somehow finds Kotlin's main.
Probably some issue with Kotlin plugin itself (it's load probably depends on certain events that doesn't always fire)
Add the following to your configuration:
apply plugin: 'eclipse'
eclipse {
classpath {
containers 'org.jetbrains.kotlin.core.KOTLIN_CONTAINER'
}
}
See https://gitlab.com/frnck/kotlin-gradle-eclipse for a working configuration.
I'd like to add to frnck answer that this is only part of the solution. I also had to add these lines:
eclipse.project {
buildCommand 'org.jetbrains.kotlin.ui.kotlinBuilder'
natures 'org.jetbrains.kotlin.core.kotlinNature'
natures 'org.eclipse.jdt.core.javanature'
linkedResource name: 'kotlin_bin', type: '2', locationUri: 'org.jetbrains.kotlin.core.filesystem:/aio/kotlin_bin'
}
For Eclipse 2018-12 and kotlin 1.3 the solution was a combination of other answers plus some additional settings file:
eclipse {
classpath {
//Adds the kotlin container to the classpath
containers 'org.jetbrains.kotlin.core.KOTLIN_CONTAINER'
//Fixes the right output path
defaultOutputDir = file('bin')
//Make all src folders output in the same output folder (default)
file {
whenMerged {
// use default Output for all source-folders. see also defaultOutputDir per project
entries.each { source ->
// only Source-folders in the project starting with '/' are project-references
if (source.kind == 'src' && !source.path.startsWith('/')) {
source.output = null
}
}
}
}
}
project{
buildCommand 'org.jetbrains.kotlin.ui.kotlinBuilder'
//Fixes the natures
natures 'org.jetbrains.kotlin.core.kotlinNature'
natures 'org.eclipse.jdt.core.javanature'
//Links the kotlin_bin folder (generated class files)
linkedResource name: 'kotlin_bin', type: '2', locationUri: "org.jetbrains.kotlin.core.filesystem:/${project.name}/kotlin_bin".toString()
file{
whenMerged{
def kotlinPrefs = file('.settings/org.jetbrains.kotlin.core.prefs')
def jdkHome = System.properties.'java.home'
if(!(jdkHome)){
throw new GradleException('No JDK home available for setting up Eclipse Kotlin plugin, setup env "java.home" or update this script.')
}
kotlinPrefs.write """\
codeStyle/codeStyleId=KOTLIN_OFFICIAL
codeStyle/globalsOverridden=true
compilerPlugins/jpa/active=true
compilerPlugins/no-arg/active=true
compilerPlugins/spring/active=true
eclipse.preferences.version=1
globalsOverridden=true
jdkHome=$jdkHome
""".stripIndent()
}
}
}
}
I would like to add to Felipe Nascimento's answer that the location of the .settings folder does not yet exist. It works when the line below is inserted into that answer.
def kotlinPrefs = file("../${project.name}/.settings/org.jetbrains.kotlin.core.prefs".toString())
I have found that the JAVA_HOME environment variable that is set when your run this task ;
gradle cleanEclipse eclipse
is the one that is included in the Eclipse BuildPath

Gradle Artifactory plugin error

My gradle version is 2.2.1.
I am trying to use the artifactory plugin in gradle (apply plugin: 'artifactory')
This is my code:
buildscript {
repositories {
maven {
url 'http://172.14.3.93/artifactory/plugins-release'
}
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.9')
}
}
allprojects {
apply plugin: 'artifactory'
}
artifactory {
contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'libs-release-local'
maven = true
}
}
resolve {
repository {
repoKey = 'libs-release'
maven = true
}
}
}
When i run gradle build, this is what i get:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Workspace\GradleConfiguration\build.gradle' line: 100
* What went wrong:
A problem occurred evaluating root project 'GradleConfiguration'.
> Failed to apply plugin [id 'artifactory']
> Could not find method add() for arguments [artifactoryPublish, class org.jfrog.gradle.plugin.artifactory.extractor.BuildInfoTask] on task set.
* Try:
Run with --info or --debug option to get more log output.
Also, when i delete all the code that is related to the artifactory and leave only apply plugin: 'artifactory', i get this error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\OnRights_Workspace\GradleConfiguration\build.gradle' line: 86
* What went wrong:
A problem occurred evaluating root project 'GradleConfiguration'.
> Failed to apply plugin [id 'artifactory']
> Plugin with id 'artifactory' not found.
* Try:
Run with --info or --debug option to get more log output.
Your plugin version is too old. Try to use the latest (3.1.1).
The buildscript block tells Gradle where it should be looking for plugins. You've told it that it should only search at http://172.14.3.93/artifactory/plugins-release.
Are you 100% sure that the "artifactory" plugin is actually hosted there?
If you put jcenter in there, like the instructions say, does it work better?
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.0"
}
}
apply plugin: "com.jfrog.artifactory"

gradle configure plugin default during execution phase

I am new to gradle. I am trying to use a plugin but I want to change the default values of a plugin during execution
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.netflix.nebula:gradle-ospackage-plugin:2.0.3'
}
}
apply plugin: 'os-package-base'
Setting plugin defaults:
def version_local = version //project.version
ospackage {
version = version_local
.......
.......
}
I am using another plugin(gradle-release) which changes the version of an artifact and the value of it is present in project.version. The tasks of this release plugin executes before the buildRpm task of this plugin(ospackage)
I want to use the value of the version present in project.version during execution. But looks like the value is being set prior to execution of any tasks. Any ideas how?
This piece of code seems to do it.
buildRpm.doFirst {
version = project.version
}
Explanation: doFirst is an action which only gets run during execution phase of gradle, right before running buildRpm.

Gradle Api Sources and Doc when writing Gradle Plugins

The question:
How do I get the sources and javadoc/groovydoc for the gradle-api code integrated into an Eclipse project?
Background:
I'm using Gradle to build a Gradle-plugin that I'm writing. I'm using Eclipse as an IDE for this project and my Gradle script for building this plugin is using the 'Eclipse' plugin to generate my Eclipse project. Also, I'm using Spring's Gradle plugin for Eclipse which grabs all my dependencies from my build.gradle file.
The dependencies block for this Gradle script has
dependencies {
compile localGroovy()
compile gradleApi()
// I want something like: 'compile gradleApiSources()' here
// I want something like: 'compile gradleApiDoc()' here as well
}
Justification:
As I'm learning to write Gradle plugins, it would be helpful to be able to see the documentation and even implementation for Gradle to help me learn what I'm doing.
This works for me in Eclipse:
plugins.withType(EclipsePlugin) {
plugins.withType(JavaBasePlugin) {
eclipse {
classpath {
file {
whenMerged { classpath ->
String gradleHome = gradle.getGradleHomeDir()
.absolutePath
.replace(File.separator, '/')
String gradleSourceDirectory = "${gradleHome}/src"
classpath.entries.each { entry ->
if (entry in org.gradle.plugins.ide.eclipse.model.AbstractLibrary
&& entry.library.path.contains('generated-gradle-jars')) {
entry.sourcePath =
new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
.fromPath(gradleSourceDirectory)
}
}
}
}
}
}
}
}
Make sure that your Gradle Home contains the source directory. If you use the wrapper this can be done by updating the distributionUrl to an -all version in the wrapper task.
You will also need to stop the Gradle daemons that are running otherwise they will keep their own "home": ./gradlew --stop, then you can go ahead and run the task: ./gradlew eclipse
See GRADLE-2133 (which this answer was adapted from)
I don't have an answer for eclipse but I can tell you how I do this for intellij which might give you some inspiration. It would be nice if this were available more easily.
private void addGradleSourceDeps() {
PathFactory pf = new PathFactory()
pf.addPathVariable('GRADLE_HOME', project.gradle.gradleHomeDir)
project.extensions.idea.module.iml.whenMerged { Module module ->
module.dependencies.grep {
it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('gradle-')
}
}.each { ModuleLibrary lib ->
// TODO this needs to be fixed for gradle 1.9 which now includes separate sub directory for each jar
// for now a workaround is to execute the following
// cd $GRADLE_HOME
// for each in $(find . -mindepth 2 -maxdepth 2 -type d ! -name META\-INF); do cp -a ${each} .;done
lib.sources.add(pf.path('file://$GRADLE_HOME$/src'))
}
module.dependencies.grep {
it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('groovy-all')
}
}.each { ModuleLibrary lib -> lib.sources.add(pf.path('file://$GROOVY_SRC_HOME$')) }
}
}
This relies on me having installed a gradle src distribution into a location available via the GRADLE_HOME path variable in intellij (and similar for GROOVY_SRC_HOME). You can also see my plugin currently uses gradle 1.8, the src layout changed in 1.9 so I need to fix this when I upgrade.

Custom Gradle Plugin ID not found

I'm writing a Gradle plugin and I'm failing to get the apply plugin: command to work in the Gradle script that uses the plugin. I'm using Gradle 1.1.
I've build the plugin with clean build and I'm attempting to add it to the Gradle build via a flat repo for now. That seems to be working but Gradle isn't picking up that there is a plugin with the ID test-plugin. The project name in the plugin's settings.gradle is test-plugin and the properties file in META-INF/gradle-plugins is also test-plugin.properties. I'm not sure where else I can specify the plugin ID.
The build.gradle file in the project that is using the test-plugin:
repositories {
flatDir name: 'libs', dirs: "../build/libs"
}
dependencies {
compile 'test:test-plugin:0.1'
}
apply plugin: 'test-plugin'
Error from Gradle:
What went wrong:
A problem occurred evaluating root project 'tmp'.
Plugin with id 'test-plugin' not found.
The plugin Jar has to be added as a build script dependency:
buildscript {
repositories { flatDir name: 'libs', dirs: "../build/libs" }
dependencies { classpath 'test:test-plugin:0.1' }
}
apply plugin: "test-plugin"
If you want to implement a plugin into your buildscript, then you have two options.
Option 1
apply plugin: YourCustomPluginClassName
Option 2
plugins {
id 'your.custom.plugin.id'
}
apply plugin: is used when specifying your plugin by its class name (ex. apply plugin: JavaPlugin)
plugins { } is used when specifying your plugin by its id (ex. plugins { id 'java' })
See Gradle Plugins by tutorialspoint for reference
If you choose Option 1, the your custom plugin will need to be brought into your build script by 1 of 3 ways.
You can code it directly within your Gradle build script.
You can put it under buildSrc (ex. buildSrc/src/main/groovy/MyCustomPlugin).
You can import your custom plugin as a jar in your buildscript method.
See Gradle Goodness by Mr. Haki for information about the buildscript method.
If you choose Option 2, then you need to create a plugin id. Create the following file buildSrc/src/main/resources/META-INF/gradle-plugins/[desired.plugin.id].properties.
Copy and paste implementation-class=package.namespace.YourCustomPluginClassName into your newly created .properties file. Replace package.namespace.YourCustomPluginClassName with the fully-qualified class name of your desired plugin class.
See Custom Plugins by Gradle for more info.
I also had the same problem with a custom plugin id not being found. In my case, I simply forgot to add the 'src/main/resources/META-INF/gradle-plugins' properties file. The name of the properties file must match the name of the plugin id with a '.properties' extension.
The file must contain a the line:
implementation-class=(your fully qualified plugin classpath)
That's the complete mechanism on how plugin id's get resolved to class names.
In addition the plugin needs to be added as a dependency as pointed out in the previous answer. The android documentation states that you should use a name associated with your unique domain name. I.e.: the name 'test-plugin' is not really in good form, but an id like 'com.foo.gradle.test-plugin' would be better.
Ensure that your top-level build.gradle uses the correct classpath to refer to the path to the built *.jar file.
Some plugins, like maven-publish, will build and save the jar to a specific location in mavenLocal, but the path may not be clear to see.
You should look at the file path of the jar file, and ensure it matches your classpath, but the mapping is not immediately obvious:
buildscript {
dependencies {
// This *MUST* match the local file path of the jar file in mavenLocal, which is:
// ~/.m2/repository/com/company/product/plugin/product-gradle-plugin/1.0/product-gradle-plugin-1.0.jar
classpath 'com.company.product.plugin:product-gradle-plugin:1.0'
}
}
Be careful not to use the wrong classpath, which can refer to a directory instead of the actual jar file; like this:
buildscript {
dependencies {
// This is wrong, because it refers to the mavenLocal FOLDER "product-gradle-plugin",
// instead of the jar FILE "product-gradle-plugin-1.0.jar"
// However, a gradle sync will still resolve it as a valid classpath!!
classpath 'com.company.product:product-gradle-plugin:1.0'
}
}
More info:
https://discuss.gradle.org/t/what-is-the-preferred-gradle-approach-to-locally-install-an-artifact-equivalent-to-mavens-install/5592
https://docs.gradle.org/current/userguide/publishing_maven.html
https://blog.codefx.org/tools/snapshots-gradle-maven-publish-plugin/
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
Adding to what #Bonifacio2 wrote this is a path META-INF/gradle-plugins and shows in IntelliJ as META-INF.gradle-plugins. At all costs don't make the stupid mistake I did creating this directly as a directory META-INF.gradle-plugins because you are based on another sample, and never works. Another tip is copying also from another intelliJ project as this is what is added: gradle-plugins.
hmm perhaps try;
configure <org.jsonschema2pojo.gradle.JsonSchemaExtension> {
this.sourceFiles = files("${project.rootDir}/schemas")
}