我有一个Java Gradle项目,我正在尝试使用Perf4J
。我发现了一些只有Maven的Perf4J的例子。所以,我修改了一个并运行它。这是可用的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日志。我该如何解决这个问题,以便我可以将perf4j与Gradle一起使用?
答案 0 :(得分:3)
您忘记指定编织的外部代码(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包装器是个好主意。