How to define which Folders/Directories the CodeNarc Gradle Plugin will scan/analyse - gradle

What do I want to do?
I want to tell CodeNarc which folders/directories it is supposed to scan/analyse.
I cant find anything about that on the official site (http://codenarc.sourceforge.net/) or on the gradle plugin documentation (https://docs.gradle.org/current/userguide/codenarc_plugin.html).
Results of my Research:
The only related thing I found was in the grails CodeNarc plugin documentation (https://grails.org/plugin/codenarc). There it says you can configure which source files are analysed by CodeNarc. But this seems to be the case only for the grails CodeNarc plugin not the gradle version.
The gradle CodeNarc documentation describes 3 tasks, one of them with the following:
codenarcSourceSet — CodeNarc
Runs CodeNarc against the given source set’s Java source files
Based on the description I thought I might be able to do something with that.
But when I look for all tasks my gradle Project got only "codenarcMain" and "codenarcTest".
My Set-up:
My gradle project got the following structure:
.git
-> ...
.gradle
-> ...
config
-> codenarc -> MyRuleset.groovy
gradle
-> ...
src
-> main
-> groovy -> ...
-> ressources
-> test
-> groovy -> ...
-> ressources
vars
-> ...
.gitignore
build.gradle
gradlew
gradlew.bat
ReadMe.txt
settings.gradle
My build.gradle looks like that:
plugins {
// Apply the groovy plugin to add support for Groovy
id 'groovy'
id 'codenarc'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
maven { url "http://repo.jenkins-ci.org/releases/" }
maven { url "http://central.maven.org/maven2/" }
}
dependencies {
// https://mvnrepository.com/artifact/org.jenkins-ci.main/jenkins-core
compile group: 'org.jenkins-ci.main', name: 'jenkins-core', version: '2.179'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins/cloudbees-folder
compile 'org.jenkins-ci.plugins:cloudbees-folder:5.3#jar'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-api
compile 'org.jenkins-ci.plugins.workflow:workflow-api:2.35#jar'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-job
compile 'org.jenkins-ci.plugins.workflow:workflow-job:2.32#jar'
// https://mvnrepository.com/artifact/com.cloudbees/groovy-cps
compile group: 'com.cloudbees', name: 'groovy-cps', version: '1.28'
// https://mvnrepository.com/artifact/org.codenarc/CodeNarc
codenarc group: 'org.codenarc', name: 'CodeNarc', version: '1.1'
// Use the latest Groovy version for building this library
implementation 'org.codehaus.groovy:groovy-all:2.5.6'
// Use the awesome Spock testing and specification framework
testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
}
codenarc {
configFile = file("${project.projectDir}/config/codenarc/MyRuleset.groovy")
reportFormat = 'html'
reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
maxPriority1Violations = 0
maxPriority2Violations = 0
maxPriority3Violations = 0
}
Specific goal:
I basically want CodeNarc to also scan scripts inside the "vars" folder/directory.
But it seems to do that only inside the src/main/groovy and src/test/groovy.
I also don't know exactly what the default paths are because I didn't find it in the documentation.

Seems like all you have to do is add the following to your gradle.build
sourceSets {
main {
groovy {
srcDirs = ['src/main', 'vars']
}
}
}
This way CodeNarc scans everything inside the src/main folder and everything inside the vars folder. (https://discuss.gradle.org/t/gradle-codenarc-plugin-how-to-change-target-folder-for-the-scan/32102/2)
[edit]
It is even possible to define your own codeNarc task:
sourceSets {
main {
groovy {
srcDirs = ['src']
}
resources {
srcDirs = []
}
}
test {
groovy {
srcDirs = ['test']
}
resources {
srcDirs = []
}
}
vars {
compileClasspath += main.compileClasspath
compileClasspath += main.output
groovy {
srcDirs = ['vars']
}
resources {
srcDirs = []
}
}
}
codenarc {
toolVersion = '1.3'
configFile = file("${project.projectDir}/config/codenarc/ruleset.groovy")
sourceSets = [sourceSets.main, sourceSets.test, sourceSets.vars]
reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
}
codenarcMain {
configFile = file("${project.projectDir}/config/codenarc/MainRuleset.groovy")
}
codenarcTest {
configFile = file("${project.projectDir}/config/codenarc/TestRuleset.groovy")
}
codenarcVars {
configFile = file("${project.projectDir}/config/codenarc/VarsRuleset.groovy")
}

Related

Running PMD with gradle without compiling

I need to integrate PMD into the build process, so I created a file, 'check.gradle' under a separate directory and added it to the build.gradle which is a main file for the build process.
build.gradle
buildscript {
ext {
sbDir = "${projectDir.parent}"
}
repositories {
mavenCentral()
jcenter()
}
}
allprojects {
repositories {
mavenCentral() // maven { url: 'http://jcenter.bintray.com' }
}
}
apply from: file('../build/pmd/check.gradle')
check.gradle
subprojects {
apply plugin: 'pmd'
pmd {
consoleOutput = true
toolVersion = "6.21.0"
reportsDir = file("pmd/reports")
ruleSets = ["../build/pmd/MyRuleSet.xml"]
ignoreFailures = true
}
}
The code base is written in Java. The problem is that I want to run it before compile happens or i don't want to run it separately as a simple task. Our compile task is pretty complicated, cuz it depends on many libraries.
I am running as like this.
gradlew.bat --build-file bundles/build.gradle check
Is there a way to scan all the java source code (we have 200 bundles - each bundle is packaged with many java files) without compiling java source code?

How do you disable distZip in a multi project builds on Kotlin DSL in Gradle

I have set up a Gradle multi project build using Kotlin DSL. This is build.gradle.kts in the root:
plugins {
kotlin("jvm") version "1.2.70" apply false
}
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
version = "1.0"
}
This is sub/build.gradle.kts in my sub project:
plugins {
application
kotlin("jvm")
}
application {
mainClassName = "me.package.MainKt"
}
dependencies {
compile(kotlin("stdlib"))
compile("io.github.microutils:kotlin-logging:1.6.10")
compile("ch.qos.logback:logback-classic:1.2.3")
}
When I run $ gradle build the application plugin creates me a distribution in sub/build/distribution.
I don't need the zip distribution and I want no version number in the tar distribution. Both should be trivial in the regular build.gradle like:
distZip.enabled = false
distTar.archiveName = "${project.name}.tar"
Whatever I try using Kotlin DSL, I get Unresolved reference: distZip. How do I address the distZip and distTar tasks?
What you need is:
val distZip by tasks
distZip.enabled = false
val distTar by tasks
distTar.archiveName = "${project.name}.tar"
or:
tasks.getByName<Zip>("distZip").enabled = false
tasks.getByName<Tar>("distTar").archiveName = "${project.name}.tar"

Adding external source files to a kotlin project

I have Kotlin sources located at, say, repo/project_a/src/. I created a Kotlin Gradle project in IntelliJ IDEA, located at repo/project_b/.... And I can't for the life of me figure out how to add the sources. If I add them through project structure menu it works fine, but as soon as it wants to re-read the gradle file id deletes the structure (It warns as much in the UI).
This is my gradle file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.70'
}
group 'cli'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
I've tried adding all variations of
sourceSets {
main {
kotlin {
srcDirs += "repo/project_a/"
}
}
}
But it does absolutely nothing.
Any ideas?
The path you are giving to Gradle will compile to the current project path plus "repo/project_a/". Try with:
sourceSets {
main {
kotlin {
srcDirs += "../project_a/"
}
}
}

How to build Google protocol buffers and Kotlin using Gradle?

I'm trying to build a project that uses both Google protocol buffers and Kotlin using Gradle. I want the proto files to compile into Java source, which is then called from my Kotlin code.
My source files are arranged like this:
src/main/proto/*.proto
src/main/kotlin/*.kt
src/test/kotlin/*.kt
Here's my build.gradle file:
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
buildscript {
ext.kotlin_version = '1.1-M02'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.12'
}
When I run ./gradlew assemble I get a bunch of "Unresolved reference" errors during :compileKotlin. Afterwards I can see that there are no Java source files generated, so it appears that the proto compiler is not being invoked at all.
If I remove the apply plugin: 'kotlin' line, then ./gradlew assemble successfully generates the Java source, but of course my Kotlin source is never compiled.
How do I fix my build.gradle so that I can call my protobuf code from Kotlin?
To get protobuf-gradle-plugin and kotlin-gradle-plugin to cooperate, you need to ensure that the Java code is (re)generated before invoking the Kotlin compiler.
For Gradle's default source sets, main and test, you can do that like this:
compileKotlin.dependsOn ':generateProto'
compileTestKotlin.dependsOn ':generateTestProto'
If you are using other source sets, you'll need to make adjustments.
Older versions of protobuf-gradle-plugin also required updating sourceSets, but newer versions do not seem to require this.
// Don't do this with protobuf-gradle-plugin 0.9.0 or higher
sourceSets.main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/java"
sourceSets.test.java.srcDirs += "${protobuf.generatedFilesBaseDir}/test/java"
For Kotlin and Android:
android {
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/debug/java'
release.java.srcDirs += 'build/generated/source/proto/release/java'
}
}
An additional source directory has to be added for every build type. In this sample there are two build types: debug and release.
If you're using grpc, another line has to be added per build type:
android {
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/debug/java'
debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
release.java.srcDirs += 'build/generated/source/proto/release/java'
release.java.srcDirs += 'build/generated/source/proto/release/grpc'
}
}
At least with Kotlin 1.0.6, protobuf-gradle-plugin 0.8.0, protobuf 3.2.x and grpc 1.x it's not required to fiddle with the task order.
if you are working with multiple build types and flavors in android and with protobuf-lite use below with kotlin.
for example I have debug and release builds with demo and prod flavors it will create demoDebug, demoRelease and prodDebug and prodRelease variants.
then use
`
android{
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/demoDebug/javalite'
debug.java.srcDirs += 'build/generated/source/proto/prodDebug/javalite'
release.java.srcDirs += 'build/generated/source/proto/demoRelease/javalite'
release.java.srcDirs += 'build/generated/source/proto/prodRelease/javalite'
}
}
`
tie the different compileKotlin with generateProto
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
if (getName() == 'compileDemoDebugKotlin')
dependsOn(':app:generateDemoDebugProto')
if (getName() == 'compileDemoReleaseKotlin')
dependsOn(':app:generateDemoReleaseProto')
if (getName() == 'compileProdDebugKotlin')
dependsOn(':app:generateProdDebugProto')
if (getName() == 'compileProdReleaseKotlin')
dependsOn(':app:generateProdReleaseProto')
}
For the gradle setup :
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.protobuf' version "0.8.17"
}
Then at the bottom of the build.gradle
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
// Generates the java Protobuf-lite code for the Protobufs in this project. See
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
// for more information.
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}

QueryDSL, spring-boot & Gradle

I was hoping to bring querydsl into my spring-boot project via gradle. Despite finding a couple of examples online, none of them actually work for me because of issues with dependencies (I think). According to the QueryDSL support forum, gradle is not supported yet. But I was wondering with all the gradle & spring-boot being created if someone has managed to make it work yet?
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
}
}
repositories {
mavenCentral()
maven { url: "http://repo.spring.io/libs-snapshot" }
// maven { url: "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RC5")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC5")
compile("org.springframework:spring-orm:4.0.0.RC1")
compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("org.thymeleaf:thymeleaf-spring4")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile('org.codehaus.groovy:groovy-all:2.2.1')
compile('org.jadira.usertype:usertype.jodatime:2.0.1')
// this line fails
querydslapt "com.mysema.querydsl:querydsl-apt:3.3.2"
testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
sourceSets {
main {
generated {
java {
srcDirs = ['src/main/generated']
}
}
java {
srcDirs = []
}
groovy {
srcDirs = ['src/main/groovy', 'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
output.resourcesDir = "build/classes/main"
}
test {
java {
srcDirs = []
}
groovy {
srcDirs = ['src/test/groovy', 'src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
output.resourcesDir = "build/classes/test"
}
}
configurations {
// not really sure what this is, I see it in examples but not in documentation
querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
"-proc:only",
"-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}
compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}
clean {
delete sourceSets.generated.java.srcDirs
}
idea {
module {
sourceDirs += file('src/main/generated')
}
}
But gradle fails with:
Could not find method querydslapt() for arguments [com.mysema.querydsl:querydsl-apt:3.3.2]
I have tried changing the querydsl-apt version to earlier ones but I get the same error.
Working configuration for Spring Boot 1.3.5 and supported QueryDSL, tested with gradle 2.14.
ext {
queryDslVersion = '3.6.3'
javaGeneratedSources = file("$buildDir/generated-sources/java")
}
compileJava {
doFirst {
javaGeneratedSources.mkdirs()
}
options.compilerArgs += [
'-parameters', '-s', javaGeneratedSources
]
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile "com.mysema.querydsl:querydsl-jpa:$queryDslVersion"
compileOnly "com.mysema.querydsl:querydsl-apt:$queryDslVersion:jpa"
}
Complete project source code: spring-boot-querydsl
You probably need to do at least 2 things:
Declare the "querydslapt" configuration before you use it
Add querydsl-jpa (or whatever flavours you need) to your "compile" configuration.
Then you will have the classpath set up, but the apt bit will not do anything without some more configuration (as you found no doubt from the querydsl support forum). The apt but is used to generate some code that you then need to compile and use in your application code (the "Q*" classes corresponding to your domain objects). You could drive that from a build task in gradle I imagine (it only has to run once for every change in the domain objects).

Resources