Issues with Gradle and packagePrefix

Answered

I am currently in the process of migrating a project with a custom build tool (classic case of Not Invented Here syndrome) to Gradle, and I'm having trouble getting package prefixes to work.

The project in question has a project structure something like this:


a/src
a/test
a/b/src
a/b/test
c/src
c/test

Classes in a/src have package name com.example.a, and classes in a/b/src have package name com.example.a.b

Neither Gradle nor IntelliJ individually seem to have issues with this setup. Gradle just compiles them without complaint, and in IntelliJ we have the option to set a package prefix (which is stored in the IML file).

The problem arises when trying to use them both simultaneously. Using the gradle idea plugin I can easily generate IML files that have the required package prefix setting in the XML:

idea {
    module {
        iml {
            withXml {
                it.asNode().component.content.sourceFolder.@packagePrefix = 'com.example.a';
            }
        }
    }
}

Which results in the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<module relativePaths="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true" LANGUAGE_LEVEL="JDK_1_8">
    <exclude-output/>
    <orderEntry type="inheritedJdk"/>
    <content url="file://$MODULE_DIR$/">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="com.example.a"/>
      <excludeFolder url="file://$MODULE_DIR$/.gradle"/>
      <excludeFolder url="file://$MODULE_DIR$/build"/>
    </content>
    <orderEntry type="sourceFolder" forTests="false"/>
  </component>
  <component name="ModuleRootManager"/>
</module>

But when I open the resulting project (or import it as a Gradle project), the package prefix remains empty in IntelliJ:

Is there any way to get the package prefix to be set correctly in IntelliJ?

Modifying the package structure to conform to a more sensible mainstream default (which I would love) is an insane amount of work for this project, and not currently an option I am allowed to explore (the custom build tool and Gradle need to be able to run side by side on the same codebase for a while).

0
5 comments

In Kotlin multiplatform project it works for `jvm` source sets but unfortunately doesn't seem to work for `common` source sets.

0

I have this working in build.gradle but not build.gradle.kts.  I tried adding functions to add it to the dsl but I don't have enough knowledge for this. Are there any pointers on getting it to work with the DSL?

0

Please try 

fun IdeaModule.settings(configure: ModuleSettings.() -> Unit) =
(this as ExtensionAware).configure(configure)

val ModuleSettings.packagePrefix: PackagePrefixContainer
get() = (this as ExtensionAware).the()

idea {
module {
settings {
packagePrefix["src/main/kotlin"] = "org.example"
}
}
}

Please feel free to also vote for this request: https://github.com/JetBrains/gradle-idea-ext-plugin/issues/44

0

Please sign in to leave a comment.