2

我想生成一个不包含依赖项的 pom 文件。我尝试使用下面的代码清除依赖项,但是依赖项仍列在生成的 pom.xml 中。

install.doFirst {
    repositories.mavenInstaller {
        pom.dependencies.clear()
    }
}

为什么我需要这个:当我将它上传到“自制”服务时,我需要一个 pom 文件包含在我的 jar 中,但是当它读取“+”作为依赖项的版本时,该服务会爆炸(我正在使用动态依赖项)。

4

2 回答 2

7
uploadArchives {
    repositories {
        mavenDeployer {
            // Do not write any dependencies into the POM
            pom*.whenConfigured { pom -> pom.dependencies.clear() }                 
        }
    }
}  
于 2015-03-27T05:58:59.123 回答
0

我最终使用了以下调用的解决方案gradle cleanPom。基本上我使用 XML 解析器来编辑生成的 POM。

apply plugin: 'maven'

pomFileLocation="build/poms/noDeps.pom"

/**
 * Creates pom with dependencies
 */
task writeNewPom << {
    pom {}.writeTo(pomFileLocation)
}

/**
 * Reads and Overwrites POM file removing dependencies
 */
task cleanPom(dependsOn: writeNewPom) << {
    def xml = new XmlParser().parse(pomFileLocation)
    def nodes = []
    xml.dependencies[0].each {
        nodes.add(it)
    }
    def parrent = nodes.first().parent()
    nodes.each {
        parrent.remove(it)
    }
    new XmlNodePrinter(new PrintWriter(new FileWriter(pomFileLocation))).print(xml)
}
于 2013-06-26T16:25:40.863 回答