运行 Java 工具,在处理资源时。

huangapple go评论127阅读模式
英文:

Run Java Tool while processing resources

问题

我有一个多项目构建,其中我有一个包含XML文件的文件夹,这些文件在生产中需要作为资源。

XML文件是不同构建工件的一部分,因此应该打包到它们自己的JAR文件中。

我的问题是我现在需要两个不同版本的这些XML文件:

  1. 原样的XML文件用于测试目的。
  2. 用于生产的精简版本(可以通过运行一个JavaExec任务来生成精简版本)。

我想通过为测试目的指定第二个配置来实现这一点,以便在xml-config.jar旁边生成一个xml-config-test.jar

所以,我基本上有两个问题:

  1. 如何运行JavaExec任务以处理XML文件,然后将它们放入默认的JAR文件中?
  2. 如何额外生成一个第二个测试JAR,而不处理XML文件?
英文:

I have a multi-project build, where I have a folder containing XML files that are needed as resources in production.
The XML files are part of different build artifacts and should therefore be bundled in their own JAR.

My problem now is that I need 2 different versions of these XML files:

  1. the XML files as they are for testing purposes.
  2. a stripped down version for production (The stripped down version can be produced by running a JavaExec task)

I want to realize this by specifying a second configuration for the testing purposes producing a xml-config-test.jar alongside the xml-config.jar.

So, I basically have 2 questions:

  1. How can I run the JavaExec task to process the XML files before putting them in the default jar?
  2. How can I additionally produce a second test jar without processing the XML files?

答案1

得分: 1

假设XML文件位于项目目录下的 config 文件夹中。然后,您可以创建类似以下示例的任务:

def testConfig = fileTree(dir: 'config', include: '**/*.xml')

task testConfigJar(type: Jar) {
    from testConfig
    archiveFileName = 'xml-config-test.jar'
}

task productionConfig(type: JavaExec) {
    def configFiles = testConfig
    inputs.files(configFiles)
    def destinationDir = temporaryDir
    outputs.dir(destinationDir)
    
    classpath = ...
    main = ...
    args = ...
}

task productionConfigJar(type: Jar) {
    from productionConfig
    archiveFileName = 'xml-config.jar'
}

使用这个框架,您只需配置 productionConfig 任务来处理由 configFiles 提供的文件,并将结果文件存储在由 destinationDir 指定的目录中。

然后,您可以调用 gradle testConfigJar 来创建包含测试配置文件的JAR文件,以及 gradle productionConfigJar 来创建包含生产配置文件的JAR文件。您甚至可以将这些任务绑定到现有任务或常见的生命周期任务,比如 assemblebuild(例如使用 dependsOn)。

英文:

Let's assume the XML files are in a directory config under the project directory. You may then create tasks similar to the ones in the following example:

def testConfig = fileTree(dir: 'config', include: '**/*.xml')

task testConfigJar(type: Jar) {
    from testConfig
    archiveFileName = 'xml-config-test.jar'
}

task productionConfig(type: JavaExec) {
    def configFiles = testConfig
    inputs.files(configFiles)
    def destinationDir = temporaryDir
    outputs.dir(destinationDir)
    
    classpath = ...
    main = ...
    args = ...
}

task productionConfigJar(type: Jar) {
    from productionConfig
    archiveFileName = 'xml-config.jar'
}

Using this skeleton you just need to configure the productionConfig task to process the files given by configFiles and to store the resulting files inside the directory given by destinationDir.

You may then call gradle testConfigJar to create the JAR file containing the configuration files for testing and gradle productionConfigJar to create the JAR file containing the configuration files for production. You may even bind those tasks to your existing tasks or common lifecycle tasks like assenble or build (e.g. using dependsOn).

huangapple
  • 本文由 发表于 2020年9月17日 20:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63938477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定