这是我第一次尝试使用Gradle's C plugin构建的C程序:

apply plugin: 'c'

model {
    components {
        derpus(NativeExecutableSpec) {
            sources {
                c(CSourceSet) {
                    source {
                        srcDir "src/derpus/c"
                        include "**/*.c"
                    }
                    exportedHeaders {
                        srcDir "src/derpus/headers"
                    }
                }
            }
        }
    }
}

这将生成一个名为derpus.exe的可执行文件。我希望,如果可能的话,将这些可执行文件(derpus-1.0.0.exederpus-1.0.1.exe等)设置为版本。当我像这样将derpus闭包更改为derpus-1.0.0时:



并运行gradle clean build我得到:
D:\workspace\derp\20150505\derpus>gradlew clean build

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\derpus\build.gradle' line: 6

* What went wrong:
Could not compile build file 'D:\derpus\build.gradle'.
> startup failed:
    build file 'D:\derpus\build.gradle': 6: unexpected tok
    en: 0 @ line 6, column 20.
                    derpus-1.0.0(NativeExecutableSpec) {
                               ^

    1 error

有人知道对这些可执行文件进行版本控制的方法吗?

更新资料

现在这是真的很奇怪!根据Amnon的建议,我添加了一个gradle.properties文件,该文件定义了version=1.0.0。然后,我将model闭包修改为:
model {
    components {
        derpus(NativeExecutableSpec) {
            sources {
                c(CSourceSet) {
                    source {
                        srcDir "src/derpus/c"
                        include "**/*.c"
                    }
                    exportedHeaders {
                        srcDir "src/derpus/headers"
                    }
                }
            }

            baseName = "derpus-${version}"
        }
    }
}

这将产生一个名为derpus-1的可执行文件(什么?!?!!)!

因此,我再次修改了model:
version = "3.4"

model {
    components {
        derpus(NativeExecutableSpec) {
            sources {
                c(CSourceSet) {
                    source {
                        srcDir "src/derpus/c"
                        include "**/*.c"
                    }
                    exportedHeaders {
                        srcDir "src/derpus/headers"
                    }
                }
            }

            baseName = "derpus-${version}"
        }
    }
}

如您所见,这应该覆盖gradle.properties中设置的版本,但是在运行gradle clean build之后,它将生成derpus-3!

因此,我再次修改了model:
model {
    components {
        derpus(NativeExecutableSpec) {
            sources {
                c(CSourceSet) {
                    source {
                        srcDir "src/derpus/c"
                        include "**/*.c"
                    }
                    exportedHeaders {
                        srcDir "src/derpus/headers"
                    }
                }
            }

            baseName = "derpus-3.4.5"
        }
    }
}

这产生derpus-3.4 !!! 这是怎么回事?! C插件中是否存在不符合完整version变量的错误?

最佳答案

在上面的示例中,derpus-1.0.0的问题在于,连字符是减号,这在组件规格名称中是意外的,因此是失败的。您可以通过用反向逗号将derpus-1.0.0换行来克服此问题。但是,更好的方法是将版本应用于组件规范的baseName属性,即在derpus组件定义下添加以下行:

baseName = "derpus-1.0.0"

要么
baseName = "derpus-$version"

在第二种情况下,版本属性 $version 来自project对象。

更新

在下面的每个smeeb注释中,可以应用的另一种解决方法是直接重命名目标二进制文件:
afterEvaluate {
    RenameNativeBinaries()
}

def RenameNativeBinaries() {
    binaries.all { b ->
        if (b instanceof SharedLibraryBinarySpec) {
            b.sharedLibraryFile = ReconstructFileName(b.sharedLibraryFile)
        } else if (b instanceof StaticLibraryBinarySpec) {
            b.staticLibraryFile = ReconstructFileName(b.staticLibraryFile)
        }
    }
}

def ReconstructFileName(File originalFile) {
    def originalFileName = originalFile.absolutePath
    def filePath = FilenameUtils.getFullPath(originalFileName)
    def baseName = FilenameUtils.getBaseName(originalFileName)
    def extension = FilenameUtils.getExtension(originalFileName)

    def newName = "$baseName-$version.$extension"
    def newFile = new File(filePath, newName)
    newFile
}
FilenameUtils来自commons-io:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'commons-io', name: 'commons-io', version: '2.4'
    }
}

09-30 23:15