我正在使用 maven 3.0.3 和 Java7。
我有一个 AnnotationProcessor,它应该解析src/main/java
( not src/test/java
) 中带注释的 java 文件并为 JUnit-Tests 生成 Helper-Classes。这些助手类应该存储在其中,target/generated-test-sources/test-annotations
因为它们使用仅在测试范围内可用的库。(注意:只要此依赖项不在测试范围内,一切都可以正常工作,但是构建会立即失败。它绝对只需要在测试范围内/在单元测试和测试类编译期间。)
我尝试了几种配置但没有任何运气:
我将 配置为maven-compiler-plugin
在compile:compile
. 生成的 HelperClass 将存储在generated-sources/annotations
. 未在generated-test-sources/test-annotations
如需要的话。结果是,不会使用测试范围的依赖项。由于编译错误“找不到符号”,构建失败。失败
我使用了上面的配置并重新定义了生成的SourcesDirectory:
<generatedSourcesDirectory>
${project.build.directory}/generated-test-sources/test-annotations
</generatedSourcesDirectory>
Run Code Online (Sandbox Code Playgroud)
生成的类将按generated-test-sources/test-annotations
预期存储,但构建仍然失败,因为它尝试按照上述方式编译该文件并错过了测试范围的依赖项。失败
我尝试使用上述配置并排除**/generated-test-sources/test-annotations/**/*.java
以防止编译器在此阶段编译:
<excludes>
<exclude>**/generated-test-sources/test-annotations/**/*.java</exclude>
</excludes>
Run Code Online (Sandbox Code Playgroud)
没运气。与上述相同的编译器错误。失败
我将 配置为maven-compiler-plugin
在test-compile:testCompile
. 该助手类理论上可能已经在发生generated-test-sources/test-annotations
,但AnnotationProcessor会在位于在注释类不失足src/main/java
,不src/test/java
,这是编译范围的过程中AFAIK test-compile:testCompile
。因此不会找到 Annotated 类,不会生成 HelperClass,因此无法存储在generated-test-sources
. 失败
试图在compile:testCompile
and期间运行它test-compile:compile
,这在两种情况下都会导致 src/main/java …
鉴于我有两个雄辩的模型:预订和客户。
当我将所有预订与相应客户一起列出时,我还想显示相应客户的总预订量(此预订的数量 + 所有其他预订)。
示例输出:
为了避免 n+1 问题(显示此内容时每个预订额外查询一个),我想bookingsCount
为客户急切加载。
关系是:
预订: public function customer() { return $this->belongsTo(Customer::class) }
顾客: public function bookings() { return $this->hasMany(Booking::class) }
使用预先加载查询预订的示例
工作,但没有急切地加载bookingsCount:
Booking::whereNotCancelled()->with('customer')->get();
Run Code Online (Sandbox Code Playgroud)
不工作:
Booking::whereNotCancelled()->with('customer')->withCount('customer.bookings')->get();
Run Code Online (Sandbox Code Playgroud)
我了解到,您不能withCount
在相关模型的字段上使用,但您可以创建一个hasManyThrough
关系并调用withCount
该关系,例如Booking::whereNotCancelled()->withCount('customerBookings');
(请参阅此处接受的答案)。
但是:这不起作用。我想,这是因为一个预订属于一个客户,而一个客户有许多预订。
这是类 Booking 的 hasManyThrough 关系
public function customerBookings()
{
// return the bookings of this booking's customer
return $this->hasManyThrough(Booking::class, Customer::class);
}
Run Code Online (Sandbox Code Playgroud)
这是 hasManyThrough 的失败测试
/**
* @test …
Run Code Online (Sandbox Code Playgroud)