我正在嘗試使用Gradle構建幾個罐子,而不是維護一個包含EJB的類的列表,以便我可以單獨部署它們我認爲在製作時可能很整潔地掃描類罐子裏。發佈工件覆蓋Gradle中的其他工件
與其加載類並使用反射來獲取註釋我認爲使用asm掃描類可能會更簡單,因此在其中一個任務中使用Chuncky ClassReader。
我不認爲這是問題所以可以忽略,基本上我有2個任務,我用它來定義罐子的內容,都報告說,不同的內容是通過eachFile打印出來進入他們,但是當我查看發佈存儲庫位置時,這兩個文件和關聯的sha1都是相同的。
Gradle壞了,或者更可能是我做了一些瘋狂的事,但看不到它是什麼,任何人都可以幫忙?
順便說一下,如果我禁用任何jar文件的發佈,那麼得到創建的是正確的,所以我認爲這是發佈的錯誤,而不是震驚,但可能是錯誤的。
// ASM is used to interpret the class files, this avoids having to load all classes in the vm and use reflection
import org.objectweb.asm.*
task ejbJar(type: Jar) {
//outputs.upToDateWhen { false }
from "${project.buildDir}/classes/main"
eachFile { println "EJB server: ${name}" }
include getEjbClassFiles(project.buildDir)
}
task clientEjbJar(type: Jar) {
//outputs.upToDateWhen { false }
from "${project.buildDir}/classes/main/com/company/core/versioner"
eachFile { println "Client EJB ${name}" }
include '**/*'
}
artifacts {
archives clientEjbJar
archives ejbJar
}
String[] getEjbClassFiles(base) {
def includedFiles = []
def baseDir = project.file("${base}/classes/main")
def parentPath = baseDir.toPath()
if (baseDir.isDirectory()) {
baseDir.eachFileRecurse(groovy.io.FileType.FILES) { file ->
if(file.name.endsWith('.class')) {
//get hold of annotations in there --- org.objectweb.asm.Opcodes.ASM4
def reader = new ClassReader(file.bytes).accept(
new ClassVisitor(Opcodes.ASM4) {
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if(desc.equals("Ljavax/ejb/Stateless;") ||
desc.equals("Ljavax/ejb/Stateful;")) {
includedFiles += parentPath.relativize(file.toPath())
}
return null //no interest in actually visiting the annotation values
}
},
ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE
)
}
}
}
return includedFiles
}
publishing {
publications {
mypub(IvyPublication) {
artifact(ejbJar) {
name 'ejb'
}
artifact(clientEjbJar) {
name 'client-ejb'
}
}
}
repositories {
ivy {
name 'personal'
url "${ant['developer.repository']}/"
layout 'pattern', {
artifact "[organisation]/[module]/[artifact]/[revision]/[type]/[artifact]-[revision].[ext]"
ivy "[organisation]/[module]/[type]/[revision]/[type]/[type]-[revision].[ext]"
}
}
}
}
我做下破事成更簡單的形式,因爲我認爲這可能是一個搖籃錯誤。
的簡化形式爲:
apply plugin: 'java'
apply plugin: 'ivy-publish'
task bigJar(type: Jar) {
from "${rootDir}/src/main/resources"
include '**/*'
}
task smallJar(type: Jar) {
from "${rootDir}/src/main/resources/A/B"
include '**/*'
}
group 'ICantBeEmpty'
artifacts {
archives bigJar
archives smallJar
}
publishing {
publications {
mypub(IvyPublication) {
artifact(bigJar) { name 'biggie' }
artifact(smallJar) { name 'smallie' }
}
repositories {
ivy {
name 'personal'
url "c:/temp/gradletest"
layout 'pattern', {
artifact "[organisation]/[module]/[artifact]/[revision]/[type]/[artifact]-[revision].[ext]"
ivy "[organisation]/[module]/[type]/[revision]/[type]/[type]-[revision].[ext]"
}
}
}
}
}
這導致2個文件在C:/temp/gradletest/ICantBeEmpty/report-bug/biggie/unspecified/biggie-unspecified.jar和c:/溫度/ gradletest/ICantBeEmpty/report-bug/smallie/unspecified/smallie-unspecified.jar 這兩個文件都是相同的,但我想我知道爲什麼會看到我以後的答案。
感謝您的興趣,但'舊的方式'鏈接顯示我正在做的事情,除了我正在使用常春藤回購而不是平面文件。因爲處於孵化狀態的東西並不意味着它不應該被使用,並且會引發問題,這是開源演化如此迅速的本質。我現在已經確定了這個問題,因爲用於在配置中定位工件的名稱不是唯一的,並且會發布完整的描述以保存其他Gradle用戶同樣的麻煩。 – 2013-05-08 10:42:44