how do i import a kotlin script from a kotlin buil...
# gradle
s
how do i import a kotlin script from a kotlin build script as i have a 1.6k line kotlin build script (
build.gradle.kts
) that i want to split into smaller files
g
If you want be type safe, extract logic to buildSrc, do not use scripts and apply
1
s
could you give me an example of that?
g
See also official Kotlin DSL sample projects that mention buildSrc in name https://github.com/gradle/kotlin-dsl/tree/master/samples
Also if you have a lot of business logic (like preconfigure extensions, tasks) applied by request, maybe make sense to create plugin in buildSrc and use precompiled script plugins which reduce amount of boilerplate code for gradle plugins. See docs: https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:precompiled_plugins And sample project https://github.com/gradle/kotlin-dsl/tree/master/samples/precompiled-script-plugin
s
ill take a look at that
i get
Copy code
org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: Build file '/home/macropreprocessor/AndroidStudioProjects/sample/buildSrc/build.gradle.kts' line: 5
Script compilation errors:

Line 5: gradlePlugin {
    ^ Unresolved reference: gradlePlugin

        Line 7:         register("greet-plugin") {
    ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
    ...
    
    Line 8:             id = "greet"
    ^ Function invocation 'id(...)' expected

        Line 8:             id = "greet"
    ^ No value passed for parameter 'id'

    Line 8:             id = "greet"
    ^ Variable expected

        Line 8:             id = "greet"
    ^ Type mismatch: inferred type is String but PluginDependencySpec was expected

    Line 9:             implementationClass = "GreetPlugin"
    ^ Unresolved reference: implementationClass

        7 errors
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:154)
    at org.gradle.initialization.DefaultGradleLauncher.executeTasks(DefaultGradleLauncher.java:124)
    ...
    at java.lang.Thread.run(Thread.java:745)
    Caused by: org.gradle.internal.exceptions.LocationAwareException: Build file '/home/macropreprocessor/AndroidStudioProjects/sample/buildSrc/build.gradle.kts' line: 5
    Script compilation errors:

    Line 5: gradlePlugin {
    ^ Unresolved reference: gradlePlugin

        Line 7:         register("greet-plugin") {
    ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
    ...

    Line 8:             id = "greet"
    ^ Function invocation 'id(...)' expected

        Line 8:             id = "greet"
    ^ No value passed for parameter 'id'

    Line 8:             id = "greet"
    ^ Variable expected

        Line 8:             id = "greet"
    ^ Type mismatch: inferred type is String but PluginDependencySpec was expected

    Line 9:             implementationClass = "GreetPlugin"
    ^ Unresolved reference: implementationClass

        7 errors
        at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageScript$cacheDir$1.invoke(Interpreter.kt:624)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageScript$cacheDir$1.invoke(Interpreter.kt:349)
   ...
    ... 166 more
g
hard to say without your build.gradle.kts code
most probably you didn’t apply java-gradle-plugin
s
buildSrc/build.gradle.kts
Copy code
plugins {
    `kotlin-dsl`
}

gradlePlugin {
    plugins {
        register("greet-plugin") {
            id = "greet"
            implementationClass = "GreetPlugin"
        }
    }
}

repositories {
    jcenter()
}
g
But if you do not plan to publish this plugin, I would instead try to use precompiled script plugins that don’t require registration and you could just remove this boilerplate code
so yes, you didn’t apply plugin
java-gradle-plugin
that provides
gradlePlugin
extension
s
so i put this at the top of the file right?
apply 'java-gradle-plugin'
g
no
Copy code
plugins {
    `kotlin-dsl`
   `java-gradle-plugin`
}
s
ok
But again, I would recommend start from precompiled script plugin instead https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:precompiled_plugins
s
Copy code
Timeout waiting to lock buildSrc build lock. It is currently in use by another Gradle instance.
Owner PID: 17080
Our PID: 11178
Owner Operation: 
Our operation: 
Lock file: /home/macropreprocessor/AndroidStudioProjects/sample/buildSrc/.gradle/noVersion/buildSrc.lock
g
Error message looks clear for me
s
by
In addition to normal Kotlin source files that go under src/main/kotlin
its talking about
buildSrc/src/main/kotlin
right?
g
yes
s
where do i put
Copy code
plugins {
    'greet'
}
g
'greet'
- doesn’t look correct for me, if you have your plugin in greet.gradle.kts you should use:
Copy code
plugins {
    greet
   // or
    `greet`
}
s
as i get
Copy code
startup failed:
build file '/home/macropreprocessor/AndroidStudioProjects/sample/build.gradle': 31: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

See <https://docs.gradle.org/4.10.1/userguide/plugins.html#sec:plugins_block> for information on the plugins {} block

 @ line 31, column 1.
   plugins {
   ^

1 error

Open File
g
where do i put
To any module where you want to apply it
s
ok
g
this error message also looks pretty clear for me
you probably applied it not on top of your file
see documentation link from this error message to get additional info why it happend
s
like this?
Copy code
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.sample"
        minSdkVersion 'Q'
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), '<http://proguard-rules.pro|proguard-rules.pro>'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

plugins {
    'greet'
}
app/build.gradle
g
'greet'
- is incorrect
use backticks instead
also plugins block must be on the top of file
see documetnation from error message:
The plugins {} block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).
s
Copy code
build file '/home/macropreprocessor/AndroidStudioProjects/sample/app/build.gradle': 2: unexpected char: '`' @ line 2, column 5.
       `greet`
g
just use
plugins { greet }
there is some problem with backticks, but it hard to understand without checking original file
s
Copy code
startup failed:
build file '/home/macropreprocessor/AndroidStudioProjects/sample/app/build.gradle': 1: only id(String) method calls allowed in plugins {} script block

See <https://docs.gradle.org/4.10.1/userguide/plugins.html#sec:plugins_block> for information on the plugins {} block

 @ line 1, column 11.
   plugins { greet }
g
check that you don’t have other problems, that you greet plugin exist and available
or share your project
s
ill try to upload it
g
I will try to check if you share it, or just some self-contained example
s
g
You use too old Gradle
You need at least Gradle 5
better use 5.3 which supports static accessors and plugin block for precompiled script plugins
But also you don’t use build.gradle.kts! Your configs are build.gradle and they of course do not support backticks or static type accessors To use plugin from groovy, use standard, dynamic syntax:
Copy code
plugins { id("greet") }
also your GreetPlugin.kt in a wrong directory
move it from
buildSrc/src/main
to
buildSrc/src/main/kotlin
s
g
What exactly did you copied? This sample has Greeting plugin in src/main/kotlin, it uses build.gradle.kts, so everything work there as expected
GreetPlugin.kt itself is fine
but also this is not precompiled plugin
s
i would have cloned it but i didnt want to copy every sample in there as im only interested in the buildSrc-plugin folder
g
okay, I just mean that your sample has problems, so this is the reason why it doesn’t work
s
ok
so how would i fix this so it works
g
created PR
s
ok
g
First thing about Gradle is actually not true if you don’t use precompiled script plugins
s
o.o
g
You use too old Gradle
I mean this. For your particular sample it’s fine to use Gradle 4.10
s
how long does everything take to compile
as im still
refreshing 'sample' gradle project
g
a couple seconds for first time
s
after pulling your PR
g
this looks as some AS sync issue
s
about 6 or so mins ago
g
refreshing ‘sample’ gradle project
this is not a gradle messaage
looks as some Android Studio problem
try to build from console. restart AS etc
also it may be related to new Gradle, AS downloads it and other dependencies, on slow connection it may be a long operation. check your IDE logs first
s
message has been deleted
g
I don’t know what is that, I would just restart AS
looks as some network issue
when you download dependencies
s
and now its indexing
took 13 mins lol
welp lets see if this works
waits for it to finish indexing first
g
Because it downloaded new gradle
not sure why it so long in your case, but when you update gradle, it download gradle, dependencies and index them
s
ok
imma just chalk it up to it needed to rebuild everything lmao
to apply it would i do
TASK.dependsOn greet
g
not sure what you mean
s
and nothing happens...
oh nvm i was on the Sync tab
Copy code
> Task :app:greet
I'm app.
g
👍
s
if i want to publish this would i need to include the buildSrc folder? or would i only need to obtain the .jar file that is produced where ever that is
g
See, I replaced binary plugin to precompiled plugin to show that it requires much less boiler plate to cofigure and use https://github.com/mgood7123/SamplePlugin/pull/2/files
s
as other users may also have a buildSrc folder in their projects which i do not want to overwrite if possible
g
if i want to publish this would i need to include the buildSrc folder
publish plugin or your project?
s
plugin
g
as other users may also have a buildSrc folder in their projects which i do not want to overwrite if possible
Not sure what you mean. buildSrc is part of project build configs, you do not publish your build configs, you publish project that builded using your build configs
s
also .kts cannot use external dependancies right? or does that only apply to build.gradle.kts
g
you can publish plugin from buildSrc
also .kts cannot use external dependancies right? or does that only apply to build.gradle.kts
It can
s
ok
are there any advantages over .kt as opposed to .kts? and vice versa
g
Less boilerplate
type safe accessors
what I already mentioned a few times
s
ok
g
check docks they perfectly explain rationale and show examples
also see my PR, you can see that it’s exactly the same, but less code
s
if i wanted to include a dependancy would i do that as i would for any other kotlin module?
g
no
You can declare plugin dependencies in buildSrc/build.gradle, or for precompiled plugin (kts) in plugins block
s
for example, if i wanted to include gson as a dependancy
g
add it to buildSrc/build.gradle dependencies block
think about buildSrc as about any other Gradle module, you can define dependencies in a same way
s
well porting buildSrc failed
Copy code
Executing tasks: [:app:assembleDebug]

> Task :buildSrc:compileKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compileKotlin'.
> Could not resolve all files for configuration ':buildSrc:kotlinCompilerPluginClasspath'.
   > Could not find org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.2.61.
     Searched in the following locations: file:/home/macropreprocessor/.gradle/caches/4.10.1/embedded-kotlin-repo-1.2.61-2/repo/org/jetbrains/kotlin/kotlin-scripting-compiler-embeddable/1.2.61/kotlin-scripting-compiler-embeddable-1.2.61.jar
     Required by:
         project :buildSrc
   > Could not find org.jetbrains.kotlin:kotlin-sam-with-receiver:1.2.61.
     Required by:
         project :buildSrc

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at <https://help.gradle.org>

BUILD FAILED in 2s
g
Do you have
repositories{}
block in your buildSrc/build.gradle?
s
no
forgot to change it back lol
124 replies so far lmao
g
¯\_(ツ)_/¯
s
ok the plugin works
now to try and port my buildscript to it lol
it fails ;-;
Unresolved reference: rootDir
Unresolved reference: file
Unresolved reference: projectDir
Copy code
import org.gradle.api.GradleException
import java.io.*
import java.nio.file.Files.*
import java.util.*

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import org.gradle.api.Plugin
import org.gradle.api.Project

import org.gradle.kotlin.dsl.*
file( can be fixed by replacing it with File(
but i dont know how to fix the projectDir and rootDir
g
use
project.rootDir
instead
s
ok
now i get
Copy code
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> More than one file was found with OS independent path 'META-INF/better-parse.kotlin_module'
removing the dependancy fixes it
0.0 yay instancing a ByteBuffer works in my class as a plugin
as in kotlin-dsl (build.gradle.kts) , for some reason, a ByteBuffer cannot be instanced inside a class inside a class/function
g
This is not a problem of ByteBuffer, but problem that you created class
lexer
in a script and task from other module tries to access it, it wrapped by Gradle script environmet just extract it to own file in buildSrc/src/kotlin and everything will be fine
s
ok
how do i produce a jar file of my plugin
g
For publishing?
s
yea
g
Use java-gradle-plugin plugin to publish your plugin, as in your original example, it also work with precompiled plugins
see also docs of java-gradle-plugin for more info how configure publishing and which task to use
s
as this is my current plugin: https://github.com/mgood7123/kpp
g
you know, I now realized, that I’m actually not sure that java-gradle-plugin is required in this case, maybe just maven-publish is enough
s
ok
g
ohh
I see, it required, but no additional configuration except repositories needed
just apply java-gradle-plugin and maven-publish, than configure publishing block
s
so many changes x.x
g
I just never published precompiled script plugin, use it only for local buildSrc, so not sure how it should work
s
Line 27: versionMapping { ^ Unresolved reference: versionMapping
g
What is versionMapping?
s
g
I don’t know why it’s not resolved, try check in IDE
yeah, already got it
Maybe requires some new version of Gradle
s
if i remove it i get
Copy code
Line 33:                 properties.set(mapOf(
                                          ^ Type mismatch: inferred type is Map<TypeVariable(K), TypeVariable(V)> but String! was expected
g
what is properties?
if it Gradle project properties than you cannot set map I think
s
Copy code
properties.set(mapOf(
                    "myProp" to "value",
                    "prop.with.dots" to "anotherValue"
                ))
g
Which version of Gradle do you use?
because this doc uses very new static version of
pom
config, which available onluy from Gradle 5.2, as I remember
s
5.1 i think
g
It’s not available in 5.1, see version of documentation for 5.1 https://docs.gradle.org/5.1/userguide/publishing_maven.html
same for versionMapping
s
.gradle/4.10.1
g
than of course it doesn’t work
Use version of documentation for your version or update Gradle
as you can see 4.10 even don’t have Kotlin DSL samples
s
;-;
Copy code
> Task :buildSrc:compileKotlin UP-TO-DATE
> Task :buildSrc:compileJava NO-SOURCE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors UP-TO-DATE
> Task :buildSrc:processResources UP-TO-DATE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:inspectClassesForKotlinIC UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestKotlin NO-SOURCE
> Task :buildSrc:pluginUnderTestMetadata UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:validateTaskProperties UP-TO-DATE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'kpp'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find com.android.tools.build:gradle:5.3.1.
     Searched in the following locations:
       - <https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.pom>
       - <https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.jar>
       - <https://jcenter.bintray.com/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.pom>
       - <https://jcenter.bintray.com/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.jar>
     Required by:
         project :

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at <https://help.gradle.org>

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See <https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings>

CONFIGURE FAILED in 9s
Could not find com.android.tools.build:gradle:5.3.1.
Searched in the following locations:
  - <https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.pom>
  - <https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.jar>
  - <https://jcenter.bintray.com/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.pom>
  - <https://jcenter.bintray.com/com/android/tools/build/gradle/5.3.1/gradle-5.3.1.jar>
Required by:
    project :
Open File
g
what is that?
Gradle 5.3 is not a dependency
you should update version of gradle in
gradle/wrapper/gradle-wrapper.properties
ohhh
I see what you did
s
ok
g
you updated Android Gradle plugin
but you have to update Gradle itself
s
ok
rip
versionMapping is still undefined
g
with gradle 5.3.1?
s
i think?
gradle is in my root dir
get it working for me lol
g
gradle is in my root dir
Check that your IDE uses gradle wrapper, not standalone version
s
well assuming my build JAR is located at
/home/macropreprocessor/AndroidStudioProjects/kpp/buildSrc/build/libs/buildSrc.jar
how would i go about adding it to an existing project and invoking its task
as i tried
file
>
new
>
new module
>
import .JAR/AAR
then adding
preBuild.dependsOn KOTLIN_PRE_PROCESSOR
to the end of
app/build.gradle
but i get
Could not get unknown property 'KOTLIN_PRE_PROCESSOR' for project ':app' of type org.gradle.api.Project.
g
no, it doesn’t make sense to attach jar
publish your Gradle plugin (to local maven, or to some remote repo) and apply it using standard Gradle API
existing project
Am I understand corretly, existing project which is not a part or submodule of project kpp?
s
yes
g
Than you have to publish it
s
as in for example, create a new project, then import the plugin into that project
g
See, plugin is similar to any other dependency, just dependency of build script. So if you want to reuse the same gradle plugin in not related projects, than publish this plugin somewhere, to local maven repo, but in this case you can use it only on the same computer or publish to some remote repo After that apply it as any other Gradle plugin
s
;-;
cant you create a PR for this? as it wont work for me
Copy code
class kpp : Plugin<Project> {

    override fun apply(project: Project): Unit = project.run {

        tasks {
            register("KOTLIN_PRE_PROCESSOR") {
                group = "kotlin pre processor"
                description = "kotlin pre processor"
                doLast {
                    globalVariables.INITPROJECTDIR = projectDir
                    globalVariables.INITROOTDIR = rootDir
                    println("starting KOTLIN_PRE_PROCESSOR")
                    find_source_files(globalVariables.INITPROJECTDIR.toString(), "kt")
                    println("KOTLIN_PRE_PROCESSOR finished")
                }
            }
        }
    }
}
buildscripts are so much easier to export and import ;-;
as literally all i would need to do is copy the script to a directory, say kpp, then add this to my apps build.gradle
Copy code
apply from: '../kpp/build.gradle.kts'

preBuild.dependsOn KOTLIN_PRE_PROCESSOR
sadly buildscripts do not support the full capabilities of kotlin, such as external dependancies, local import's, and other bugs that would otherwise be non-present in the official kotlin plugin
e
Just came here to say a big THANK YOU to @gildor for sticking through this entire thread. Truly inspiring how you gave time and thought to help 🙏
g
Precompiled script plugins support all those features and easy to apply, just use plugins block
Thanks @efemoney, I'm very appreciate this!
@Smallville7123 I send PR with changes that I believe make sense to reduce boilerplate on which you complained https://github.com/mgood7123/kpp/pull/1
As you can see it’s actually easy to use precompiled plugins, it doesn’t add any additional boilerplate and more type safe than apply script plugins
s
ok
also what do you mean by "boilerplate on which you complained"
g
buildscripts are so much easier to export and import ;-;
I agree, that if you use binary plugin you need unnecessary configuration (in build.src) and use plugin just to register task, but with precompiled script plugin you have best from both worlds
s
so with the build.gradle.kts i can just copy the entire buildSrc to my other project then invoke it as i previously did with the original build.gradle.kts ?
g
Yes, just copy buildSrc and apply kpp plugin where you need this task
s
ok, ill try that
g
Next step, if you don’t want to copy-paste code, you have 2 options: 1. Extract plugin to own project and than connect it using composite build to any of your projects 2. Publish plugin to local or remote repository
s
Copy code
FAILURE: Build failed with an exception.

* Where:
Script '/home/macropreprocessor/AndroidStudioProjects/kpp-backup/kpp/buildSrc/build.gradle.kts' line: 1

* What went wrong:
The plugins {} block must not be used here. If you need to apply a plugin imperatively, please use apply<PluginType>() or apply(plugin = "id") instead.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at <https://help.gradle.org>

CONFIGURE FAILED in 0s
The plugins {} block must not be used here. If you need to apply a plugin imperatively, please use apply<PluginType>() or apply(plugin = "id") instead.
app/build.gradle:
Copy code
apply from: '../kpp/buildSrc/build.gradle.kts'

preBuild.dependsOn KOTLIN_PRE_PROCESSOR
g
This snippet doesn’t make any sense for me
You should never apply build.gradle.kts files manually
also for buildSrc it doesn’t make any sense
what do you want to do? apply kpp plugin?
s
yes
g
Why you don’t do this how in my PR?
apply plugin: 'kpp'
or just
Copy code
plugins { id("kpp") }
apply from: ‘../kpp/buildSrc/build.gradle.kts’
This is just wrong, you shouldn’t apply build.gradle.kts of another project, including buildSrc
s
Plugin with id 'kpp' not found.
g
But it works in my PR
Do you use changes from my PR?
s
probably cus the buildSrc is being build and automatically included somehow in the project itself
g
Could you please explain in details what you want to achieve, what is your use case, so I could help you
s
i did
[macropreprocessor@macro-pc kpp-backup]$ git clone <http://github.com/mgood7123/kpp>
g
you copied whole buildSrc to another project, correct?
s
yes
g
and you applied kpp plugin in this project?
s
/home/macropreprocessor/AndroidStudioProjects/kpp-backup/kpp/buildSrc
g
it doesn’t tell me anything without other context
buildSrc must be in root directory of your project
s
buildSrc is not at the actual project's rootDir
g
maybe because root dir is
kpp-backup
, not
kpp-backup/kpp
s
i know
g
so? Than it shouldn’t work
Did you read documentation of buildSrc?
For multi-project builds there can be only one buildSrc directory, which has to sit in the root project directory
s
ok
ill clone it into a new project
and i immediatly get
Download <https://services.gradle.org/distributions/gradle-5.3.1-bin.zip> (83.20 MB)
g
sure
it downloads Gradle
s
ok
g
than this Gradle version will be cached and may be used by any projects on your computer
s
g
what does it mean?
s
btw that assumes a java based plugin right?
and not a kotlin based plugin?
g
I don’t understand your question
I just don’t understand context of your question
maven-publish
maybe used to publish any type of project
s
god, java is using up all my RAM x.x 7.2GB of 7.3GB, 600.2MB swap
g
Probably you leak in your plugin, or something like that, just kill java processes and try again
s
message has been deleted
g
Most probably you just have many different versions of Gradle Daemon
s
oh
Copy code
plugins {
    `kotlin-dsl`
    `java-library`
    `maven-publish`
    signing
}

group = "com.example"
version = "1.0"

tasks.register<Jar>("sourcesJar") {
    from(sourceSets.main.get().allJava)
    archiveClassifier.set("sources")
}

tasks.register<Jar>("javadocJar") {
    from(tasks.javadoc)
    archiveClassifier.set("javadoc")
}

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            artifactId = "my-library"
            from(components["java"])
            artifact(tasks["sourcesJar"])
            artifact(tasks["javadocJar"])
            versionMapping {
                usage("java-api") {
                    fromResolutionOf("runtimeClasspath")
                }
                usage("java-runtime") {
                    fromResolutionResult()
                }
            }
            pom {
                name.set("My Library")
                description.set("A concise description of my library")
                url.set("<http://www.example.com/library>")
                properties.set(mapOf(
                    "myProp" to "value",
                    "prop.with.dots" to "anotherValue"
                ))
                licenses {
                    license {
                        name.set("The Apache License, Version 2.0")
                        url.set("<http://www.apache.org/licenses/LICENSE-2.0.txt>")
                    }
                }
                developers {
                    developer {
                        id.set("johnd")
                        name.set("John Doe")
                        email.set("<mailto:john.doe@example.com|john.doe@example.com>")
                    }
                }
                scm {
                    connection.set("scm:git:<git://example.com/my-library.git%22|git://example.com/my-library.git">)
                    developerConnection.set("scm:git:<ssh://example.com/my-library.git%22|ssh://example.com/my-library.git">)
                    url.set("<http://example.com/my-library/>")
                }
            }
        }
    }
    repositories {
        maven {
            // change URLs to point to your repos, e.g. <http://my.org/repo>
            val releasesRepoUrl = uri("$buildDir/repos/releases")
            val snapshotsRepoUrl = uri("$buildDir/repos/snapshots")
            url = if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
        }
    }
}

signing {
    sign(publishing.publications["mavenJava"])
}

tasks.javadoc {
    if (JavaVersion.current().isJava9Compatible) {
        (options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
    }
}

repositories {
    jcenter()
}
if this works where would i find the resulting file
as it builds ok but i dont see any new directories or files
g
which task do you use for publishing?
s
also how do i get it to delete the build directory of buildSrc/build as
Copy code
tasks.create<Delete>("clean2") {
    group = "clean"
    delete = setOf (
        "build"
    )
}
doesnt work
g
clean should work
but maybe your right, it doesn’t clean buildSrc
s
restructured the build directory
anyway, where would i find the files produced by maven-bublish
g
Which task do you use?
You publish plugin to some repo and can find it there, by default publishing to local maven repo is available
s
g
You published nothing if you didn't start publishing task 🤷‍♂️
s
how do i start it
g
./gradlew taskName
You can do the same from IDE
888 Views