【问题标题】:Equivalent to product flavors in non-Android Gradle?相当于非 Android Gradle 中的产品风味?
【发布时间】:2016-04-08 21:10:36
【问题描述】:

我正在创建一个桌面 Java 应用程序,并希望创建两个版本:免费和付费。不同之处主要在于包含哪些资源文件(在将来,它可能还会涉及不同的代码,但不是现在)。

我读过Android build variants 允许通过“产品风味”实现此类事情。不过,这似乎是android 插件独有的功能,桌面上显然没有。

是否有不依赖于androidplugin 的与这些产品风格等效的产品?

如果有帮助,我的最终目标是找到一种方法,让我可以运行 Gradle build 任务并输出两个不同版本的应用程序,这是我对 Android 构建变体完成什么的理解。

【问题讨论】:

    标签: gradle android-gradle-plugin android-productflavors


    【解决方案1】:

    当然您可以使用sourceSets 并自定义Jar 任务来完成相同的效果。

    group 'com.jbirdvegas.example'
    version '1.0-SNAPSHOT'
    
    repositories {
        jcenter()
    }
    
    // adding the java plugin add the `jar` task to the build task graph
    apply plugin: 'java'
    
    compileJava {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    
    /**
     * SourceSets allows different builds to have their own
     * code.  If main and flavorOne contain com.foo.Bar then
     * the `flavorOne` jar will contain the file specified by
     * the `flavorOne` source set files.
     */
    sourceSets {
        main {
            // you could also do per sourceSet resources if needed
            java.srcDir 'src/main/java'
        }
        flavorOne {
            java.srcDir 'src/flavorOne/java'
        }
        flavorTwo {
            java.srcDir 'src/flavorTwo/java'
        }
    }
    
    /**
     * Creates the first flavor's jar when the Java plugin's `Jar`
     * task is called.  Since the `jar` task depends on `flavorOne` task
     * this task will run before the generic `jar` task which produces
     * the base .jar artifact.
     */
    task flavorOne(type: Jar) {
        from sourceSets.flavorOne.output
        classifier = 'flavorOneJar'
        manifest {
            attributes "Main-Class": "de.jeha.photo.mosaic.cli.Main"
        }
    }
    
    /**
     * Creates the second flavor's jar when the Java plugin's `Jar`
     * task is called.  `flavorTwo` can run before `flavorOne` because
     * both must just run before the `jar` task (base artifact)
     */
    task flavorTwo(type: Jar) {
        from sourceSets.flavorTwo.output
        classifier = 'flavorTwoJar'
        manifest {
            attributes "Main-Class": "de.jeha.photo.mosaic.cli.Main"
        }
    }
    
    // be sure to build all flavors whenever the `jar` task runs
    jar.dependsOn flavorOne, flavorTwo
    
    dependencies {
        // you can declare different dependencies per sourceSets
        flavorOneCompile 'com.google.code.gson:gson:2.5'
        flavorTwoCompile 'com.something:else:1.0'
        compile 'commons-io:commons-io:2.4'
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
    
    /**
     * if you want to have specific control over the main jar's
     * build then you could configure the main jar as needed.
     * This is the equivalent of the `flavorBlah` tasks except
     * this task was added for us by the `java` plugin.  Changes
     * here will only affect the main jar built from `sourceSet.main`
     *
     * Think of this as the default where other `productFlavors` are
     * completely divorced from this jar's configuration.
     */
    jar {
        manifest {
            attributes "Main-Class": "de.jeha.photo.mosaic.cli.Main"
        }
    }
    

    然后我们可以看到不同的构建。我们的口味和 main 源集 jar。

    $ ls -l build/libs/
    -rw-r--r--  1 jbirdvegas  63209268   1.3K Jan  6 08:58 my-sweet-jar-1.0-SNAPSHOT-flavorOneJar.jar
    -rw-r--r--  1 jbirdvegas  63209268   302B Jan  6 08:58 my-sweet-jar-1.0-SNAPSHOT-flavorTwoJar.jar
    -rw-r--r--  1 jbirdvegas  63209268    18K Jan  6 08:58 my-sweet-jar-1.0-SNAPSHOT.jar
    

    【讨论】:

    • 添加了更多的 cmets 并添加了一个 jar 闭包来展示如何将配置应用到主 jar。这是flavorBlah 任务的等效方法。我可以进一步解释任何有帮助的部分
    • 谢谢,非常感谢!我会稍等一下,看看是否会出现其他答案。
    • 谢谢。还有一个问题:如何选择某种风味并获得 IntelliJ IDEA 支持?我的意思是,我希望自动完成功能与我通过 flavorXCompile 包含的库一起使用。
    • @Miha_x64,你也许可以做类似configurations.compile.extendsFrom flavorX
    • @JBirdVegas 我在 org.gradle.api.internal.artifacts.configurations 类型的配置 ':compile' 上得到'找不到参数 [task':someFlavor'] 的方法 extendsFrom() .DefaultConfiguration。'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多