本文介绍了如何在Gradle中应用补丁文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gradle构建脚本,它成功构建了我的项目并编译了我需要的所有构件。

然而,在一些情况下,我想给其他开发人员可以选择修补某些文件。例如,在其中一个存档中,有一个xml文件,其中包含有关数据库挂钩的信息 - 某些开发人员使用其他版本(甚至是引擎),并且需要在使用构建输出之前更改这些文件。



不是让他们更改由版本控制的文件,他们可能会犯错误,我想让他们选择一个本地的,单独的补丁文件,构建脚本适用。

在一个旧的ant脚本中,我们做了这样的事情

 < target name =appcontext-patchif =applicationContext.patch.file> 
< / target>

但我无法弄清楚如何在Gradle中做同样的事情。有没有更好的(比较习惯的)做法比直接将它转换成 ant.patch



某些上下文

这就是文件在档案中的结尾:

<$ ('META-INF'){$'b $'from'deployment',{
include'applicationContext.xml'
rename {fn - > jboss-spring.xml}
}
}

('META-INF'){$ b $'b'from'deployment',{
include'applicationContext.xml'
rename {fn - > jboss-spring.xml}
patch'local / applicationContext.xml.patch'
}
}

,并且在将文件放入存档之前应用修补程序文件。我不介意编写一些代码来实现此目的,但我对Gradle很陌生,而且我有不知道从哪里开始。

解决方案

您应该可以直接将您的ant调用转换为gradle。 b
$ b

关于如何做到这一点的gradle文档一般来说,属性变成了命名参数,而子标签变成了关闭,文档中有一些很好的例子。



我的第一个猜测应该是这样的:

  apply plugin:'java'

assemble.doFi rst {
ant.patch(patchfile:applicationContext.patch.file,
originalFile:$ {dist.dir} /applicationContext.xml)
}

这是未经测试的,所以我很确定它会让您开始正确的道路。目的是在java插件组装你的档案之前,你希望gradle调用闭包。在这种情况下,闭包会执行一个ant操作来修补你的xml。



或者你可以使用上面的任务来执行复制和标记。

 任务myCopyTask(类型:复制){
...
}<< {
ant.patch(patchfile:applicationContext.patch.file,
originalFile:$ {dist.dir} /applicationContext.xml)
}

在这种情况下,您正在自己编写任务,左移运算符(< <)相当于 .doLast ,但是非常酷。我不确定你喜欢哪种方法,但是如果你已经有了一个复制任务来首先获取文件,我认为doLast会尽可能使相关代码块尽可能接近。


I have a Gradle build script that successfully builds my project and compiles all the artifacts I need.

However, in a couple of cases I'd like to give other developers the option to patch some of the files. For example, in one of the archives there's an xml file with information about database hooks - some of the devs use other versions (or even engines) and need to change these before they can use the build output.

Instead of having them make changes to a version-controlled file, which they might commit by mistake, I'd like to give them the option to have a local, individual patch file which the build script applies.

In an old ant script, we did something like this

<target name="appcontext-patch" if="applicationContext.patch.file">
    <patch patchfile="${applicationContext.patch.file}" originalfile="${dist.dir}/applicationContext.xml"/>
</target>

but I can't figure out how to do the equivalent in Gradle. Is there a better (i.e. more idiomatic) way of doing this than trying to directly convert this into a call to ant.patch?

Some context
This is how the file ends up in the archive in the first place:

into('META-INF') {
    from 'deployment', {
        include 'applicationContext.xml'
        rename { fn ->  "jboss-spring.xml" }
    }
}

It would be fantabulous if I could just do something like

into('META-INF') {
    from 'deployment', {
        include 'applicationContext.xml'
        rename { fn -> "jboss-spring.xml' }
        patch 'local/applicationContext.xml.patch'
    }
}

and have the patch file applied before the file is put in the archive. I don't mind writing some code to make this possible, but I'm quite new to Gradle and I have no idea where to begin.

解决方案

You should be able to translate your ant call into gradle pretty directly.

The gradle doc on how to do this generically. Basically attributes become named arguments and child tags become closures. The documentation has a bunch of good examples.

Once you have your translated ant task you can put in in a doFirst or doLast block on an appropriate task.

My first guess would be something like this:

apply plugin: 'java'

assemble.doFirst {
    ant.patch(patchfile: applicationContext.patch.file,
              originalFile: "${dist.dir}/applicationContext.xml")
}

That's untested, so but I'm pretty sure it will get you started on the right path. The intent is that just before the java plugin assembles your archive you want gradle to call a closure. In this case the closure will perform an ant action that patches your xml.

Alternately you could use the task you have above that performs a copy and tag onto that.

task myCopyTask(type: Copy) {
    ...
} << {
    ant.patch(patchfile: applicationContext.patch.file,
              originalFile: "${dist.dir}/applicationContext.xml")
}

In this case you are writing the task yourself and the left-shift operator (<<) is equivalent to .doLast but a whole lot cooler. I'm not sure which method you prefer, but if you already have a copy task that gets the file there in the first place, I think doLast keeps the relevant code blocks as close to each other as possible.

这篇关于如何在Gradle中应用补丁文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 10:25