我添加了this artifact这对我的gradle项目依赖项是一场 war 。
我需要扩展一些类,并从中使用经过修改的servlet上下文。
我原本希望 war 能够像现在一样导入,然后我将使用gradle任务进行操作,以将jar包含到依赖项中,将静态资源复制到正确的classpath等。
但是gradle实际上为依赖添加了许多jar。
我不确定gradle是否递归扫描了jar和poms的所有路径,或者可能只是 war 中WEB-INF / classes文件夹下的jar。
我可以假定poms存储库可能不是stated here。
我是正确的,是假设没有导入缩小的 war 中WEB-INF / lib文件夹中的jars?很难说,因为我的项目和相关 war 之间存在许多共享的依赖关系
那么,如果我需要按照上面的描述进行扩展和修改,在Maven repo / jcenter中声明对 war 依赖的最佳方法是什么?
更新:
我现在正尝试在下面使用答案以及此解决方案https://discuss.gradle.org/t/how-to-add-an-artifactory-war-as-a-gradle-dependency/19804/2
,仅在将具有复制的jar的目录移动到buildDir之外之后,此方法才有效
我的build.gradle
configurations {
warOnly
}
dependencies {
// not working implementation fileTree('$buildDir/explodedWar/WEB-INF/classes')
implementation fileTree('anotherDir/explodedWar/WEB-INF/classes')
// implementation fileTree('$buildDir/explodedWar/WEB-INF/lib')
implementation fileTree('anotherDir/explodedWar/WEB-INF/lib')
warOnly 'ca.uhn.hapi.fhir:hapi-fhir-jpaserver-starter:4.2.0@war'
}
tasks.register("explodeWar",Copy) {
from zipTree(configurations.warOnly.singleFile)
// into "${buildDir}/explodedWar"
into "anotherDir/explodedWar"
}
compileJava {
dependsOn explodeWar
}
最佳答案
通过声明对WAR的依赖关系,Gradle只需将其添加到匹配配置的文件列表中即可。因此,如果您在implementation
中添加WAR,它将仅在compileClasspath
和runtimeClasspath
上进行任何处理。
因此,可以肯定的是,Gradle不会以对它包含的JAR的依赖来转换您的WAR依赖。
如果要在重新打包之前使用WAR复制和修改其某些内容,则可以使用隔离的自定义配置从远程存储库中解析它。然后,您将定义一个Gradle任务,该任务将将该配置的文件作为输入并在WAR上进行所需的处理。请注意,该任务也可以是一系列将WAR操纵到一个输出,然后将该输出操纵到另一个输出的任务的起点,依此类推...
configurations {
warOnly
}
dependencies {
warOnly "com.pany:some-war:1.0"
}
tasks.register("copyWar", Copy) { // Register a copy task to modify the WAR
from(zipTree(configurations.warOnly)) // I did not run this, so you may have to get to the single file instead
// Regular copy configuration to decide a destination, perform on the fly changes, etc ...
}
关于java - gradle从 war 依赖项中导入了什么,我如何控制/操纵 war 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61779989/