如何在Gradle上运行AspectJ加载编织(特别是libgdx构建)

Dan*_*son 7 java aspectj gradle libgdx

所以我想在我的libgdx游戏中注入一些测试和日志记录功能.

所以我在主桌面gradle依赖项中添加了以下内容.

    compile 'org.aspectj:aspectjweaver:1.8.2'
    compile "org.aspectj:aspectjrt:1.8.2"
Run Code Online (Sandbox Code Playgroud)

最初它没有找到依赖项,但是通过关闭离线模式并关闭并重新打开我的IntelliJ项目(Gradle同步按钮不起作用)解决了这个问题.

我的理解是aspectjweaver必须作为java代理加载.所以我找到gradle下载它的地方,并将以下内容添加到我的VM运行时配置选项中

-javaagent:/Users/daniel/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjweaver/1.8.2/4963c0bef4748d5ad039cc26c1ac32a082eb755e/aspectjweaver-1.8.2.jar
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,这给了我以下警告信息

objc[66447]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Run Code Online (Sandbox Code Playgroud)

没有-javaagent行,该消息不存在.

我试图按照这个例子来进行加载编织示例. http://andrewclement.blogspot.co.uk/2009/02/load-time-weaving-basics.html

但是,我不需要单独的可重用方面,因此我只是在src目录中创建了一个基本的aop.xml文件,其中包含以下内容.

<aspectj>
    <weaver options="-verbose"/>
</aspectj>
Run Code Online (Sandbox Code Playgroud)

显然我还没有设置任何东西,我只想确认设置是否正常.该教程与我的目标环境之间存在足够的差异,我认为很多可能会出错.

我不介意解决方案是编译时还是类加载,只要它在libgdx/gradle环境中可预测地工作即可.由于我不熟悉libgdx/gradle的建筑环境和要求,我选择了查看类加载解决方案.

Thanx提前.

更新:将尝试通过这个http://www.breskeby.com/2010/02/using-gradle-with-aspectj/

...但是教程中没有提到真正熟悉的iajc,并且知道如何在libgdx构建脚本中使用它也似乎很复杂.

Hey*_*nge 0

我不会提供 gradle 实现,而是使用 AspectJ 提供 Maven 实现。显然,您必须进行定制/调整以满足您的需求。下面是一个bar.foo采用两个参数((String, Boolean)带有@Around通知和 )的方法的一个方面ProceedingJoinPoint。任何建议或切入点显然都可以实现。

爪哇

@Aspect
@ajcPrivileged
public class MyAspect {
    @Pointcut("execution(* bar.foo(String, Boolean)) && args(string, bool)")
    void foo(String string, Boolean bool) {}

    @Around(value = "loadFile(filePathName, toGray)", argNames = "filePathName, toGray")
    public MyReturnType foo(final ProceedingJoinPoint joinPoint, final String string, final Boolean bool) throws MyEx {
        //impl
    }
}
Run Code Online (Sandbox Code Playgroud)

聚甲醛

请参阅编织第三方依赖注释来定义要编织的第三方库。

<project ..>
    ..
    <properties>
        <aspectj-maven-plugin.version>1.7</aspectj-maven-plugin.version>
        <aspectjrt.version>1.8.2</aspectjrt.version>
        ..
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectjrt.version}</version>
        </dependency>
        ..
    </dependencies>
    <build>
        ..
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>${aspectj-maven-plugin.version}</version>
                <configuration>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <!-- Weave third party dependency -->
                    <weaveDependencies>
                        <weaveDependency>
                            <groupId>yourThirdPartyDepGroupId</groupId>
                            <artifactId>yourThirdPartyDepGroupIdArtifactId</artifactId>
                        </weaveDependency>
                    </weaveDependencies>
                </configuration>
                <!-- Weave on compile -->
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>${lifecycle-mapping.version}</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>aspectj-maven-plugin</artifactId>
                                        <versionRange>[1.7,)</versionRange>
                                        <goals>
                                            <goal>compile</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)