2013-06-24 45 views
2

我想生成一个不包含依赖关系的pom文件。我已经尝试使用下面的代码清除依赖项,但依赖项仍列在生成的pom中。Gradle - 生成没有依赖关系的pom

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

为什么我需要这样的:我需要一个POM文件将包含在我的罐子,当我把它上传到了“自家酿制的”服务,但该服务鼓起时,它读取“+”作为版本的依赖关系(我正在使用动态依赖)。

+0

您是否尝试过清除的配置呢? –

+0

@JoshGagnon我该怎么做? –

+0

我不知道它是否会起作用(因此仅仅是评论),但'configurations.compile.clear()'可能是值得一试的,尽管它听起来超级骇人。您可能会在这里阅读第65.2.4节:http://www.gradle.org/docs/current/userguide/publishing_maven.html并尝试。 –

回答

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) 
} 
7
uploadArchives { 
    repositories { 
     mavenDeployer { 
      // Do not write any dependencies into the POM 
      pom*.whenConfigured { pom -> pom.dependencies.clear() }     
     } 
    } 
} 
+0

这应该是新的正确答案。 –