首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Gradle项目中使用Perf4J与Profiled注释?

如何在Gradle项目中使用Perf4J与Profiled注释?
EN

Stack Overflow用户
提问于 2014-06-25 23:31:38
回答 1查看 823关注 0票数 3

我有一个Java项目,我正在尝试使用Perf4J。我只找到了一些Perf4J示例。所以,我修改了一个并运行了它。下面是可以工作的Java类和Maven构建文件。

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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。我找到了这个插件,并为同一个build.gradle类编写了一个build.gradle文件,如下所示:

代码语言:javascript
复制
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 cleangradle run命令不提供任何错误或警告。但是,我没有在控制台上看到perf4j日志。我如何解决这个问题,以便我可以在Gradle中使用perf4j?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-09 10:01:15

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

代码语言:javascript
复制
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包装是个好主意。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24420252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档