相当于非Android摇篮产品的口味? [英] Equivalent to product flavors in non-Android Gradle?

查看:244
本文介绍了相当于非Android摇篮产品的口味?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个桌面的Java应用程序,并希望创建两个版本:免费和付费。所不同的主要是其中的资源文件包含(在未来某一日期,它可能涉及不同的code为好,但不是现在)。

I'm creating a desktop Java app and would like to create two versions: free and paid. The difference is mainly in which resource files are included (at a future date, it may involve different code as well, but not right now).

我读过有关的Andr​​oid建立变种允许只是这样的事情,通过产品的口味。然而,这似乎是唯一的机器人插件,这显然不是用在桌面上的功能。

I've read about Android build variants allowing for just this sort of thing through "product flavors". However, this seems to be a feature unique to the android plugin, which is obviously not available on the desktop.

是否有一个相当于不依赖于 Android的这些产品的口味插件?

Is there an equivalent to these product flavors that does not depend on the androidplugin?

如果有帮助,我的最终目标是发现我可以运行摇篮的方式建立任务和输出两个不同版本的应用程序,这是我的理解什么Android编译变种完成。

If it helps, my end goal is to find a way that I can run the Gradle build task and output two different versions of the app, which is my understanding of what the Android build variants accomplishes.

推荐答案

当然你可以使用 sourceSets 和定制任务来完成相同的效果。

Sure you can use sourceSets and customize the Jar tasks to accomplish the same effect.

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"
    }
}

然后,我们可以看到不同的基础之上。我们的口味和源集罐子。

$ 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

这篇关于相当于非Android摇篮产品的口味?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆