我有一个包含许多模块和一个主要应用程序的复杂项目。
我在其中两个模块上添加了“uploadArchives”(因为是 android 库)。通过这种方式,我可以在 Maven 上发布我的库,只需从我的主应用程序运行任务 uploadArchives,或者使用 gradle 并调用此任务“uploadArchives”。
您可以在您的 build.gradle(您要发布的库的)“build.finalizedBy(uploadArchives)”中使用它。
例如:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 17
targetSdkVersion 23
versionCode 2
versionName "2.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
abortOnError false
}
}
build.finalizedBy(uploadArchives)
task wrapper(type: Wrapper) {
gradleVersion = "2.8"
}
dependencies {
compile project(':spfshared')
compile 'com.google.code.gson:gson:2.4'
}
//task for Sonatype Nexus OSS
uploadArchives {
repositories {
mavenDeployer {
repository(
url: "${nexusUrl}/content/repositories/releases") {
authentication(userName: nexusUsername, password: nexusPassword)
}
snapshotRepository(
url: "${nexusUrl}/content/repositories/snapshots") {
authentication(userName: nexusUsername, password: nexusPassword)
}
pom.version = "2.0.0.1"
pom.artifactId = "spflib"
pom.groupId = "it.polimi.spf"
}
}
}
每次构建后,uploadArchives 都会启动。
我尝试了这个解决方案,它确实有效。
我还尝试了一些使用“build.dependsOn myTaskName”的解决方案,但没有成功。如果你愿意,你可以尝试,但在我的 AndroidStudio 上,它的第一个解决方案是有效的。
PS:我使用命令“gradlew -q build”测试了我的解决方案,并且还专门从 Android Studio 的主模块运行了任务“build”(它是我的主应用程序)。
如果您想在每次发布时调用“uploadArchives”,只需将“build”替换为发布任务即可。
更新:
我也尝试了这些代码行:
defaultTasks 'uploadArchives'
clean.finalizedBy(uploadArchives)
assembleDebug.finalizedBy(uploadArchives)
assembleRelease.finalizedBy(uploadArchives)
但有时他们多次调用“uploadArchives”,我认为这不是一个好主意。
你的问题很有挑战性……我试了整整一个小时:)