maven插件与maven-publish插件的区别
maven插件适用于gradle1.0-6.2版本,6.2版本后该插件就被废弃了,推荐使用maven-publish插件。
maven-publis插件是在gradle 1.3 版本后开始支持的,使配置更加简洁。
maven-publis插件的使用
砍柴不误磨刀工,我们先来看一下官方文档。
The Maven Publish Plugin
完整示例
下面配置展示如何签署和发布 Java 库,包括源代码、Javadoc 和自定义 POM:
plugins {id 'java-library'id 'maven-publish'id 'signing'
}group = 'com.example'
version = '1.0'java {withJavadocJar()withSourcesJar()
}publishing {publications {mavenJava(MavenPublication) {artifactId = 'my-library'from components.javaversionMapping {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 = 'john.doe@example.com'}}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/repodef releasesRepoUrl = layout.buildDirectory.dir('repos/releases')def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl}}
}signing {sign publishing.publications.mavenJava
}javadoc {if(JavaVersion.current().isJava9Compatible()) {options.addBooleanOption('html5', true)}
}
结果将发布以下工件:
-
POM:
my-library-1.0.pom
-
Java 组件的主要 JAR 工件:
my-library-1.0.jar
-
已显式配置的源 JAR 工件:
my-library-1.0-sources.jar
-
已显式配置的 Javadoc JAR 工件:
my-library-1.0-javadoc.jar
签名插件用于为每个工件生成签名文件。此外,将为所有工件和签名文件生成校验和文件。
publishToMavenLocal` 不会在$USER_HOME/.m2/repository .如果您想验证校验和文件是否已正确创建,或将其用于以后发布,请考虑使用 URL 配置自定义 Maven 存储库file:// 并将其用作发布目标。 |
当然有些配置你不需要,可以删除,比如删除下面配置:
id 'signing'
下面是我项目中 使用的配置,当然pom的配置你可以继续跟进需要进行删减。
group = 'cn.rock.spi'
version = '0.0.1'java {withJavadocJar()withSourcesJar()
}//gradle 官方文档:https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:complete_example
publishing {publications {mavenJava(MavenPublication) {artifactId = 'serviceloader'from components.javaversionMapping {usage('java-api') {fromResolutionOf('runtimeClasspath')}usage('java-runtime') {fromResolutionResult()}}pom {name = 'serviceloader library'description = 'A concise description of my library'url = 'http://www.baidu.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 = 'john.doe@example.com'}}scm {connection = 'scm:git:git://example.com/my-library.git'developerConnection = 'scm:git:ssh://example.com/my-library.git'url = 'http://baidu.com/my-library/'}}}}repositories {maven {url = uri('../repo')println("maven url $url")}}
}
从上图可以看出生成了doc文件和source文件,是因为下面的配置:
java {withJavadocJar()withSourcesJar()
}
maven-publish可以用的配置
一、下面是精简后的配置(基于官方推荐版本):
group = 'cn.rock.spi'
version = '0.0.1'publishing {publications {mavenJava(MavenPublication) {artifactId = 'serviceloader'from components.java}}repositories {maven {url = uri('../repo')}}
}
发布插件
这个时候,点击下Sync
同步下代码,然后可以在右侧Gradle
的Tab
中,找到publishing
,里面有publish
选项,我们双击它,就开始执行发布插件的操作了。
如果你的Android Studio 在配置后没有publishing的task,可以尝试clean下重新build。
如果还是不行,不要怀疑配置有问题,mac电脑有时候会出现这种问题,换window电脑马上就有这个task了。
第二种配置
如果你比较喜欢恋旧,下面的配置也是可以的。
afterEvaluate {publishing {//配置maven-publishing插件的输出物publications {mavenJava(MavenPublication) {groupId = 'cn.rock.spi'artifactId = 'serviceloader'version = '0.0.1'from components.java}}repositories {maven {url = uri('../repo')}}}
}
注意:
使用mavenJava(MavenPublication)
引用:
dependencies {implementation 'cn.rock.spi:serviceloader:0.0.1'
}
完整demo:
https://github.com/jdsjlzx/spi