简体   繁体   English

如何在Maven中添加test-jar作为aspectLibrary

[英]How to add test-jar as aspectLibrary in Maven

I have an aspect that I want to use in my test-classes.我有一个方面想在我的测试类中使用。 I don't want to add it to the main jar, as it would pull in test libraries like junit and mockito.我不想将它添加到主 jar 中,因为它会引入像 junit 和 mockito 这样的测试库。 While there's a configuration setting to add an aspectLibrary, it always adds the main jar, there's no way to specify the test-jar.虽然有一个配置设置来添加一个 aspectLibrary,但它总是添加主 jar,无法指定 test-jar。

My aspectj plugin looks like this:我的 aspectj 插件如下所示:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <aspectLibraries>
        <aspectLibrary>
            <groupId>aspect.test</groupId>
            <artifactId>general</artifactId>
            <type>jar</type>
        </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
        <phase>process-sources</phase>
        <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
        </goals>
        </execution>
    </executions>
</plugin>

I actually want to specify test-jar but that doesn't seem possible.我实际上想指定 test-jar 但这似乎不可能。 Without the it defaults to the jar (obviously).没有它默认为罐子(显然)。

I also might have to configure aspectj-maven-plugin for the compile and test-compile goal... but first I need to know how to specify the test-jar.我可能还需要为编译和测试编译目标配置 aspectj-maven-plugin...但首先我需要知道如何指定 test-jar。 Any suggestions are welcome.欢迎任何建议。

Please read the Maven JAR Plugin documentation, chapter How to create a jar containing test classes .请阅读 Maven JAR 插件文档, 如何创建包含测试类的 jar一章。 There are two options listed:列出了两个选项:

  • the easy way: using type "test-jar" (will not work here)简单的方法:使用类型“test-jar”(在这里不起作用)
  • the preferred way: creating a normal JAR containing only test classes, then importing it with scope "test"首选方法:创建一个仅包含测试类的普通 JAR,然后使用范围“test”导入它

We will choose the preferred way because it solves your problem.我们将选择首选方式,因为它可以解决您的问题。 So basically you do the following:所以基本上你做以下事情:

  • Create a separate module for test helper aspects/classes, put everything under src/main/java , not src/test/java .为测试助手方面/类创建一个单独的模块,将所有内容放在src/main/java而不是src/test/java AspectJ Maven plugin should have an execution with <goal>compile</goal> for that module. AspectJ Maven 插件应该使用<goal>compile</goal>执行该模块。
  • Add that module as a test-scoped dependency wherever you need the test aspects在需要测试方面的任何地方添加该模块作为测试范围的依赖项
  • Refer to the module as an <acpectLibrary> from AspectJ Maven plugin and also be careful to only use <goal>test-compile</goal> in your plugin execution for that module so as to avoid to have the aspects woven into production code or to get error messages because the dependency has test scope and is unavailable for normal compile.将该模块作为 AspectJ Maven 插件中的<acpectLibrary> ,并注意仅在该模块的插件执行中使用<goal>test-compile</goal>以避免将切面编入生产代码或获取错误消息,因为依赖项具有测试范围并且无法用于正常编译。

Because I do not want to fully quote 3 POMs and several classes here, I have created a little GitHub sample project for you.因为我不想在这里完整引用 3 个 POM 和几个类,所以我为您创建了一个小的GitHub 示例项目 Just clone it, inspect all the files and their respective locations and - be happy.只需克隆它,检查所有文件及其各自的位置,然后 - 很高兴。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM