license-gradle-plugin icon indicating copy to clipboard operation
license-gradle-plugin copied to clipboard

Not works for *.kt - kotlin files

Open tomtom-gangdam-style-for-map opened this issue 7 years ago • 19 comments
trafficstars

com.android.tools.build:gradle:3.1.0-beta2 gradle wrapper 4.4 Working for java - but for *.kt not even when I try:

license {
    header rootProject.file('LICENSE')
    mapping("kt", "SLASHSTAR_STYLE")
    includes(["**/*.kt", "**/*.java"])
    strictCheck true
    excludes(["**/*Tests.java", "**/*.xml", "**/*TestsUtils.java", "**/*.json", "com/android/**/*.java"])
}

Yeah, I have this problem as well!

widarlein avatar Apr 16 '18 20:04 widarlein

This is a general problem. When the extension is not recognised, it does not even help to create a mapping.

It would be nice to have the ability to add custom extensions, not just language names to a mapping.

ysb33r avatar Apr 20 '18 12:04 ysb33r

What is the complete build.gradle file? It should definitely be possible to add new mappings.

hierynomus avatar Apr 23 '18 08:04 hierynomus

Here's is one example: https://gitlab.com/ysb33rOrg/java-nio2-providers/blob/development/build.gradle#L221

ysb33r avatar Apr 23 '18 09:04 ysb33r

@hierynomus my build.gradle isn't available publicly yet, so here it is:

plugins {
    id "com.github.hierynomus.license" version "0.14.0"
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.hierynomus.license'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "se.geecity.android.steerandput"
        minSdkVersion 14
        targetSdkVersion 23
        manifestPlaceholders = [mapsApiKey: MAPS_API_KEY]
    }

    buildTypes {

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt') //, 'proguard-rules.txt'
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

        buildTypes.each {
            it.buildConfigField 'String', 'BICYCLESERVICE_API_KEY', '"$BICYCLESERVICE_API_KEY"'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

license {
    header rootProject.file('mit.header')
    exclude('**/AutoValue_*.java')
    strictCheck true
    mapping('java', 'SLASHSTAR_STYLE')
    mapping('kt', 'SLASHSTAR_STYLE')
    ext.year = '2018'
    ext.name = 'Alexander Widar'
}

dependencies {
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.android.support:support-v4:25.4.0'
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.android.support:design:25.4.0'
    compile 'com.android.support:cardview-v7:25.4.0'
    compile 'com.google.android.gms:play-services-location:11.0.1'
    compile 'com.google.android.gms:play-services-maps:11.0.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'com.squareup.okhttp3:okhttp:3.7.0'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    compile 'org.koin:koin-android:0.8.0'
}
repositories {
    mavenCentral()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}

widarlein avatar Apr 28 '18 22:04 widarlein

it doesnot recognize kotlin as java project. Henc you have to add this in build.gradle

task licenseFormatForKotlin(type: com.hierynomus.gradle.license.tasks.LicenseFormat) {
    source = fileTree(dir: <kotlin project directory>).include("**/*.kt")
}
licenseFormat.dependsOn licenseFormatForKotlin

now if you run ./gradlew licenseFormat it should consider kotlin files too

yogitas avatar Mar 03 '19 11:03 yogitas

It seems to work without any issue now for .kt files, this should probably be closed

utybo avatar Jun 24 '19 23:06 utybo

It only works for me when adding @yogitas snippet. I have my kotlin code in the regular java source set, so I changed <kotlin project directory> to src/main/java

widarlein avatar Jun 25 '19 11:06 widarlein

I have my kotlin code in the regular java source set

That's a strange organization... It worked without any issue for me by placing the Kotlin code in the correct folder src/main/kotlin

utybo avatar Jun 25 '19 11:06 utybo

That's a strange organization...

Not at all, the kotlin language reference states that In mixed-language projects, Kotlin source files should reside in the same source root as the Java source files, and follow the same directory structure https://kotlinlang.org/docs/reference/coding-conventions.html#directory-structure

Also android developer reference states that By default, new Kotlin files are saved into src/main/java/. You might find it's easier to see both Kotlin and Java files in one location. But if you'd prefer to separate your Kotlin files from your Java files, you can put Kotlin files under src/main/kotlin/ instead. https://developer.android.com/studio/projects/add-kotlin

There's nothing inherently "correct" about putting code in the src/main/kotlin folder

widarlein avatar Jun 25 '19 15:06 widarlein

Huh, TIL, I didn't know that. Sorry about my assumptions!

utybo avatar Jun 25 '19 15:06 utybo

There's nothing inherently "correct" about putting code in the src/main/kotlin folder

FYI There is the matter of Gradle build cache optimisation w.r.t. to polyglot builds. The Gradle folks recommend to have different language types in separate folders. (I still mix Groovy & Java in the same folder as I am willing to sacrifice a couple of seconds of build time).

ysb33r avatar Jun 25 '19 19:06 ysb33r

0.15.0 not working for *.kt files and @utybo's fix works for the licenseFormat task but unfortunately not the license check itself.

gladed avatar Sep 25 '20 22:09 gladed

@gladed That's easy just duplicate the logic with the LicenceCheck task

tasks.register("licenseCheckForKotlin", com.hierynomus.gradle.license.tasks.LicenseCheck::class) {
    source = fileTree(project.projectDir) { include("**/*.kt") }
}
tasks["license"].dependsOn("licenseCheckForKotlin")
tasks.register("licenseFormatForKotlin", com.hierynomus.gradle.license.tasks.LicenseFormat::class) {
    source = fileTree(project.projectDir) { include("**/*.kt") }
}
tasks["licenseFormat"].dependsOn("licenseFormatForKotlin")

Although unrecognized extensions are not handled, eg if the above snippet includes "**/*.kts" as well the plugin reports Unknown file extension

bric3 avatar Sep 28 '21 10:09 bric3

@bric3: I would have a question. Maybe you could help me.

I would like to modularize my Gradle scripts like this:

File: /app/build.gradle

apply from: rootProject.file('gradle/license.gradle')

File: /gradle/license.gradle

buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath "gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin:$gradleLicensePlugin"
    }
}

import com.hierynomus.gradle.license.tasks.LicenseCheck
import com.hierynomus.gradle.license.tasks.LicenseFormat

apply plugin: 'com.github.hierynomus.license'

license {
    header rootProject.file('LICENSE.txt')
    strictCheck true
    ignoreFailures false
    mapping('kt', 'SLASHSTAR_STYLE')
    include '**/my/package/**/*.kt'
}

downloadLicenses {
    includeProjectDependencies = true
    dependencyConfiguration = 'releaseRuntimeClasspath'
}

tasks.register('licenseCheckForKotlin', LicenseCheck) {
    source = fileTree(project.projectDir) { include('src/**/*') }
}
tasks.register('licenseFormatForKotlin', LicenseFormat) {
    source = fileTree(project.projectDir) { include('src/**/*') }
}
tasks['license'].dependsOn('licenseCheckForKotlin')
tasks['licenseFormat'].dependsOn('licenseFormatForKotlin')

Gradle Sync works. But when I invoke licenseCheck I get the following error:

* Exception is:
org.gradle.internal.execution.WorkValidationException: A problem was found with the configuration of task ':app:licenseCheckForKotlin' (type 'LicenseCheck').
  - In plugin 'com.hierynomus.gradle.license.LicenseBasePlugin' type 'com.hierynomus.gradle.license.tasks.LicenseCheck' property 'encoding' doesn't have a configured value.

I guess this is because the License Extension mechanism via license does not work in such a setup. But I don't understand it as I am no Gradle/Groovy expert.

Is there anything I can do to get it working and still keep the whole license stuff in a separate file?

Thanks in advance,

StephanSchuster avatar Oct 19 '21 20:10 StephanSchuster

@StephanSchuster Have you tested this config on a single gradle project ?

Otherwise I suggest to use the subproject (declared on the root descriptor) approach to configure the plugins on the subprojects.

bric3 avatar Oct 22 '21 16:10 bric3

@bric3 Thanks for your response. Yes, I am currently using a gradle project with only a single module/subproject. If I ever switch to multiple subprojects, I would also want to apply apply from: rootProject.file('gradle/license.gradle') selectively, not to all subprojects.

Any other idea how to keep everything related to "license" separate and applying it selectively?

StephanSchuster avatar Oct 22 '21 16:10 StephanSchuster

@StephanSchuster I suppose I would try something like that (excerpt in Kotlin DSL). Not sure though.

val licensedProjects = subprojects - project(":lib-a") - project(":lib-b")

// configure license
configure(licensedProjects) {
  apply(plugin = "com.github.hierynomus.license")

  // ... license plugin stuff
}

I think we should stop this discussion here as it pollutes the original issue. Maybe open a new one on this project, or maybe gradle community.

bric3 avatar Oct 25 '21 16:10 bric3

What's the current status on Kotlin support?

leinardi avatar Apr 28 '23 12:04 leinardi