【问题标题】:How do I wrap maven, or any other build system with gradle so that the dependencies work如何使用 gradle 包装 maven 或任何其他构建系统,以便依赖项工作
【发布时间】:2015-12-24 11:24:10
【问题描述】:

我需要用 gradle 包装一个 maven 构建。 坏主意,对,明白了。但是,我已经有大量使用 gradle 的代码库,还有一些仍在使用 maven。 我有一套 gradle 工具来完成我所有的分支、部署和很多很酷的事情。我也想在我的 Maven 项目中利用它。

我的顶级任务是“deb”,因为我正在构建 debian 软件包。我的样板基础设施任务依赖于“deb”,因此无论每个项目如何实现它,它都可以工作,无论底层构建基础设施、maven、make、ant 等等。每个项目只需要定义一个“deb”任务并使其依赖于构建输出 debian 包所需的任何内容。这样,基础设施 gradle 脚本就不会关心具体的项目实施。

我的主要问题是,如何在 gradle 中生成依赖项,这样我就不会进行不必要的构建。

【问题讨论】:

    标签: maven gradle build


    【解决方案1】:

    这是我最终完成的 gradle 任务。

    它的要点是我需要想出一些规则来计算项目的构建系统生成的输入和输出文件。在这个示例中,它很简单,因为 java 依赖于 *.java、*.xml 和 **/pom.xml,仅此而已。 我的输出都是 debian 包,所以只需要一个 *.deb 文件规范。 setNewVersion 任务允许我将基本版本字符串保存在 gradle.properties 中。我使用 gmaven 插件将其读入 maven newVersion 字符串。 (见Maven2 property that indicates the parent directory

    def env = System.getenv()    // get env to pass in to child build
    def sourceBaseDir = "source" // Source dir for the wrapped project
    
    // Set the maven newVersion using gradle.properties.
    task setNewVersion(type:Exec) {
        group = "build"
        description = "(nim) Copy newVersion string into maven variable"
        commandLine "bash", "-c", "sed -e \"s/version/newVersion/\" gradle.properties > source/build.properties"
    }
    
    // Build the maven targets
    task mvnBuild(type:Exec, dependsOn: setNewVersion) {
        group = "build"
        description = "(nim) Build store with maven - TODO: uses -DskipTests because mvn build from gradle fails with junit running."
        environment = System.getenv()
        workingDir = sourceBaseDir
        inputs.files fileTree(sourceBaseDir) {
             include '**/src/**/*.java', '**/src/**/*.xml', '**/pom.xml'
        }
        outputs.files fileTree(sourceBaseDir) {
            include '**/target/*.deb'
        }
        println "inputs=${inputs.getFiles() .getFiles()}"
        println "outputs=${outputs.getFiles().getFiles()}"
        commandLine "mvn", "-DskipTests", "package"
    }
    
    // Maven clean target
    task mvnClean(type:Exec) {
        group = "build"
        description = "(nim) Clean store with maven, depends on local and remote repe cleanup as well"
        workingDir = sourceBaseDir
        commandLine "mvn", "clean"
    }
    
    task deb(dependsOn: "mvnBuild")
    

    【讨论】:

    • 存在 -DskipTests 是因为对于这个特定的项目,它们在 gradle 下运行时会失败。这是一个我仍然需要解决的问题,但并不妨碍它按原样使用。
    猜你喜欢
    • 2013-10-04
    • 2014-09-07
    • 2018-01-23
    • 2015-08-28
    • 1970-01-01
    • 2018-01-02
    • 2015-11-14
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多