gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:39 26 4
gpt4 key购买 nike

我有一个 Java Gradle 项目,我正尝试为其使用 Perf4J。我发现了一些 Perf4J only 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 cleangradle run 命令不会给出任何错误或警告。但是,我没有在控制台上看到 perf4j 日志。我该如何解决这个问题,以便我可以将 perf4j 与 Gradle 结合使用?

最佳答案

您忘记指定要嵌入的外部代码(带有 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 包装器是个好主意。

关于java - 如何在 Gradle 项目中使用带有 Profiled 注释的 Perf4J?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com