Unable to publishtomavenlocal due to components.java

Viewed 295

Unable to publish to maven local with gradle 7.3.3.

project's build.gradle:

plugins {
    id 'java'
    id 'java-library'
    id 'maven-publish'
}

def gitBranch() {
    def branch = ""
    def proc = "git rev-parse --abbrev-ref HEAD".execute()
    proc.in.eachLine { line -> branch = line }
    proc.err.eachLine { line -> println line }
    proc.waitFor()
    branch
}

group = 'my.project'
version = gitBranch()

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

subprojects {
    project.version = gitBranch()
    apply plugin: 'maven-publish'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenLocal()
        mavenCentral()
        flatDir {
            dirs "../ZKM"
        }
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                artifactId = jar.archiveBaseName
            }
        }
    }

    repositories {
        mavenLocal()
        mavenCentral()
    }
}

This returns the error:

A problem occurred evaluating root project 'my-project'. Could not get unknown property 'java' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

1 Answers

You apply the java plugin only to the root project (“my-project”) of the build. The subprojects don’t seem to apply the plugin, hence components.java is unknown in their build configurations.

You can fix this by simply adding apply plugin: 'java' somewhere near the beginning of your subprojects { … } block.

Related