android - 如何上传多个android文件(每种口味一个)

标签 android maven gradle android-gradle-plugin

我正在尝试使用 maven gradle 插件为每个项目上传多个 Artifact :

http://www.gradle.org/docs/current/userguide/maven_plugin.html

53.6.4.1. Multiple artifacts per project

Maven can only deal with one artifact per project. This is reflected in the structure of the Maven POM. We think there are many situations where it makes sense to have more than one artifact per project. In such a case you need to generate multiple POMs. In such a case you have to explicitly declare each artifact you want to publish to a Maven repository. The MavenDeployer and the MavenInstaller both provide an API for this:

Example 53.9. Generation of multiple poms

build.gradle
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
            addFilter('api') {artifact, file ->
                artifact.name == 'api'
            }
            addFilter('service') {artifact, file ->
                artifact.name == 'service'
            }
            pom('api').version = 'mySpecialMavenVersion'
        }
    }
}
You need to declare a filter for each artifact you want to publish. This filter defines a boolean expression for which Gradle artifact it accepts. Each filter has a POM associated with it which you can configure. To learn more about this have a look at PomFilterContainer and its associated classes.

我真正想做的是为我的构建的不同风格上传不同的文件。

这是我的项目 build.gradle 文件:

apply plugin: 'com.android.library'
apply plugin: 'maven'

group 'com.test'
version '0.0.1'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        abortOnError false
    }

    productFlavors {
        flavor1 { }
        flavor2 { }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://${System.properties['user.home']}/.m2/repository")

            addFilter('debugFlavor1') { artifact, file ->
                artifact.name.contains("debugFlavor1")
            }
            addFilter('releaseFlavor1') { artifact, file ->
                artifact.name.contains("releaseFlavor1")
            }
            addFilter('debugFlavor2') { artifact, file ->
                artifact.name.contains("debugFlavor2")
            }
            addFilter('releaseFlavor2') { artifact, file ->
                artifact.name.contains("releaseFlavor2")
            }

            pom('debug').artifactId = ${artifactId} + "-" ${flavor} + "-debug"
            pom('release').artifactId = ${artifactId} + "-" ${flavor}
        }
    }
}


dependencies {
    compile 'com.android.support:support-v4:21.0.2'
    compile 'org.apache.commons:commons-lang3:3.3.2'
}

这样的事情可能吗?

如何在每次发布中获取 artifactId 和 flavor?我使用 ${artifactId} 和 ${flavor} 作为占位符,但我知道这是不对的。

编辑:

好吧,我想出了如何为每种口味获得不同的神器。其中一个关键是设置:

publishNonDefault true

但是,当我这样做时,我并没有为每个 Artifact 获得一个 pom 文件。我有需要引入的传递依赖项,我认为如果没有 pom,它们将不会被引入。

这是我当前的 gradle 脚本,如何为每个脚本生成一个 pom。

apply plugin: 'com.android.library'
apply plugin: 'maven'

group 'com.test'
version '0.0.1'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"
    publishNonDefault true

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        abortOnError false
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
    }

    productFlavors {
        flavor1 {}
        flavor2 {}
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://${System.properties['user.home']}/.m2/repository")

            addFilter('flavor1Release') { artifact, file ->
                artifact.attributes.classifier.equals("flavor1Release")
            }
            addFilter('flavor2Release') { artifact, file ->
                artifact.attributes.classifier.equals("flavor2Release")
            }

            pom('flavor1Release').artifactId = project.archivesBaseName + "-flavor1"
            pom('flavor2Release').artifactId = project.archivesBaseName + "-flavor2";
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:21.0.2'
    compile 'org.apache.commons:commons-lang3:3.3.2'
}

最佳答案

//我尝试了以下方式,并且能够上传多个 jar 文件。 //希望这对您有所帮助....

import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact

// jarfilename1 and jarfilename2 are the two files that gets upload
// make sure the artifactID and the file name are the same


apply plugin: 'java'
apply plugin: 'maven'


artifacts{
    archives new DefaultPublishArtifact("jarfileName1", "jar", "jar", null, new Date(), new File("build/", "jarfilename1.jar"))
    archives new DefaultPublishArtifact("jarfileName2", "jar", "jar", null, new Date(), new File("build/", "jarfilename2.jar"))
}

uploadArchives {
    repositories {
    mavenDeployer {
        repository(url: "$archivaUrl") {
        authentication(userName: "$userName", password: "$passWord")
        }

        // addfilter for multiple pom upload 
        addFilter("jarfileName1") {artifact, file ->
                      artifact.name == "jarfileName1"
                     }
        addFilter("jarfileName2") {artifact, file ->
                       artifact.name == "jarfileName2"
                      }

        //Creating pom files and adding the Entity
        pom("jarfileName1").version ='1.1.1'
        pom("jarfileName1").groupId ='what.so.ever'
        pom("jarfileName1").artifactId ='jarfilename1'

        pom("jarfileName2").version ='1.1.1'
        pom("jarfileName2").groupId ='what.so.ever'
        pom("jarfileName2").artifactId ='jarfilename2'
    }
    }
}

关于android - 如何上传多个android文件(每种口味一个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160087/

相关文章:

gradle - 没有com.android.tools.build:aapt2:3.6.1-6040484的缓存版本可用于离线模式

java - Firebase 中的 getValue() 方法是什么?

android - 从 onCreateWindow WebView 处理 URL

maven - 如何为 Maven 插件指定默认目标?

java - 如何在 Windows 上使用命令行执行具有外部依赖项的 jar 文件?

gradle - 使用install4j gradle任务时出错(无法转换Install4jExtension_Decorated)

Gradle 配置/依赖语法

android - 菜单项图标在 API 23 中重叠

android - Firebase 大查询 - 如何从自定义事件表中检索数据

java - 在构建 WAR 之前在 Maven 中重命名生成的文件