How can I define a custom component that its output can be used as a Maven artifact?

Basically, in the Maven publish plugin docs, there’s the following example snippet:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

How can I define a task that exposes something like components.foo and use it with the Maven publish plugin?

Thanks in advance.