Christian Quast Christian Quast - 3 months ago 57
Groovy Question

Gradle: Using 'maven-publish' plugin in custom standalone plugin

Almost feeling like spamming after my third question, cause my questions seem so trivial. But I just don't find any help in Gradle Docs for my cases and also other asked questions on stackoverflow weren't helping.

This time: I can't use the

maven-publish
plugin in my own standalone custom plugin. The code of my plugin so far:

package com.sample.gradle;

import org.gradle.api.Project;
import org.gradle.api.Plugin;

public class SampleGradlePlugin implements Plugin<Project> {
public void apply(Project project) {

project.apply plugin: 'application'
project.apply plugin: 'maven-publish'
project.apply plugin: 'maven'

project.publishing.publications {
maven(MavenPublication) {
groupId project.group
artifactId project.name
version=project.version
from components.java
}
}

project.uploadArchives {
repositories {
mavenDeployer {
repository(url: project.Repo_Upload_Internal){
authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
}
snapshotRepository(url: project.Repo_Upload_Snapshot){
authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
}
}
}
}
//this try/catch is not important for my question
try{
project.repositories {
maven {
url project.Repo_Gp_Internal
credentials {
username project.Repo_Gp_User;
}
}
}
}
catch(MissingPropertyException e){
println "Info: '"+e.getProperty()+"' not declared in \\"+project.name+"\\gradle.properties"
}
}
}


The
publishing
closure is the important part that brings me this error, when I use my plugin in one of my projects:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\quastc\Desktop\Gradle_Projects\Application_RootProject\buil
d.gradle' line: 3

* What went wrong:
A problem occurred evaluating root project 'Application_RootProject'.
> Failed to apply plugin [id 'sample.sample-groovy-plugin']
> Cannot create a Publication named 'MavenPublication' because this container
does not support creating elements by name alone. Please specify which subtype
of Publication to create. Known subtypes are: MavenPublication


Where's my mistake? What did I forget?

If somebody answers, PLEASE let me know HOW you got your knowledge.

Answer

Found a solution that works for me right now. I don't use the maven-publish plugin anymore. Just the maven plugin. Then I put this code at the end of my own plugin:

    project.configurations {
        deployerJars 
    }
    project.dependencies {
        deployerJars "org.apache.maven.wagon:wagon-http:2.2"
    }   
    project.uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: project.Repo_Upload_Internal){
                    authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                }        
                snapshotRepository(url: project.Repo_Upload_Snapshot){
                    authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                }
            }
        }
    }

Further:

I haven't found a way to suppress the uploading of a jar and a tar file. I still need some copy&paste code in the several build.gradle files to do this job. It's this code:

configurations.archives.with {
    artifacts.remove artifacts.find { it.archiveTask.is distZip }
    artifacts.remove artifacts.find { it.archiveTask.is distTar }
}

I'm not able to change that code to work in my custom plugin yet.

Comments