maven插件与maven-publish插件的区别
maven插件适用于gradle1.0-6.2版本,6.2版本后该插件就被废弃了,推荐使用maven-publish插件。
maven-publis插件是在gradle 1.3 版本后开始支持的,使配置更加简洁。
maven-publis插件的使用
砍柴不误磨刀工,我们先来看一下官方文档。
完整示例
下面配置展示如何签署和发布 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' //若是war包,就写components.web,若是jar包,就写components.java from components.java 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 = '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/repo def 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:// 并将其用作发布目标。 |
from components.java
若是war包,就写components.web,若是jar包,就写components.java
如果发布的产物没有jar包就是缺少这个配置
当然有些配置你不需要,可以删除,比如删除下面配置:
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 { publications { mavenJava(MavenPublication) { artifactId = 'serviceloader' //若是war包,就写components.web,若是jar包,就写components.java from components.java versionMapping { 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,参考下面方法。
解决方案(Mac)
点击Android Studio–>Preference–>选择Experimental(实验性选项)–>在Gradle一栏中取消勾选"Do not build Gradle task list during Gradle sync"
或者勾选下面的选项
点击File–>Sync Project with Gradle Files即可
第二种配置
如果你比较喜欢恋旧,下面的配置也是可以的。
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
GitHub - jdsjlzx/NewSpi: Android中SPI思想应用与改进
其他版本一:
apply plugin: 'com.android.library' apply plugin: 'maven-publish' apply plugin: 'digital.wup.android-maven-publish' android { compileSdkVersion "33" as int defaultConfig { ...... testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { } def VERSION = "0.9.3.3" def GROUP_ID = "com.baidu.sdk" def ARTIFACT_ID = "ARTIFACT_ID" def MAVEN_USERNAME = "username" def MAVEN_PASSWORD = "123456" def MAVEN_URL = "http://www.baidu.com" afterEvaluate { publishing { // 配置maven-publishing插件的输出物 publications { maven(MavenPublication) { afterEvaluate { artifact(tasks.getByName("bundleReleaseAar"))} groupId = GROUP_ID artifactId = ARTIFACT_ID version = VERSION } } repositories { maven { credentials { username MAVEN_USERNAME password MAVEN_PASSWORD } url = MAVEN_URL } } } }
其他版本二:
apply plugin: 'maven-publish' ext { GROUP_ID = "com.baidu.group" ARTIFACT_ID = "im-sdk" VERSION_NAME = "0.0.1" } //声明变量记录上传Maven库地址 def repositoryUrl //判断发到正式库还是snapshot库 if (isReleaseBuild()) { //上传Release私有仓库 repositoryUrl = "https://baidu.com/" } else { println 'SNAPSHOT Versions' //上传snapshot私有仓库 repositoryUrl = "http://baidu.com/debug/" } //从项目gradle.properties中读取Nexus服务器登录用户名 def getRepositoryUserName() { // return hasProperty('USERNAME') ? USERNAME : "" return "autoai-AVS" } //读取Nexus服务器登录密码 def getRepositoryPassword() { // return hasProperty('PASSWORD') ? PASSWORD : "" return "@pIbl9kE" } def isReleaseBuild() { return !VERSION_NAME.contains("SNAPSHOT") } afterEvaluate { task generateSourcesJar(type: Jar) { from android.sourceSets.main.java.srcDirs classifier 'sources' } publishing { publications { release(MavenPublication) { if (project.getPlugins().hasPlugin('com.android.application') || project.getPlugins().hasPlugin('com.android.library')) from(components.release) else from(components.java) pom { groupId = GROUP_ID version = VERSION_NAME artifactId = ARTIFACT_ID } artifact generateSourcesJar } } repositories { maven { credentials { username getRepositoryUserName() password getRepositoryPassword() } url repositoryUrl } } } }