Search code examples
javagradlegradle-kotlin-dsl

How do I reference buildsrc from src


This might be an XY problem but, I am working on a JavaCard project (Java Embedded) and I have a Kotlin Gradle build for the installation. I have some Kotlin code in my buildSrc folder, which is used to calculate some of the installation parameters. I know I can't use Kotlin code on my JavaCard itself.

I do have to pass some custom TLVs to the JavaCard in the installation parameters though, and I want a single source of truth for the Tag values.

My idea was to create a Java interface containing all custom tag values as constants. I want to use these constants in my gradle build, so I think I should put them in buildSrc. I also want to use this interface in my Configuration class that parses the incoming parameters. How do I reference buildSrc from src?

I tried creating a interface at buildSrc/src/main/java/<same_package_as_src>/InstallTags, but the code doesn't see it. NB: buildSrc/src/main/kotlin should not be included in the main project


Solution

  • The "right" way to share code between the build and the main project would be to make use of an included build. Then you can import both a plugin and a module from that included build.

    You can do what you've suggested though, even though it's an anti-pattern. You can add the compiled buildSrc Kotlin code1 as dependency of your main code by writing:

    dependencies {
        implementation(
            files(rootProject.projectDir.resolve("buildSrc/build/classes/kotlin/main"))
        )
    }
    

    This a bit of a 'hack' and it won't add in any dependencies the buildSrc code might need. However, in a simple case, this may be acceptable.


    1 For any Java code, simply replace kotlin with java in the path