I'm trying to get my project ready for use with co...
# gradle
s
I'm trying to get my project ready for use with configure on demand and configuration cache. I've been told that grabbing tasks from another project, and then having your task depend on them is an antipattern. Instead I'm trying to use the artifact syntax. Is this the correct way to use the artifact syntax? Code in 🧵
Producing Project:
Copy code
val productionJsBuildTask = tasks.getByName<KotlinWebpack>("jsBrowserProductionWebpack")

val jsFiles by configurations.creating {
    isCanBeConsumed = false
    isCanBeResolved = true
}

artifacts {
    add("jsFiles", productionJsBuildTask.outputDirectory) {
        builtBy(productionJsBuildTask)
    }
}
Consuming Project:
Copy code
val copyJsResources by tasks.register("copyJsResources", Copy::class) {
    from(project(":producing-project").configurations.getByName("jsFiles").artifacts.files)
    into("$projectDir/assets/")
}

tasks["installDist"].dependsOn(copyJsResources)
tasks["run"].dependsOn(copyJsResources)
e
no
see https://docs.gradle.org/current/userguide/cross_project_publications.html, the simplest setup would be something along the lines of producer
Copy code
val jsFiles by configurations.creating {
    isCanBeConsumed = false
    isCanBeResolved = true
    attributes { ... }
}
consumer
Copy code
val jsFiles by configurations.creating {
    isCanBeConsumed = true
    isCanBeResolved = false
    attributes { ... }
}
dependencies {
    jsFiles(project(":producing-project"))
}
and you can choose whatever attributes you want as long as they are unambiguous, even
Copy code
attributes {
    attribute(Usage.USAGE_ATTRIBUTE, objects.named("js"))
}
would work
🏆 1
👍 1
1
🔖 1
v
Why do you think this is simpler than referencing the configuration in the consumer and not using attributes? (Of course the right way as documented in given link, not the bad way shown in OP)
e
it's broken in a number of use cases (publication, as mentioned in the docs, and also a couple of plugins I've used in the past for scanning dependencies have broken when using
project(path:configuration:)
notation)
👀 1
so I should have wrote "the simplest thing that works reliably"
m
I wish that snippet above would be highlighted in the docs. The docs are overwhelming when it comes to cross-project configuration. Takes a while to process and this snippet is relatively simple 👍
plus1 1
v
I personally did not have problems with the configuration referencing as long as it is not for publication but just within the same build. 🙂
s
thank you for your answers. How would I actually get access and copy the compiled files to the assets directly like I'm doing during the compilation?
e
replace
from(cross-project configuration)
with
from(local project configuration)
s
So instead of
Copy code
val copyJsResources by tasks.register("copyJsResources", Copy::class) {
    from(project(":producing-project").configurations.getByName("jsFiles").artifacts.files)
    into("$projectDir/assets/")
}
It should be
Copy code
val copyJsResources by tasks.register("copyJsResources", Copy::class) {
    from(project.configurations.getByName("jsFiles").artifacts.files)
    into("$projectDir/assets/")
}
?
e
if you have
val jsFiles by configurations.creating
then just
from(jsFiles)
is fine
you shouldn't need
.artifacts.files
in this usage either
s
ok, thank you
@ephemient Hey, we got around to trying your solution, but then it seems like we have a JVM module trying to use dependencies from a JS project, when really, we just want to copy over the JS files. When we use your approach and then sync, we get the following error while running:
Copy code
* What went wrong:
Execution failed for task ':image-gen-server:copyJsResources'.
> Could not resolve all files for configuration ':image-gen-server:jsFiles'.
   > Could not resolve org.jetbrains.skiko:skiko:0.7.63.
     Required by:
         project :image-gen-server > project :image-gen-canvas > org.jetbrains.compose.foundation:foundation:1.4.3 > org.jetbrains.compose.foundation:foundation-desktop:1.4.3
         project :image-gen-server > project :image-gen-canvas > org.jetbrains.compose.material:material:1.4.3 > org.jetbrains.compose.material:material-desktop:1.4.3 > org.jetbrains.compose.ui:ui:1.4.3 > org.jetbrains.compose.ui:ui-desktop:1.4.3
         project :image-gen-server > project :image-gen-canvas > org.jetbrains.compose.material:material:1.4.3 > org.jetbrains.compose.material:material-desktop:1.4.3 > org.jetbrains.compose.ui:ui-text:1.4.3 > org.jetbrains.compose.ui:ui-text-desktop:1.4.3
         project :image-gen-server > project :image-gen-canvas > org.jetbrains.compose.material:material:1.4.3 > org.jetbrains.compose.material:material-desktop:1.4.3 > org.jetbrains.compose.ui:ui:1.4.3 > org.jetbrains.compose.ui:ui-desktop:1.4.3 > org.jetbrains.compose.ui:ui-graphics:1.4.3 > org.jetbrains.comp
ose.ui:ui-graphics-desktop:1.4.3
      > Cannot choose between the following variants of org.jetbrains.skiko:skiko:0.7.63:
          - androidRuntimeElements-published
          - awtRuntimeElements-published
        All of them match the consumer attributes:
          - Variant 'androidRuntimeElements-published' capability org.jetbrains.skiko:skiko:0.7.63:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Provides org.jetbrains.kotlin.platform.type 'jvm' but the consumer didn't ask for it
                  - Provides ui 'android' but the consumer didn't ask for it
          - Variant 'awtRuntimeElements-published' capability org.jetbrains.skiko:skiko:0.7.63:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Provides org.jetbrains.kotlin.platform.type 'jvm' but the consumer didn't ask for it