问题
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.
回答1:
In my case I fixed that error message adding this:
import org.gradle.api.publish.maven.MavenPublication
But then faced other issues and finally ended up using the nebula-publishing-plugin, solving it all
回答2:
This worked for me, with gradle 4.10 and maven-publish.
In my custom plugin .groovy:
import org.gradle.api.publish.maven.MavenPublication
...
project.extensions.configure PublishingExtension, new ClosureBackedAction( {
publications{
mycustompub(MavenPublication) {
groupId = project.group
artifactId = <artifact name>
artifact source: <path to artifact>, extension: 'zip'
}
}})
回答3:
The closure you pass to project.publishing.publications does not have project
in its context when it executes. This means MavenPublication
is not in the context of the closure, even though you apply maven-plugin
to project
.
If you write the plugin in your build script, it will have project
as context by default, but the context is different when writing it as a standalone plugin.
You can setDelegate(project)
on a closure to set the context, then pass it:
def publicationsClosure = {
maven(MavenPublication) {
groupId project.group
artifactId project.name
version=project.version
from components.java
}
}
publicationsClosure.setDelegate(project)
project.publishing.publications(publicationsClosure)
I had the same error in a plugin and eventually realized what went on when I read this article: http://trickyandroid.com/gradle-tip-2-understanding-syntax/
回答4:
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 zip 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.
来源:https://stackoverflow.com/questions/38748458/gradle-using-maven-publish-plugin-in-custom-standalone-plugin