java - How to use Perf4J with Profiled annotation in Gradle project? -
i have java gradle project trying use perf4j
. found examples of perf4j maven. so, modified 1 , ran it. here java class , maven build file 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 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 goal weave main classes --> <goal>test-compile</goal> <!-- use goal weave 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 , gives me expected output. now, want same gradle project, don't know how. since, using @profiled
annotation, realized want aspectj
. found plugin , wrote build.gradle file same java class 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
, gradle run
commands not give error or warning. however, not see perf4j log on console. how can fix can use perf4j gradle?
you forgot specify external code weave in (the line ajinpath
). adopted script (included application plugin execute gradle run
, updated version numbers , changed 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 gradle 2.0. idea put version numbers variables , use gradle wrapper.
Comments
Post a Comment