gpt4 book ai didi

groovy - Gradle WAR 插件 : how to find destination for files to copy

转载 作者:行者123 更新时间:2023-12-03 03:24:09 24 4
gpt4 key购买 nike

我一直在努力让 Gradle 根据 this blog post 生成一个 IntelliJ WAR 工件定义作为“想法”任务的一部分。 .尽管我的代码很脏,但到目前为止,这已经很成功了,所以我正在尝试清理它。
就我而言, war 任务应该包含在 IntelliJ 中创建必要标签所需的所有信息。例如,给定一个 Gradle Project 对象,比如 gradleProject,我可以执行以下操作:

def artifactManager = project.component.find { it.@name == 'ArtifactManager' } as Node
if (artifactManager) {
Node artifact = artifactManager.artifact.find { it.@type == 'exploded-war' }
if (artifact) {
artifactManager.remove(artifact)
}
} else {
artifactManager = project.appendNode('component', [name: 'ArtifactManager']);
}
def builder = new NodeBuilder();
def artifact = builder.artifact(type: 'exploded-war', name: "${gradleProject.name} exploded war") {
'output-path'("${gradleProject.buildDir}/${gradleProject.name}-exploded.war")
root(id: 'root') {
element(id: 'directory', name: 'WEB-INF') {
// copy web.xml
element(id: 'file-copy', path: gradleProject.war.webXml)
Set<String> excludeJars = [] as Set
element(id: 'directory', name: 'classes') {
// copy classes
element(id: 'module-output', name: gradleProject.name)
// copy depending projects module output (classes)
gradleProject.configurations.runtime.allDependencies.each {
if (it.hasProperty('dependencyProject')) { // TODO: make it functional?!?
if (it.dependencyProject.hasProperty('jar'))
excludeJars.add(it.dependencyProject.jar.archiveName)
element(id: 'module-output', name: it.dependencyProject.name)
}
}
}
// copy dependency jars excluding possible existing module jars (not sure it's necessary)
element(id: 'directory', name: 'lib') {
// TODO: this should handle ALL kinds of dependencies
gradleProject.configurations.runtime.each {
if (!excludeJars.contains(it.name))
element(id: 'file-copy', path: it)
}
}
}
}
}
artifactManager.append artifact

修改或创建我的 IntelliJ .ipr 文件。
现在我在 Gradle 脚本中向我的 WAR 工件中添加了一些文件,如下所示:
war{
from project(':rest').file('resource/some.properties')
from project(':some_project').fileTree('some_folder')
metaInf {
from parent.file('build/config/website/' + environment + '/context.xml')
}
}

我想添加一些 Groovy 代码来获取这些文件并创建正确的 XML 标记,让 IntelliJ 将它们复制到正确的位置。
最简单的方法是什么?我尝试玩以下游戏:
gradleProject.war.source.each { // ... create tags etc.  }

但是源文件列表包括所有文件,无论它们的目标是什么(所有库、web.xml、类等)。

我怎样才能实现我想要做的事情?

最佳答案

我终于在 Gradle 文档的帮助下和一些反复试验弄明白了。我使用以下代码得到了想要的结果:

def artifactManager = project.component.find { it.@name == 'ArtifactManager' } as Node
if (artifactManager) {
Node artifact = artifactManager.artifact.find { it.@type == 'exploded-war' }
if (artifact) {
artifactManager.remove(artifact)
}
} else {
artifactManager = project.appendNode('component', [name: 'ArtifactManager']);
}
def builder = new NodeBuilder();
def artifact = builder.artifact(type: 'exploded-war', name: "${gradleProject.name} exploded war") {
'output-path'("${gradleProject.buildDir}/${gradleProject.name}-exploded.war")
root(id: 'root') {
println "Adding files to root"
gradleProject.war.rootSpec.children.each {
if (it.destPath.pathString.equals("")) {
add(builder, it, "")
}
}
println "Adding files to META-INF"
element(id: 'directory', name: 'META-INF') {
gradleProject.war.rootSpec.children.each {
if (it.destPath.pathString.equals("META-INF")) {
add(builder, it, "META-INF")
}
}
}
println "Adding files to WEB-INF"
element(id: 'directory', name: 'WEB-INF') {
element(id: 'file-copy', path: gradleProject.war.webXml)
gradleProject.war.rootSpec.children.each {
if (it.destPath.pathString.equals("WEB-INF")) {
add(builder, it, "WEB-INF")
}
}
Set<String> excludeJars = [] as Set
element(id: 'directory', name: 'classes') {
element(id: 'module-output', name: gradleProject.name)
gradleProject.configurations.runtime.allDependencies.each {
if (it.hasProperty('dependencyProject')) {
if (it.dependencyProject.hasProperty('jar'))
excludeJars.add(it.dependencyProject.jar.archiveName)
element(id: 'module-output', name: it.dependencyProject.name)
}
}
}
element(id: 'directory', name: 'lib') {
gradleProject.configurations.runtime.each {
if (!excludeJars.contains(it.name))
element(id: 'file-copy', path: it)
}
}
}
}
}
artifactManager.append artifact

add函数定义为:
def add(NodeBuilder builder, Object s, String dest, String prefix = "") {
println(prefix + "current destination: [" + dest + "]")
if (s instanceof File) {
println(prefix + "-- file " + s.toString())
builder.element(id: 'file-copy', path: s)
} else if (s instanceof FileTree) {
println(prefix + "-- folder " + s.dir.toString())
builder.element(id: 'dir-copy', path: s.dir)
} else if (s instanceof CopySpecInternal) {
print(prefix + "-- copy spec " + s.toString() + " with destination path: [" + s.destPath.pathString + "]; ")
if (dest.equals(s.destPath.pathString)) {
println("destination matches current folder")
s.sourcePaths.each { path ->
add(builder, path, dest, prefix + " ")
}
s.children.each { child -> add(builder, child, dest, prefix + " ") }
} else {
println("")
if (s.destPath.segments != null && s.destPath.segments.length > 0 && s.destPath.segments[0].equals(dest)) {
String relDest = new RelativePath(false, Arrays.copyOfRange(s.destPath.segments, 1, s.destPath.segments.length)).pathString
builder.element(id: 'directory', name: relDest) {
s.sourcePaths.each { path ->
add(builder, path, relDest, prefix + " ")
}
s.children.each { child -> add(builder, child, relDest, prefix + " ") }
}
} else {
println(prefix + "!! destination path is not relative to [" + dest + "]: [" + s.destPath.pathString + "]")
}
}
} else println(prefix + "?? unknown " + s.toString())
}

我基本上对 war 进行了逆向工程任务属性并挖掘到要复制的文件树中。据我所知,这个任务并没有公开一个接口(interface)来做这种事情,所以我不得不求助于一个“hacky”的解决方案,这不是我最喜欢的。如果有人可以提供一些更简洁的代码,我会很高兴。

关于groovy - Gradle WAR 插件 : how to find destination for files to copy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com