这是我第一次尝试使用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.exe
,derpus-1.0.1.exe
等)。当我将derpus
关闭更改为derpus-1.0.0
时,如此:
derpus-1.0.0(NativeExecutableSpec){
运行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
的{{1}}文件。然后我将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"
}
}
}
!!! 这里发生了什么?!? C插件中是否有一个不符合完整derpus-3.4
变量的错误?
答案 0 :(得分:1)
在上面的示例中,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'
}
}