maven project as dependency in gradle project

前端 未结 2 568
盖世英雄少女心
盖世英雄少女心 2020-12-30 23:05

I have a project which is using Gradle as build tool and a second subproject which is using Maven\'s POM. I don\'t have the freedom of changing build tool on the subproject.

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 23:57

    you can "fake" including a Maven project like this:

    dependencies {
        compile files("vendor/other-proj/target/classes") {
            builtBy "compileMavenProject"
        }
    }
    
    task compileMavenProject(type: Exec) {
        workingDir "vendor/other-proj/"
        commandLine "/usr/bin/mvn", "clean", "compile"
    }
    

    This way Gradle will execute a Maven build (compileMavenProject) before compiling. But be aware that it is not a Gradle "project" in the traditional sense and will not show up, e.g. if you run gradle dependencies. It is just a hack to include the compiled class files in your Gradle project.

    Edit: You can use a similar technique to also include the maven dependencies:

    dependencies {
        compile files("vendor/other-proj/target/classes") {
            builtBy "compileMavenProject"
        }
        compile files("vendor/other-proj/target/libs") {
            builtBy "downloadMavenDependencies"
        }
    }
    
    task compileMavenProject(type: Exec) {
        workingDir "vendor/other-proj/"
        commandLine "/usr/bin/mvn", "clean", "compile"
    }
    
    task downloadMavenDependencies(type: Exec) {
        workingDir "vendor/other-proj/"
        commandLine "/usr/bin/mvn", "dependency:copy-dependencies", "-DoutputDirectory=target/libs"
    }
    

提交回复
热议问题