Home > Software engineering >  Export Gradle subproject in jar without adding it to dependencies
Export Gradle subproject in jar without adding it to dependencies

Time:04-06

I have a Gradle project with 3 subprojects : core, 1_13, and 1_14. The 1_13 and 1_14 depends on the core project, but the final build has to be done on the core project, and I wanted to include the builds of the 1_13 and 1_14 in the jar. The 1_13 and 1_14 subprojects have deps that aren't in the core subproject.

Actually, I use the sourceSets to include the source files of the 1_13 and 1_14 projects, but the fact that there are dependencies in the subprojects that doesn't exist in the core subproject, and that I can't use apiElements in the dependencies of the core subproject, because if I do a circular import occurs gets me stuck.

Actually, I use this :

sourceSets {
    main {
        java {
            srcDirs 'src', '../1_13/src', '../1_14/src'
            resources {
                srcDir 'resources'
            }
        }
    }
}

But because the libs are not present in the core subprojects, the build fails. I precise that I only need the libs at the compile time, and not at runtime. I also can't add the libs to the core subproject because the submodules uses different versions of the same library. The core module is only here to route on whether one or another package should be used.

CodePudding user response:

The cleanest way would be to extract the common logic that is used by 1_13 and 1_14 to a new project (e.g. common) and add 1_13 and 1_14 as real dependencies to the core project.

You could of course hack something like „use the dependencies of the subprojects as dependencies of the core project“ but hacks like this usually cause more trouble later on.

CodePudding user response:

After continuing to search for a while, I found the way to do exactly what I want using a Gradle task.

Actually, I created a task to combine all the jar files of the different modules :

task buildPlugin(type: Jar, group: 'build') {
    // Change the archive name and include all outputs of build of modules
    archiveFileName = "${rootProject.name}.jar"
    from getRootProject().allprojects.findAll().sourceSets.main.output

    // Delete the old jar of the core module, to keep only the finally build jar file
    doLast {
        var oldJar = new File(project.buildDir, "libs/"   project.name   "-"   project.version   ".jar")
        if (oldJar.exists()) {
            oldJar.delete()
        }
    }
}

Thank you for your answers anyway :) !

  • Related