gradle init script can't apply artifactory plugin - gradle

I'm trying to offload build logic into a gradle init script, which gets included in a custom gradle wrapper. It seems promising. One of the things I need to do is to apply the artifactory plugin prior to configuring it, while the following code works fine in build.gradle, it fails to find the plugin when the code is shifted into an init script.
build.gradle:
buildscript{
Properties properties = new Properties()
properties.load(new File(gradle.getGradleUserHomeDir(), 'gradle.properties').newDataInputStream())
repositories {
maven {
url properties.artifactory_contextUrl + '/gradle-plugins-virtual'
credentials {
username = properties.artifactory_user
password = properties.artifactory_password
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2"
}
}
apply plugin: "com.jfrog.artifactory"
and in the init script it's almost the same:
initscript{
Properties properties = new Properties()
properties.load(new File(getGradleUserHomeDir(), 'gradle.properties').newDataInputStream())
repositories {
maven {
url properties.artifactory_contextUrl + '/gradle-plugins-virtual'
credentials {
username = properties.artifactory_user
password = properties.artifactory_password
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2"
}
}
apply plugin: "com.jfrog.artifactory"
but I get Plugin with id 'com.jfrog.artifactory' not found. with this attempt.
I have also tried making an init plugin, but my plugin skills are not strong, and it also seems to fail.
I tried moving just the apply plugin line to build.gradle from the init script but that also fails, indicating it might be the dependency resolution. How can I debug this further?
I did a build scan and it appears that the plugin jar was found okay.
org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2
commons-io:commons-io:2.7
commons-lang:commons-lang:2.4
commons-logging:commons-logging:1.1.1
1.2
org.apache.ivy:ivy:2.2.0
org.jfrog.buildinfo:build-info-extractor:2.19.2
Any help, comments, suggestions appreciated.
Rant: gradle docs have far too few examples.

For gradle init script, you must use the fully qualified class name of the plugin instead of the id.
Like this:
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin

Related

Gradle: How can I get the path to a specific external dependency stored in .gradle?

I'm quite new to gradle. For the OpenAPI generator repo, I've taken advantage of the very convenient gradle plugin to do almost exactly what I want (generate HTML2 static docs on my openapi.json spec), except that there's an old-time bug where that doc isn't populated correctly. There was a PR request that hasn't been touched for many months that mostly fixes the issue, so I built a custom jar with the changes.
I want to create a new gradle task that runs this custom external jar as an executable, so that I can essentially do the same functionality as their grade plugin. By doing so I've added it to a maven repository and pulled it down as a dependency:
dependencies {
compile 'org.openapitools:openapi-generator-custom-cli:3.0.0'
}
This then pulls the jar into my External Libraries directory which is stored in a very specific location in the cache (ref: Gradle: Where are external dependencies stored?)
How can I get the exact location of this jar? Is there any other approach that would seem more best practice?
It sounds like you're taking a much harder route to get custom documentation in place. If the issue you've found is with a template only, you can specify templateDir in your Gradle task and point to a directory holding your custom directory.
For example:
buildscript {
repositories {
mavenLocal()
maven { url "https://repo1.maven.org/maven2" }
maven {
url "https://plugins.gradle.org/m2/"
}
maven {
url "https://oss.sonatype.org/content/repositories/releases/"
}
}
dependencies {
classpath "org.openapitools:openapi-generator-gradle-plugin:4.2.3"
}
}
apply plugin: 'org.openapi.generator'
task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "go"
templateDir = "$rootDir/custom-templates".toString()
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
additionalProperties = [
packageName: "petstore"
]
outputDir = "$buildDir/go".toString()
configOptions = [
dateLibrary: "threetenp"
]
}
Then, you can build this with gradle buildGoSdk.
If the issue you're looking to fix has some logic in the Java code that manages the generator, you can create a custom generator jar and load this without modifying the gradle plugin itself.
I actually have a sample repo for the HTML2 generator which does exactly this. My example only changes the generator name and adds Google Analytics as a config option and to the template, but it should be easy enough to follow along. See https://github.com/jimschubert/custom-generator-example
The key part would be to add your custom generator jar to the buildscript dependencies like this (taken from my repo example):
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "us.jimschubert.examples:html-generator:1.0-SNAPSHOT"
classpath "org.openapitools:openapi-generator-gradle-plugin:3.3.3"
}
}
apply plugin: 'org.openapi.generator'
task buildCustomHtml2(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "custom-html2"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
additionalProperties = [
appName: "My Custom HTML2 Generator",
googleAnalytics: "12345-6"
]
outputDir = "$buildDir/custom-html".toString()
}

How to use init.gradle to provide plugin repositories for plugins configured in the settings.gradle

I have configured a Gradle plugin in settings.gradle file as follows,
buildscript {
dependencies {
classpath "org.test.group:gradleplugins:${pluginVersion}"
...
}
}
apply plugin: 'org.test.group:gradleplugins'
....
and I am trying to provide artifacts repository using init.gradle as follows,
initscript {
repositories {
maven { url "https://test.repo/gradleplugins" }
...
}
}
also, I have provided init.gradle file to the build task using,
.... -i -I ./init.gradle'
but the build still gets a dependency resolution error as follows,
Cannot resolve external dependency org.test.group:gradleplugins:1.0.0-SNAPSHOT because no repositories are defined.
It could be done either way by writing Gradle plugin in the init.gradle file as following,
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.settingsEvaluated { settings ->
settings.pluginManagement {
repositories {
maven { url "https://repo.org/gradleplugins" }
maven { url "https://repo.org/maven" }
}
}
}
}
}
according to Gradle documentation,
https://docs.gradle.org/current/userguide/init_scripts.html
The init.gradle has another purpose (and that file is being automatically detected).
void settingsEvaluated​(Settings settings) might be to only chance to manipulate these settings (but the question does not provide the least valid reason to do so, with only one repository). It rather seems that you're unnecessarily over-complicating things, where there otherwise would be no problem. And this doesn't belong into the settings.gradle either (which also has another purpose). Just add the plugin repositories into the buildscript block of the root project's build.gradle, where they belong. The userguide shows how it should look alike, when defining the plugin repositories statically.

Plugin with id 'org.ajoberstar.grgit' not found

I'm trying to compile an opensource minecraft mod called VeinMiner.
I use ./gradlew build to compile it.
However it failed and give me this reason:
FAILURE: Build failed with an exception.
* Where:
Build file '/home/xxxx/Desktop/VeinMiner/build.gradle' line: 26
* What went wrong:
A problem occurred evaluating root project 'VeinMiner'.
> Plugin with id 'org.ajoberstar.grgit' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 14.778 secs
I have tried to git clone it again or run it in sudo.
However, neither of these two work.
This build gradle is like this:
(It's too long so I select some part of it)
buildscript {
repositories {
mavenCentral()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
}
}
plugins {
id 'com.matthewprenger.cursegradle' version '1.0.7'
id 'org.ajoberstar.grgit' version '1.3.2'
}
apply plugin: 'forge'
apply plugin: 'maven'
apply plugin: "org.ajoberstar.grgit"
apply plugin: "com.matthewprenger.cursegradle"
ext.git = grgit.open(file('.'))
ext {
configFile = file "build.properties"
revision = git.head().abbreviatedId
depth = git.log().size()
}
I expected to Build Successful. Have anyone met this situation?
The mentioned plugin org.ajoberstar.grgit gets applied twice. The first time it gets applied via the new plugins block, the second time via the old apply plugin: method. Simply remove the line apply plugin: "org.ajoberstar.grgit" and the error should disappear.
To apply plugins via the old apply plugin: method, the plugin package needs to be resolved from an arbitrary repository inside the buildscript block. This works in the same way as resolving project dependencies via the normal repositories and dependencies blocks.
The new plugins block directly resolves plugins from the Gradle plugin portal and applies them in the same step.
I would guess that the way how to apply to plugin was changed to the newer one, but the removal of the old method did not happen. In existing setups the plugin may have been resolved from the local Gradle cache, but on your new setup it could not be found.

Gradle: add plugin dependency from another plugin

I'm creating gradle custom plugin and one of my tasks needs to be sure that another plugin applied to same project. Because it will operate on top of it.
I want for users of my plugin to avoid setting up an explicit dependency to another plugin - I want to do it inside my plugin.
So, I want to have this plugin (https://plugins.gradle.org/plugin/org.hidetake.ssh) applied. It's my dependency.
The way how I create plugin - I just create a class code on groovy, put it in buildSrc\src\main\groovy and apply groovy plugin in project. So my custom plugin is visible to gradle on build phase. It works, I have few other plugins done this way for same project, so it's fine for now.
I've looked through other topics and google for same question, but I can not make this work for me. This how I apply the code:
void apply(Project project) {
project.buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.hidetake:gradle-ssh-plugin:1.1.3"
}
}
project.apply plugin: "org.hidetake.ssh"
...
The error message I got: Plugin with id 'org.hidetake.ssh' not found.
I tried to do it via gradle api also using project.repositories.mavenCentral() and project.dependencies.add and project.apply(plugin:'org.hidetake.ssh') then - doesn't work also - same error message. Tried to use long notation in project.dependencies.add("myConfig",[group:'org.hidetake', name:'gradle-ssh-plugin', version:'1.1.3']) - no result.
Appreciate if someone can guide to the correct syntax\way to make it work.
Ok, finally I got it. To solve the issue you need to do the following:
Place build.gradle in your buildSrc directory.
Declare dependency for the plugin as runtime. Like this:
repositories {
jcenter()
}
dependencies {
runtime 'org.hidetake:gradle-ssh-plugin:2.6.0'
}
Apply plugin explicitly in your own plugin definition. Like this:
void apply(Project project) {
project.pluginManager.apply('org.hidetake.ssh')
...

Applying Gradle Dependency-Check plugin

I am trying to use the dependency.check from the following link and have been unable to get it to run properly (at all) when following the instructions given.
https://github.com/jeremylong/DependencyCheck/tree/master/dependency-check-gradle
When trying to build with the apply plugin and additional dependency the fails on startup and it throws the following error.
Where:
Build file '/Users/aaron/work/backups/eiss/build.gradle' line: 25
What went wrong:
A problem occurred evaluating root project 'eiss'.
Failed to apply plugin [id 'dependency.check']
Plugin with id 'dependency.check' not found.
I made a little progress when making some changes but was still ultimately unsuccessful.
First, I commented out the apply plugin line.
Next, I switched:
classpath "com.thoughtworks.tools:dependency-check:0.0.7"
over to:
compile "com.thoughtworks.tools:dependency-check:0.0.7"
After these two changes it began recognizing the path and I was able to see it grabbing the items from the repository.
Even with the path correct I am still having issues with the apply plugin line with it throwing the same error whenever I place it into the script or even try to change the '.' in it into a '-' (both are used in the instructions and in different repository examples).
Any help on this issue would be appreciated! Thanks
lastly here is the build.gradle script. I didn't want to just leave this blob right in the center of the post.
defaultTasks 'assemble'
// For third party libs that are widely used, keep versions in one place
ext {
MONGO_JAVA_DRIVER = "org.mongodb:mongo-java-driver:2.12.3"
RABBITMQ_VERSION = "com.rabbitmq:amqp-client:3.4.3"
LOG4J = "log4j:log4j:1.2.16"
// For groovy there are multiple libs, just capture version number and use lib-name-$GROOVY_VERSION
GROOVY_VERSION = "2.3.6"
}
//
// Common settings for all projects
//
subprojects {
defaultTasks 'assemble'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
apply plugin: 'dependency.check'
targetCompatibility = "1.6"
sourceCompatibility = "1.6"
repositories {
mavenCentral()
}
dependencies {
compile LOG4J
compile "org.codehaus.groovy:groovy:${GROOVY_VERSION}"
compile "org.codehaus.groovy:groovy-json:${GROOVY_VERSION}"
compile "org.codehaus.groovy:groovy-templates:${GROOVY_VERSION}"
compile "com.thoughtworks.tools:dependency-check:0.0.7"
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile "org.codehaus.groovy:groovy-test:${GROOVY_VERSION}"
testCompile "org.hamcrest:hamcrest-core:1.3"
}
clean.doLast {
// The archive path is configured via the jar tasks. Can't use
// delete jar.archivePath because that will configure the delete with
// the wrong (default) path of build/libs/<component.jar>
jar.archivePath.delete()
jarSources.archivePath.delete()
}
//--------------------------------------------------------------------
// Run and test
//--------------------------------------------------------------------
test {
// Uncomment to see standard output when running tests
testLogging.showStandardStreams = true
// This causes tests to run even when nothing has changed
outputs.upToDateWhen { false }
maxParallelForks = 1
}
task runClass(dependsOn: 'classes', type: JavaExec) {
if (project.hasProperty('classToRun')) {
if (project.hasProperty('arguments')) {
args(arguments.split(','))
}
classpath = sourceSets.main.runtimeClasspath
main=classToRun
}
}
//run this task to create source jars
task jarSources(type:Jar){
destinationDir = new File(projectDir.parent + "/sourcelibs")
from sourceSets.main.allSource
classifier 'sources'
}
}
You added plugin dependency in a wrong place, to the dependencies of your project, not a build script itself, which will use it. Try to add buildscript dependencies, as it's made in the example of plugin installation
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.thoughtworks.tools:dependency-check:0.0.7'
}
}
And then return your apply plugin
apply plugin: 'dependency.check'
Though this is very old post, accepted answer is using legacy plugin application, whereas below could be used while using the plugins DSL: https://plugins.gradle.org/plugin/org.owasp.dependencycheck
plugins {
id "org.owasp.dependencycheck" version "7.3.0"
}
With recent version of gradle it is below steps
add id 'com.diffplug.spotless' version '6.3.0' in plugins section like
plugins {
id 'com.diffplug.spotless' version '6.3.0'
}
And define your task to generate required format reports. Here for e.g. xml and json will be generated along with the html report
dependencyCheck{
formats=['xml','json']
check.dependsOn(dependencyCheckAnalyze)
}
And this can be integrated with Sonar by adding below properties to sonare.properties file (Provide dependency plugin is installed already on the sonar)
sonar.dependencyCheck.xmlReportPath=build/reports/dependency-check-report.xml
sonar.dependencyCheck.jsonReportPath=build/reports/dependency-check-report.json
sonar.dependencyCheck.htmlReportPath=build/reports/dependency-check-report.html

Resources