我正在尝试Beginning Java EE 7中的练习,只是我试图使它们适应使用Gradle而不是Maven。对于第2章拦截器练习,我编写了这个build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5', 'org.jboss.weld.se:weld-se-core:2.2.10.Final'
testCompile "junit:junit:4.11"
}
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'org.agoncal.book.javaee7.chapter02.Main'
}
}
我只是直接从author's GitHub使用src目录。
./gradlew -x test build
成功,然后java -jar build/libs/gradleTest.jar
提供预期的输出(尽管它还会吐出很多意外警告)。 ./gradlew test
但是失败,并显示以下错误:org.jboss.weld.exceptions.DeploymentException: WELD-001417: Enabled interceptor class <class>org.agoncal.book.javaee7.chapter02.LoggingInterceptor</class> in file:/home/allen/gradleTest/build/resources/main/META-INF/beans.xml@7 does not match an interceptor bean: the class is not found, or not annotated with @Interceptor and still not registered through a portable extension, or not annotated with @Dependent inside an implicit bean archive
beans.xml和所有类文件都直接来自GitHub上作者的存储库,并且看起来与上述错误完全相同。 beans.xml声明了拦截器,并且拦截器类用@Interceptor注释。
我的猜测是问题出在我的Gradle构建上。有人看到问题出在哪里吗?
最佳答案
艾伦,我假设您正在执行相同的示例:agoncal-book-javaee7 / chapter02 / chapter02-putting-together。
当我尝试“mvn test”时,它可以工作,并且发现了maven和gradle之间的build / target目录之间的差异。 Maven将类和资源都合并在一个目录中(就像将其包装在jar中一样),但是gradle将两者分开,一个文件夹用于类,另一个文件夹用于资源。读取bean.xml时,用于测试的WeldContainer无法提取该类。
这是我的build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5', 'org.jboss.weld.se:weld-se-core:2.2.10.Final'
compile 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.5.2'
compile 'javax:javaee-api:7.0'
compile 'org.apache.derby:derby:10.10.1.1'
testCompile "junit:junit:4.11"
}
test.doFirst {
copy {
from 'build/resources/test/META-INF/beans.xml'
into 'build/classes/main/META-INF'
}
copy {
from 'build/resources/test/META-INF/beans.xml'
into 'build/classes/test/META-INF'
}
}
运行时的结果:
> chapter02-putting-together git:(master) X gradle clean test
:clean
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses
:test
BUILD SUCCESSFUL
Total time: 4.302 secs
关于jakarta-ee - 由于找不到拦截器类,gradle测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813875/