1

Here is my project structure:

- snow
  - snow-lib
    - snow-base-util
    - snow-web-common
  - snow-web
    - snow-user

This is the code, very simple. https://github.com/site-weic/snow

case 1

When I configure snow-web-common publish to Maven Local, it published successfully(branch case1).

  • snow-lib/snow-base-util/build.gradle is empty
  • snow-lib/snow-web-common/build.gradle
dependencies {
    api 'org.springframework.boot:spring-boot-starter-web'
    api project(':snow-lib:snow-base-util')
}
java {
    withJavadocJar()
    withSourcesJar()
}
publishing {
    publications {
        maven(MavenPublication) {
            groupId = project.group
            artifactId = project.name
            version = project.version

            from components.java
        }
    }
}

case 2

But configuring snow-base-util will report an error(branch case2)

A problem occurred evaluating project ':snow-lib:snow-web-common'.
> Maven publication 'maven' cannot include multiple components

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
  • snow-lib/snow-base-util/build.gradle
java {
    withJavadocJar()
    withSourcesJar()
}
publishing {
    publications {
        maven(MavenPublication) {
            groupId = project.group
            artifactId = project.name
            version = project.version

            from components.java
        }
    }
}
  • snow-lib/snow-web-common/build.gradle
dependencies {
    api 'org.springframework.boot:spring-boot-starter-web'
    api project(':snow-lib:snow-base-util')
}
java {
    withJavadocJar()
    withSourcesJar()
}
publishing {
    publications {
        maven(MavenPublication) {
            groupId = project.group
            artifactId = project.name
            version = project.version

            from components.java
        }
    }
}

case 3

Another error will be reported when I configure to publish two projects at the same time(branch case3)

Execution failed for task ':generateMetadataFileForMavenPublication'.
> Invalid publication 'maven':
    - Publication only contains dependencies and/or constraints without a version. You need to add minimal version information, publish resolved versions (https://docs.gradle.org/7.4/userguide/publishing_maven.html#publishing_maven:resolved_dependencies) or reference a platform (https://docs.gradle.org/7.4/userguide/platforms.html)

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
  • snow-lib/snow-base-util/build.gradle
java {
    withJavadocJar()
    withSourcesJar()
}
publishing {
    publications {
        maven(MavenPublication) {
            groupId = project.group
            artifactId = project.name
            version = project.version

            from components.java
        }
    }
}
  • snow-lib/snow-web-common/build.gradle
dependencies {
    api 'org.springframework.boot:spring-boot-starter-web'
    api project(':snow-lib:snow-base-util')
}

How to fix the gradle configuration? And, what is the best practice for gradle to package multi-module projects? Could you please provide some reference projects at Github or Books? Thanks!!!‍♂️

Wei
  • 451
  • 1
  • 5
  • 15
  • I don't know why, but when I add at least one explicit version dependency to the project(such as org.slf4j:slf4j-api:1.7.30) ,which has already been set in root/build.gradle, and then set unique task name such as `"$project.name"(MavenPublication)`, I can publish – Wei Mar 09 '23 at 02:14

0 Answers0