maven-publish
是一个Gradle插件,可以我们的编译的输出物(artifacts)发布到Apache Maven仓库当中,例如aar
,jar
等library发布到仓库当中,我们可以通过gradle或者maven进行远程依赖使用。
在需要用到的模块的build.gradle文件加入这句
apply plugin: 'maven-publish'
引入插件后,我们可以扩展一些我们自定义的属性以及任务。引入插件的模块中,我们可以通过复写publishing
节点(后面会有example)。
pulishing 内部,我们可以复写两个配置publications
和repositories
。
顾名思义,就是配置maven-publishing插件的输出物。
publishing {
publications {
myPublicationName(MavenPublication) {
// Configure the publication here
}
}
}
publicaions主要包含四种类型的配置项:
MavenPublication
example1:
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'project1-sample'
version = '1.1'
from components.java
}
}
}
example2:
publishing {
publications {
mavenJava(MavenPublication) {
pom {
name = 'My Library'
description = 'A concise description of my library'
url = 'http://www.example.com/library'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'johnd'
name = 'John Doe'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:git://example.com/my-library.git'
developerConnection = 'scm:git:ssh://example.com/my-library.git'
url = 'http://example.com/my-library/'
}
}
}
}
}
example3:
apply plugin: "java"
apply plugin: "maven-publish"
task sourceJar(type: Jar) {
from sourceSets.main.allJava
classifier "sources"
}
publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar
pom {
name = "Demo"
description = "A demonstration of Maven POM customization"
url = "http://www.example.com/project"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "johnd"
name = "John Doe"
email = "[email protected]"
}
}
scm {
connection = "scm:svn:http://subversion.example.com/svn/project/trunk/"
developerConnection = "scm:svn:https://subversion.example.com/svn/project/trunk/"
url = "http://subversion.example.com/svn/project/trunk/"
}
}
}
}
}
ps:详细参考这里
顾名思义,就是用来配置maven-publishing仓库的信息,即输出目的地
仓库的配置相对简单,只需要两项:
example1:
publishing {
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url = "$buildDir/repo"
}
}
}
example2:
publishing {
repositories {
maven {
def releasesRepoUrl = "$buildDir/repos/releases"
def snapshotsRepoUrl = "$buildDir/repos/snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
}
配置完以上信息之后,就可以在gradle的Task列表中看到我们的task了。双击便可执行。
publishToMavenLocal是maven-publish
插件自动创建的的一个任务,它会将Moudle,POM,和metadata信息发布到$USER_HOME/.m2/repository
。
plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
}
group = 'com.example'
version = '1.0'
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier = 'sources'
}
task javadocJar(type: Jar) {
from javadoc
archiveClassifier = 'javadoc'
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'my-library'
from components.java
artifact sourcesJar
artifact javadocJar
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
name = 'My Library'
description = 'A concise description of my library'
url = 'http://www.example.com/library'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'johnd'
name = 'John Doe'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:git://example.com/my-library.git'
developerConnection = 'scm:git:ssh://example.com/my-library.git'
url = 'http://example.com/my-library/'
}
}
}
}
repositories {
maven {
// change URLs to point to your repos, e.g. http://my.org/repo
def releasesRepoUrl = "$buildDir/repos/releases"
def snapshotsRepoUrl = "$buildDir/repos/snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
}
signing {
sign publishing.publications.mavenJava
}
javadoc {
if(JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}
对应的生成:
The POM: my-library-1.0.pom
The primary JAR artifact for the Java component: my-library-1.0.jar
The sources JAR artifact that has been explicitly configured: my-library-1.0-sources.jar
The Javadoc JAR artifact that has been explicitly configured: my-library-1.0-javadoc.jar