Does the `kotlin("jvm")` plugin automatically appl...
# gradle
s
Does the
kotlin("jvm")
plugin automatically apply the
java-library
plugin? I'm sure I remember reading that somewhere but I can't find a reference for it. I'm trying to debug an issue where I'm getting intermittent (!) "unresolved reference" errors when pulling in another module as an
api
dependency 😢 (both modules are libraries, consumed by a third separate module)
t
not the
JavaLibraryPlugin
but
JavaPlugin
(
JavaLibraryPlugin
applies
JavaPlugin
internally)
s
👍 thanks. In that case, I'm confused about why my
build.gradle.kts
file is able to add dependencies to the
api
configuration. I thought the
api
configuration was only available if the
java-library
plugin was added?
I guess what I'm really wondering is: if I'm writing a Kotlin library (for use in a multi-project build; not published), should I apply both the Kotlin plugin and the java-library plugin, or is the Kotlin plugin alone sufficient?
t
Kotlin plugin alone should be sufficient
s
Okay 👍. Why is that?
t
indeed only
JavaLibraryPlugin
plugins adds
api
configuration in case of Java only project. But Kotlin plugin itself, when applied, creates
api*
configurations
s
Thanks, that's good to know!
In the docs for the java-library plugin, it mentions that
A feature of the
java-library
plugin is that projects which consume the library only require the classes folder for compilation, instead of the full JAR.
Do you know if that's also true when building a Kotlin library? (Either with or without the
java-library
plugin applied)
Part of the reason I'm curious is because in the sample Kotlin library in the Gradle docs, they add both the Kotlin plugin and the
java-library
plugin. So that suggests the java-library plugin is still useful in some way.
🤔 1
t
I can't answer why Gradle example applies
java-library
as well. Probably example was not updated for some time
s
Do you think that applying the
java-library
plugin to a Kotlin project would do anything at all? Or would it have no effect?
c
A kotlin library project should apply the java-library plugin, as the artifact is a java library (JAR file containing compiled code, with Kotlin deets overlaid).
s
Thanks Chis, do you have any more info about why? What will the
java-library
plugin do in the case of a Kotlin project? (If it makes a difference, my use case is a multi-project build, so no published artifacts)
c
A Kotlin project is really just a Java project + Kotlin source code. even without publishing artifacts to a repository, those artifacts are shared internally between projects. Having said that, the java-library plugin adds very little on top of the Java plugin - if the issue with the `api`configuration, that is provided in the java plugin. Source for Java library plugin: https://github.com/gradle/gradle/blob/5ec3f672ed600a86280be490395d70b7bc634862/sub[…]ins/src/main/java/org/gradle/api/plugins/JavaLibraryPlugin.java
s
Thanks! So am I understanding correctly that the
api
configuration exists whenever the
java
plugin is applied, but its behaviour is different if the
java-library
plugin is also applied?
c
Correct. Additional configuration on
api
is performed by the Java library plugin:
Copy code
public static Configuration addApiToSourceSet(SourceSet sourceSet, ConfigurationContainer configurations) {
        Configuration apiConfiguration = maybeCreateInvisibleConfig(
            configurations,
            sourceSet.getApiConfigurationName(),
            "API dependencies for " + sourceSet + ".",
            false
        );

        Configuration compileOnlyApiConfiguration = maybeCreateInvisibleConfig(
            configurations,
            sourceSet.getCompileOnlyApiConfigurationName(),
            "Compile only API dependencies for " + sourceSet + ".",
            false
        );

        Configuration apiElementsConfiguration = configurations.getByName(sourceSet.getApiElementsConfigurationName());
        apiElementsConfiguration.extendsFrom(apiConfiguration, compileOnlyApiConfiguration);

        Configuration implementationConfiguration = configurations.getByName(sourceSet.getImplementationConfigurationName());
        implementationConfiguration.extendsFrom(apiConfiguration);

        Configuration compileOnlyConfiguration = configurations.getByName(sourceSet.getCompileOnlyConfigurationName());
        compileOnlyConfiguration.extendsFrom(compileOnlyApiConfiguration);

        return apiConfiguration;
    }
However, if your issue is intermittent it’s unlikely related to the above, as issues there would consistently fail.
👍 1
t
JavaPlugin
doesn't add
api
configurations. And I am still sure you don't need to add
java-library
plugin when Kotlin plugin is applied.
c
this call from the Java Library plugin creates the
api
configuration:
Copy code
JvmPluginsHelper.addApiToSourceSet(sourceSet, configurations);
a
I also do not think you need the java-library plugin. We have plenty of java libraries built with kotlin withiut the java-library plugin