我有uploadArhives
到Maven存储库.aar
发布。
但是我必须一直从控制台运行gradlew uploadArhives
,如何编写代码以使其在每个版本或发行版本中均被调用?
uploadArchives {
repositories {
mavenDeployer {
def credentials = [
userName: NEXUS_USERNAME,
password: NEXUS_PASSWORD
]
repository(url: MAVEN_REPO_URL, authentication: credentials)
pom.artifactId = 'aaa'
pom.version = version
pom.packaging = 'aar'
pom.groupId = 'bbb'
}
}
}
编辑:
我认为,我们可以定义函数:
def uploadToMaven = {
uploadArchives
}
但是如何在每次构建时执行它?
最佳答案
我有一个包含许多模块和一个主要应用程序的复杂项目。
我在其中两个模块上添加了“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”,我认为这不是一个好主意。
您要问的是非常具有挑战性的。。。我试了一个小时:)