I have a Java Gradle project for which I am trying to use Perf4J
. I found some examples of Perf4J only Maven. So, I modified one and ran it. Here is the Java class and Maven build file that works.
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);
}
}
}
and the 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>
The code above works fine and gives me the expected output. Now, I want to do the same to my gradle project, but I don't know how. Since, I am using @Profiled
annotation, I realized that I want AspectJ
. I found this plugin and wrote a build.gradle file for the same Java class as follows:
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'
The gradle build clean
and gradle run
commands do not give any error or warning. However, I do not see the perf4j log on the console. How can I fix this so that I can use perf4j with Gradle?
You forgot to specify the external code to weave in (the line with ajInpath
). I adopted your script (included application plugin to execute with gradle run
, updated version numbers and changed to 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'
Tested with Gradle 2.0. It would be a good idea to put the version numbers into variables and to use the gradle wrapper.