gradle how to add only javascript files to a directory in the war file

好久不见. 提交于 2019-12-25 16:49:31

问题


I am trying to create a web application that uses angular2. The part of the build.gradle responsible for compiling the typescript and adding it to the war is as follows:

public class TsCompileTask extends DefaultTask {
    File projectDir;

    @OutputDirectory
    File outputDir;

    @TaskAction
    void compile() {
        println "compiling TypeScript files..."
        project.exec {
            executable = "tsc"

            args "-p"
            args projectDir
            args "--outDir"
            args outputDir.toString()
        }
    }
}

public class npmInstallTask extends DefaultTask {
    File projectDir

    @TaskAction
    void compile(){
        println "installing npm packages"
        project.exec {
            workingDir = projectDir.path
            executable = "npm"
            args "install"
        }
    }
}

task tsCompile(type:TsCompileTask) {
    projectDir = file('./src/main/typescript')
    outputDir = file("$buildDir/ts")
}

war{
    into("js"){
        from tsCompile.outputs
    }
}

This adds the output of the typescript compiler to the js directory in the war archive, but it does not add the dependencies, which are in the directory node_modules.

I tried to use the following gradle snippet to add only the JavaScript files from the typescript directoryinto thw war:

war{
    into("js"){
        from fileTree(dir: './src/main/typescrypt/').matching{
            include('**/*.js)
        }.files
    }
}

However, this includes all files under the directory.

I have tried several variations of the above (e.g. having the into be in a closure for from instead of the other way around, using fileTree('./src/main/typescript').include('**/*.js')). How can I add only the files ending in .js to the js directory in the war file?


回答1:


the from method takes strings, not file objects.

war{
    into("js"){
        from fileTree(dir: './src/main/typescrypt/').matching{
            include('**/*.js)
        }.files.stream().map{
        File->file.toString()
    }.collect(Collectors.toSet())
}


来源:https://stackoverflow.com/questions/42745018/gradle-how-to-add-only-javascript-files-to-a-directory-in-the-war-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!