hierynomus / license-gradle-plugin

Manage your license(s)
http://www.javadude.nl
Other
414 stars 113 forks source link

Not works for *.kt - kotlin files #155

Open tomtom-gangdam-style-for-map opened 6 years ago

tomtom-gangdam-style-for-map commented 6 years ago

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"])
}
widarlein commented 6 years ago

Yeah, I have this problem as well!

ysb33r commented 6 years ago

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.

hierynomus commented 6 years ago

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

ysb33r commented 6 years ago

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

widarlein commented 6 years ago

@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'
    }
}
yogitas commented 5 years ago

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

utybo commented 5 years ago

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

widarlein commented 5 years ago

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

utybo commented 5 years ago

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

widarlein commented 5 years ago

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

utybo commented 5 years ago

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

ysb33r commented 5 years ago

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).

gladed commented 4 years ago

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

bric3 commented 3 years ago

@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

StephanSchuster commented 3 years ago

@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,

bric3 commented 3 years ago

@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.

StephanSchuster commented 3 years ago

@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?

bric3 commented 3 years ago

@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.

leinardi commented 1 year ago

What's the current status on Kotlin support?