繁体   English   中英

在Maven中使用AspectJ方面编织生产类仅用于测试执行

[英]Weaving production classes with AspectJ aspects in Maven for tests execution only

我有一个Maven构建的项目,需要为方法添加一些基本的性能跟踪。 我决定使用AspectJ。 主要要求是将跟踪方面编织到生产类中, 但仅用于单元测试执行阶段

我能够在Maven中配置编织,但是在执行测试后,应用了方面的相同生产类转到打包战争。

这个案子看起来很常见,但我无法在网上找到解决方案。

您可以将方面放在测试目录中,并在test-compile配置中将weaveMainSourceFolder标志设置为true

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
        <executions>
            <execution>
                <id>test-compile</id>
                <configuration>
                    <weaveMainSourceFolder>true</weaveMainSourceFolder>
                </configuration>
                <goals>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
</plugin>

请参阅http://mojo.codehaus.org/aspectj-maven-plugin/test-compile-mojo.html

我用加载时编织解决了这个问题。 这样就可以在运行单元测试时(在运行单元测试时通过命令行参数)进行编织,但是您的方面不会编织到已发布的工件中。

例如,我想在我的单元测试中伪造系统时钟,但显然不会在实时代码中弄乱它。 这是我的方面类:

@Aspect
public class TweakSystemAspects {
    private static long timeOffsetMillis = 0;

    public static void advanceTime(int amount, TimeUnit unit) {
        timeOffsetMillis += unit.toMillis(amount);
    }

    @Around("call (long System.currentTimeMillis())")
    public long aroundSystemTime(ProceedingJoinPoint joinPoint) throws Throwable {
        return ((Long) joinPoint.proceed()) + timeOffsetMillis;
    }
}

显然,这是通过调用TweakSystemAspects.advanceTime()方法在单元测试中使用来伪造系统中的时间流逝。 为了完成加载时间编织,我只需要创建一个定义我的方面的aop.xml文件(并且编织应该在所有类中进行):

<aspectj>
    <aspects>
        <aspect name="com.mypackage.TweakSystemAspects"/>
    </aspects>
    <weaver options="-nowarn -Xlint:ignore"/>
    <!-- During testing this was useful, but I didn't want all that output normally. -->
    <!--<weaver options="-verbose -showWeaveInfo"/>-->
</aspectj>

最后,我在我的pom文件中进行了更改,以声明AspectJ运行时依赖项并告诉surefire进行运行时编织。

<project ...>
    ...
    <properties>
        ...
        <version.aspectj>1.8.10</version.aspectj>
    <properties>

    <dependencies>
        ...
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${version.aspectj}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            ...
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- For Load Time Weaving of our AspectJ helper code -->
                    <argLine>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${version.aspectj}/aspectjweaver-${version.aspectj}.jar</argLine>
                    ...
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

我会在专用模块中执行此操作,使用Maven Dependency Plugin在generate-test-sources阶段解压缩“正在测试”的工件,然后编织类并最终运行测试。


让我试着说明一下我的意思。 让我们假设以下项目结构:

.
|-- pom.xml
`-- some-module    // this is the module that we want to weave 
    |-- pom.xml    // but only for testing purpose
    `-- ...

所以我的建议是做这样的事情:

.
|-- pom.xml
|-- some-module      
|   |-- pom.xml      
|   `-- ...
`-- test-module    // we're going to weave the classes here because we don't want
    |-- pom.xml    // the tracing aspect to be packaged in the "production" jar
    `-- ...

我们的想法是有一个额外的“测试模块”,我们将解压缩我们想要测试的工件,这样我们就可以编织它的类而不会影响“真正的”生产jar。

为此,请声明对正在测试的模块的依赖关系并使用dependency:unpack在调用AspectJ插件编织“主”类之前,将unpack解压缩到target/classes类中。

基于AspectJ编译器Maven插件中提供的示例- 使用类似下面的内容应该有效:

<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
               </executions>
           </plugin>
           ...
       </plugins>
   <build>
   ...
</project>

暂无
暂无

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

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