3

I have been trying to create a multimodule gradle project, I have referred to these link apart from a few others

I was able to create multiple modules and execute multiple commands for all modules and for specific modules using root level gradle wrapper, something like ./gradlew :java-libs:payment-lib:build works.

I am also publishing these libraries to a nexus repository that acts as an internal maven repository. This is what my root build.gradle looks like:

apply plugin: 'base'

subprojects {
    apply plugin: 'java'
    apply plugin: 'maven-publish'

    publishing {
        publications {
            maven(MavenPublication) {
                artifact("build/libs/$project.name-$version" + ".jar") {
                    extension 'jar'
                }
            }
            mavenJava(MavenPublication) {
                from components.java
            }
        }

        repositories {
            maven {
                name 'nexus'
                url "<nexus_url>/repository/maven-snapshots/"
                credentials {
                    username nexusUsername
                    password nexusPassword
                }
            }
        }
    }
}

This enables me to publish any or all modules to nexus from the root gradlew, and I don't have to write the publications section in each module again and again.

What I want to achieve

As of now each module is treated as a separate project in nexus, with its own versioning, its own folder, and when 2 of these modules need to be imported in a different project, I need to include 2 statements that are unrelated to each other.

compile("com.something.payment-lib:0.3-SNAPSHOT")
compile("com.something.refund-lib:0.2-SNAPSHOT")

I have been using some external libraries where I was able to do something like this:

compile("org.apache.httpcomponents:httpclient:4.5.12"){
    exclude(module: 'commons-logging')
}

Which suggests that all modules within the multi-module gradle project are under a single group org.apache.httpcomponents:httpclient, which also has a version of its own. And I can include/exclude specific modules within that group.
I tried looking at its code, but it is using maven and the config looks quite different.

I also don't know even if I am able to achieve such a grouping, what will the folder structure look like inside the nexus repository. Would there be a folder for the project and then there would be sub-folders with the jars of the respective modules?

neeraj
  • 1,191
  • 4
  • 19
  • 47

1 Answers1

0

I think what you're missing is declared dependencies to subprojects in the root build.gradle. Take a look here https://docs.gradle.org/current/userguide/declaring_dependencies_between_subprojects.html#declaring_dependencies_between_subprojects

Adding

dependencies {
    api(project(":payment-lib"))
    api(project(":refund-lib"))
}

to the root build.gradle.

Should give you access to both modules, by just declaring the dependency to the root project.

Tuan Pham
  • 1,171
  • 2
  • 15
  • 27
  • this doesn't work. though i couldn't find any resources for this method of implementation, I tried using ``` dependencies { api(project(":payment-lib")) api(project(":refund-lib")) } ``` replaced api with compile and implementation as well, but i got the same error `could not find method api() for arguments [project ':payment-lib'] on object of type org.gradle.api.artifacts.dsl.DefaultDependencyHandler ` – neeraj Jul 12 '21 at 09:25
  • i also added the 'group' and 'version' clauses to the root build.gradle because i eventually want to version the whole project as well – neeraj Jul 12 '21 at 09:28
  • You will need to declare the java-library plugin to be able to use `api`. https://docs.gradle.org/current/userguide/java_library_plugin.html. However, if I'm correct `compile` should work without the error. What version of Gradle are you using? – Tuan Pham Jul 15 '21 at 13:33
  • gradle version 6.8.3 – neeraj Jul 16 '21 at 09:23
  • 1
    same error when i use java-library plugin – neeraj Jul 16 '21 at 09:25