题如何在Gradle项目中使用带有Profiled注释的Perf4J?

我有一个Java Gradle项目,正在尝试使用Perf4J.我发现了一些仅Perf4J Maven的示例.因此,我修改了一个并运行了它.这是有效的Java类和Maven构建文件.

package com.mycompany.testapplication;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.perf4j.aop.Profiled;

/**
 *
 * @author Ankit Gupta
 */
public class TestClass {

    final static Logger myLogger = Logger.getLogger("org.perf4j.TimingLogger");
    final static Appender myAppender;

    static {
        myLogger.setLevel(Level.ALL);
        // Define Appender     
        myAppender = new ConsoleAppender(new SimpleLayout());

        //myAppender.setLayout(new SimpleLayout());  
        myLogger.addAppender(myAppender);
    }

    public static void main(String[] args) {
        TestClass test = new TestClass();
        test.run();
    }

    @Profiled(
            tag = "sampleTag"
    )
    private void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            java.util.logging.Logger.getLogger(TestClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

}

和pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>TestApplication</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>


    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.3</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>1.6</source>
                    <weaveDependencies>
                        <dependency>
                            <groupId>org.perf4j</groupId>
                            <artifactId>perf4j</artifactId>
                        </dependency>
                    </weaveDependencies>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <!-- use this goal to weave all your main classes -->
                            <goal>test-compile</goal>
                            <!-- use this goal to weave all your test classes -->
                        </goals>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.7</version>
        </dependency>
        <dependency>
            <groupId>org.perf4j</groupId>
            <artifactId>perf4j</artifactId>
            <version>0.9.13</version>
            <classifier>log4jonly</classifier>
        </dependency>
        <dependency>
            <groupId>commons-jexl</groupId>
            <artifactId>commons-jexl</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>


</project>

上面的代码工作正常,并给了我预期的输出.现在,我想对gradle项目做同样的事情,但是我不知道如何做.因为我使用@Profiled批注,所以我意识到我需要AspectJ.我找到了这个plugin,并为相同的Java类编写了build.gradle文件,如下所示:

apply plugin: 'java'

sourceCompatibility = '1.6'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.mycompany.testapplication.TestClass'
}

buildscript {
    repositories {
        maven {
            url "https://maven.eveoh.nl/content/repositories/releases"
        }
    }

    dependencies {
        classpath "nl.eveoh:gradle-aspectj:1.4"
    }
}

repositories {
    mavenCentral()
}

dependencies{
    compile 'org.perf4j:perf4j:0.9.16'
    compile 'org.aspectj:aspectjrt:1.6.7'
    compile 'org.perf4j:perf4j:0.9.13:log4jonly'
    compile 'commons-jexl:commons-jexl:1.1'
    compile 'log4j:log4j:1.2.17'
}

project.ext {
    aspectjVersion = '1.6.7'
}

apply plugin: 'aspectj'

gradle build clean和gradle run命令不会给出任何错误或警告.但是,我在控制台上看不到perf4j日志.如何解决此问题,以便可以在Gradle中使用perf4j?

解决方法:

您忘记指定要编织的外部代码(带有ajInpath的行).我采用了您的脚本(包括应用程序插件以通过gradle run来执行,更新了版本号并更改为Java 8:

buildscript {
    repositories {
        maven { url "https://maven.eveoh.nl/content/repositories/releases" }
    }

    dependencies {
        classpath "nl.eveoh:gradle-aspectj:1.4"
    }
}

project.ext {
    aspectjVersion = '1.8.1'
}

apply plugin: 'java'
apply plugin: 'aspectj'
apply plugin: 'application'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.perf4j:perf4j:0.9.16:log4jonly'
    compile 'org.aspectj:aspectjrt:1.8.1'
    compile 'commons-jexl:commons-jexl:1.1'
    compile 'log4j:log4j:1.2.17'

    ajInpath 'org.perf4j:perf4j:0.9.16:log4jonly'
}

sourceCompatibility = '1.8'
mainClassName = 'com.mycompany.testapplication.TestClass'

经过Gradle 2.0测试.将版本号放入变量并使用gradle包装器是一个好主意.

上一篇:AOP之aspectj


下一篇:SpringBoot自定义注解+AOP的方式实现限流