ebourg / jsign

Java implementation of Microsoft Authenticode for signing Windows executables, installers & scripts
https://ebourg.github.io/jsign
Apache License 2.0
259 stars 108 forks source link

jsign-gradle-plugin kotlin example #72

Closed vewert closed 2 years ago

vewert commented 4 years ago

Hi,

I am trying to use jsign-gradle-plugin. The gradle example given looks like it uses the groovy syntax. My build file uses Kotlin syntax and I am having trouble getting it to work.

In my build script dependencies I have:

dependencies {
    classpath(group = "net.jsign", name = "jsign-gradle-plugin", version = "3.0")
}

Then I use: apply(plugin = "net.jsign")

I then try to access jsign with a task (as in the example), but it seems unrecognized and I can't figure out how to include the parameters:

task("sign") {
  doLast {
    jsign()
  }
}

I'm not sure what I am doing wrong

ebourg commented 4 years ago

Sorry I won't be able to help as I'm not using Gradle, but if you figure this out I'd be interested in adding an example to the documentation.

ctadlock commented 2 years ago

Any update on this? I can not get it to work with Kotlin Gradle.

plugins {
    // kotlin
    kotlin("jvm") version "1.6.10"

    id("net.jsign") version "4.1"
}

repositories {
    mavenCentral()
}

Gradle error: Plugin [id: 'net.jsign', version: '4.1'] was not found in any of the following sources:

https://github.com/ebourg/jsign/issues/33

ebourg commented 2 years ago

@ctadlock Did you try translating the example in the source code to the Kotlin syntax?

https://github.com/ebourg/jsign/blob/master/jsign-gradle-plugin/example.gradle

It worked when I wrote it 5 years ago, but I don't know if it still does since Gradle often breaks the backward compatibility.

ctadlock commented 2 years ago

@ctadlock Did you try translating the example in the source code to the Kotlin syntax?

https://github.com/ebourg/jsign/blob/master/jsign-gradle-plugin/example.gradle

It worked when I wrote it 5 years ago, but I don't know if it still does since Gradle often breaks the backward compatibility.

Yes, I have tried several different ways to get this to work and the plugin can never be found.

Sage-Kreiter commented 2 years ago

Any new progress on this? I'm having the same issue trying to use id("net.jsign") version "4.1" with Kotlin and Gradle. Same syntax works for all my other Plugins

ctadlock commented 2 years ago

Im pretty sure this is the cause..

https://github.com/ebourg/jsign/issues/45

ebourg commented 2 years ago

The id("net.jsign") syntax won't work (see #45), the plugin has to be added to the classpath.

ctadlock commented 2 years ago

You need to modify your code for it to work correctly (https://github.com/ebourg/jsign/issues/45). The workaround is to add this to settings.gradle.kts which is a hack:

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
    resolutionStrategy {
        eachPlugin {
            switch (requested.id.id) {
                case 'net.jsign':
                    useModule "net.jsign:jsign-gradle-plugin:$requested.version"
                    break
            }
        }
    }
}

For reference here are three popular plugins that only need an entry in plugins {}:

plugins {
    // kotlin
    kotlin("jvm") version "1.6.10"

    // https://github.com/Kotlin/kotlinx.serialization
    kotlin("plugin.serialization") version "1.6.10"

    // https://github.com/beryx/badass-runtime-plugin
    id("org.beryx.runtime") version "1.12.7"

    // https://github.com/johnrengelman/shadow
    id("com.github.johnrengelman.shadow") version "7.1.2"

    // compose
    id("org.jetbrains.compose") version "1.1.1"
}
ebourg commented 2 years ago

I tried this but got these errors:

Script compilation errors:

  Line 09:                 case 'net.jsign':
                                ^ Unexpected tokens (use ';' to separate expressions on the same line)

  Line 10:                     useModule "net.jsign:jsign-gradle-plugin:4.1"
                                         ^ Unexpected tokens (use ';' to separate expressions on the same line)

  Line 08:             switch (requested.id.id) {
                       ^ Unresolved reference: switch

  Line 09:                 case 'net.jsign':
                           ^ Unresolved reference: case

  Line 10:                     useModule "net.jsign:jsign-gradle-plugin:4.1"
                               ^ Function invocation 'useModule(...)' expected

  Line 10:                     useModule "net.jsign:jsign-gradle-plugin:4.1"
                               ^ No value passed for parameter 'p0'

  Line 11:                     break
                               ^ 'break' and 'continue' are only allowed inside a loop

7 errors

If someone has a full example of the jsign plugin used with the Kotlin DSL I'd be happy to add it to the documentation.

ebourg commented 2 years ago

Here is a syntax that works with the Kotlin DSL:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("net.jsign:jsign-gradle-plugin:4.1")
    }
}

apply(plugin = "net.jsign")

task("sign") {
    doLast {
        val jsign = project.extensions.getByName("jsign") as groovy.lang.Closure<*>;
        jsign.call(mapOf(
            "file"      to "application.exe",
            "name"      to "My Application",
            "url"       to "http://www.example.com",
            "keystore"  to "keystore.p12",
            "alias"     to "test",
            "storepass" to "secret",
            "tsaurl"    to "http://timestamp.sectigo.com"));
    }
}

No need to hack the resolutionStrategy, the apply plugin syntax is much more compact.

If anyone knows how to make the jsign invocation look nicer I'm interested.

ebourg commented 2 years ago

In the next release (4.2) the call to mapOf() can be removed, the syntax will be reduced to:

    val jsign = project.extensions.getByName("jsign") as groovy.lang.Closure<*>
    jsign("file"      to "application.exe",
          "name"      to "My Application",
          "url"       to "http://www.example.com",
          "keystore"  to "keystore.p12",
          "alias"     to "test",
          "storepass" to "secret",
          "tsaurl"    to "http://timestamp.sectigo.com")