using gradle with aspectj
In this post I want to show you how easy it is to build your aspectj projects with gradle. IMHO gradle is the most flexible, versatile build tool for JVM based projects. It fully supports ant and integrates well in maven environments.
But lets dive into the example I prepared.
The example project is based on the “Bean Example” provided by the ajdt plugin for eclipse. This example contains three classes:
We have a basic bean class named Point:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.breskeby.bean; class Point { private int x = 0; private int y = 0; public int getX(){ return x; } public int getY(){ return y; } public void setX(int newX) { this.x = newX; } public void setY(int newY) { this.y = newY; } } |
Now I want to use this bean with full property change listener support without pollute my Point source code. So I create an aspectj BoundPoint which weaves the propertychange support into the bean:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /* * Copyright (c) 1998-2002 Xerox Corporation. All rights reserved. * * Use and copying of this software and preparation of derivative works based * upon this software are permitted. Any distribution of this software or * derivative works must comply with all applicable United States export * control laws. * * This software is made available AS IS, and Xerox Corporation makes no * warranty about the software, its performance or its conformity to any * specification. */ package com.breskeby.bean; import java.beans.*; import java.io.Serializable; /* * Add bound properties and serialization to point objects */ aspect PointAspect { /* * privately introduce a field into Point to hold the property * change support object. `this' is a reference to a Point object. */ private PropertyChangeSupport Point.support = new PropertyChangeSupport(this); /* * Introduce the property change registration methods into Point. * also introduce implementation of the Serializable interface. */ public void Point.addPropertyChangeListener(PropertyChangeListener listener){ support.addPropertyChangeListener(listener); } public void Point.addPropertyChangeListener(String propertyName, PropertyChangeListener listener){ support.addPropertyChangeListener(propertyName, listener); } public void Point.removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { support.removePropertyChangeListener(propertyName, listener); } public void Point.removePropertyChangeListener(PropertyChangeListener listener) { support.removePropertyChangeListener(listener); } public void Point.hasListeners(String propertyName) { support.hasListeners(propertyName); } declare parents: Point implements Serializable; /** * Pointcut describing the set<property> methods on Point. * (uses a wildcard in the method name) */ pointcut setter(Point p): execution( public void Point.set*(*) ) && target(p); /** * Advice to get the property change event fired when the * setters are called. It's around advice because you need * the old value of the property. */ void around(Point p): setter(p) { String propertyName = thisJoinPointStaticPart.getSignature().getName().substring("set".length()); int oldX = p.getX(); int oldY = p.getY(); proceed(p); if (propertyName.equals("X")){ firePropertyChange(p, propertyName, oldX, p.getX()); } else { firePropertyChange(p, propertyName, oldY, p.getY()); } } /* * Utility to fire the property change event. */ void firePropertyChange(Point p, String property, double oldval, double newval) { p.support.firePropertyChange(property, new Double(oldval), new Double(newval)); } } |
To test the correct behaviour of this aspect we use a junit test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.breskeby.bean; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import org.junit.Before; import org.junit.Test; import static org.mockito.Mockito.*; public class PointTest { //class under test private Point cut; @Before public void setup(){ cut = new Point(); } @Test public void testPropertyChangeEventFired(){ PropertyChangeListener changeListenerMock = mock(PropertyChangeListener.class); cut.addPropertyChangeListener(changeListenerMock); cut.setX(1); verify(changeListenerMock,times(1)) .propertyChange((PropertyChangeEvent) anyObject()); } } |
After explaining the boring part of the example we can focus on the automatic build now.
As a starting point we use a simple build file that uses the java plugin:
1 2 3 4 5 6 7 8 9 10 11 | apply id:'java' repositories { mavenCentral() } dependencies{ compile "aspectj:aspectjlib:1.5.3" testCompile "junit:junit:4.7" testCompile "org.mockito:mockito-all:1.8.2" } |
When running “gradle test” gradle tells us, that something went wrong:
1 2 3 4 5 6 7 8 | Example/src/test/java/com/breskeby/bean/PointTest.java:26: cannot find symbol symbol : method addPropertyChangeListener(java.beans.PropertyChangeListener) location: class com.breskeby.bean.Point cut.addPropertyChangeListener(changeListenerMock); ^ 1 error FAILURE: Build failed with an exception. |
To get this running we need to replace the compileJava task of the java plugin by a custom task that uses the aspectj compiler. The easiest way to get this working is to use the iajc ant task. the aspectj compiler demands additional configurations to setup the classpath for the iajc task and the classpaths for the inpath an aspectpath. To add custom configurations we simple add the following lines to our build file
1 2 3 4 5 | configurations { ajc aspects ajInpath } |
in our dependency block we add the aspect ant task to ajc. Since we don’t use external dependencies for inpath and aspectpath we needn’t add here anything. The complete dependency section for our tiny example is shown here:
1 2 3 4 5 6 | dependencies{ ajc "aspectj:aspectjtools:1.5.3" compile "aspectj:aspectjrt:1.5.3" testCompile "junit:junit:4.7" testCompile "org.mockito:mockito-all:1.8.2" } |
As mentioned we have to replace the compileJava task with our own one. Our custom task looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 | task compileJava(dependsOn: JavaPlugin.PROCESS_RESOURCES_TASK_NAME, overwrite: true) < < { ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath) ant.iajc(source:sourceCompatibility, target:targetCompatibility, destDir:sourceSets.main.classesDir.absolutePath, maxmem:"512m", fork:"true", aspectPath:configurations.aspects.asPath, inpath:configurations.ajInpath.asPath, sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){ sourceroots{ sourceSets.main.java.srcDirs.each{ pathelement(location:it.absolutePath) } } } } |
running now “gradle test” should work and the test succeeds. The complete working build.gradle file looks like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | apply id:'java' repositories { mavenCentral() } configurations { ajc aspects ajInpath } dependencies{ ajc "aspectj:aspectjtools:1.5.3" compile "aspectj:aspectjrt:1.5.3" testCompile "junit:junit:4.7" testCompile "org.mockito:mockito-all:1.8.2" } task compileJava(dependsOn: JavaPlugin.PROCESS_RESOURCES_TASK_NAME, overwrite: true) < < { ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath) ant.iajc(source:sourceCompatibility, target:targetCompatibility, destDir:sourceSets.main.classesDir.absolutePath, maxmem:"512m", fork:"true", aspectPath:configurations.aspects.asPath, inpath:configurations.ajInpath.asPath, sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){ sourceroots{ sourceSets.main.java.srcDirs.each{ pathelement(location:it.absolutePath) } } } } |
I wrapped all aspectj specific parts of the build script to a aspectj plugin available at
http://github.com/breskeby/gradleplugins/raw/0.9-upgrade/aspectjPlugin/aspectJ.gradle
If you’re already running gradle version 0.9+ you can use this plugin and the build file looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 | apply url:'http://github.com/breskeby/gradleplugins/raw/0.9-upgrade/aspectjPlugin/aspectJ.gradle' repositories { mavenCentral() } dependencies{ ajc "aspectj:aspectjtools:1.5.3" compile "aspectj:aspectjrt:1.5.3" testCompile "junit:junit:4.7" testCompile "org.mockito:mockito-all:1.8.2" } |
This sample project is available at github and comments and suggestions are appreciated.
regards,
René
Demagogie 1.5
Es folgt der mitschnitt einer rede von fräulein [tag]zensursula[/tag]. ein musterbeispiel an schamloser lügerei, hetze, demagogie und widerlicher anachronistischer rhetorik der 30/40er. die version 1.5 im titel hab ich gewählt es mir eine 2.0 definitiv nicht wert war. stellvertretend für die armen politiker in amt und würden, die sich durch etwaige aussagen quasi selbst welches amtes auch immer entheben würden, ziehe ich hiermit den nazivergleich aus meiner nichtvorhandenen kopfbedeckung und verweise auf etwaige führende figuren der 30er Jahre, welche auch das kinderreiche familienglück dem pöbel öffentlich zur schau stellten und gleichzeitig im sportpalast das “R” fast noch schöner rollen konnten als diese dame hier. wenn sie ‘”ruck zuck” sagt läufts mir echt kalt den rücken runter. hier erstmal das video:
das kann einem wirklich angstmachen wie sie hier gegen CCC und piratenpartei hetzt. und wo wir schonmal dabei sind und die gute frau neben den missbrauchsopfern, auch den armen antoine de saint-exupéry hier für ihre zwecke missbraucht muss ich schon noch mal ein besseres zitat vom selbigen nachlegen, dass mir bei den worten “HIMMEL NOCHMAL” in den sinn kommt:
wenn der glaube erlischt, stirbt gott und erweist sich fortan als unnötig
und eins noch ursel. ich glaub die linken trauen dir inzwischen alles zu. auch dieser linke ungewaschene schwarze block vom verfassungsgericht in karlsruhe kann dir ja wohl nicht ernsthaft mit meinungsfreiheit oder gewaltenteilung kommen. ZEIGS ihnen ursel, kämpf dein(en) kampf
Der Pritlove füllt das Sommerloch
Das gabs wohl noch nie beim chaosradio. gestern schrieb tim pritlove in seinem blog dass er momentan in der komfortablen situation ist, mehrere sendungen des großartigen chaosradio express (CRE) auf halde zu haben. um jetzt den geneigten hörer nicht zu überfrachten lässt er die aufzeichnungen nur nach und nach raus und wünscht erstmal die längst überfällige huldigung seitens der blogosphäre (ich mag dieses wort eigentlich überhaupt nicht).
es ist wirklich schwierig eine folge als favourite hier besonders hervorzuheben, da müsste ich unter in den letzten jahren aufgezeichneten cre folgen mindestens zwei dutzend hier auflisten. unter den sendungen der letzten wochen würde ich persönlich den cre126 als beste sendung küren. die vielen super sendungen im letzten jahr zeigen, dass sich die bahncard 100 aktion für alle beteiligten gelohnt hat. … egal ich werd jetzt müde und deshalb war es das meinerseits mit der cre werbung für heute. also verneige ich mich noch einmal und geh dann ins bett. schneller als ich war unter anderem antiblau. ich war aber schneller als http://soupeter.soup.io/post/25241060/chaosradio-express-133-mp3
[tags]blog4cre[/tags]
gruß brs
Update your IDE
Its just some days ago that the eclipse foundation released version 3.5 (a.k.a galileo) of the best (opensource) IDE. The new [tag]eclipse[/tag] release is called [tag]galileo[/tag], traditional named after a jupiter moon. I was a bit sceptic because the first release of eclipse ganymede (3.4) last year wasn’t that stable and it tooks two minor updates to get a real stable version of ganymede.
On the download page of galileo at http://eclipse.org/downloads/ you can find 9 different galileo based IDE packages. I think (and the downloads counter affirms that) the JEE package is the most popular eclipse package. That download page also clarifies, that eclipse isn’t a pure [tag]Java[/tag] IDE anymore. In the meantime eclipse is also a great IDE for C/C++ and PHP developers.
There is no predefined package for python developers. No need to cry
. With the [tag]pydev[/tag] plugin (http://pydev.sourceforge.net/ ) [tag]python[/tag] and or jython developers should also feel confortable with eclipse.
According to my dailiy development work, my first choice to download was the jee package. In comparison to the ganymede release in 2008 the release train of ganymede seems to be much faster. The download of 187.8mb for the cocoa version for my mac was done in less than 5 minutes. Galileo is the first release, which supports the cocoa api for mac. Altough galileo is also available as a carbon version, I think the cocoa version is the future, since carbon was just an API collection to support mac developers on updating mac-os software to mac-os x its days are numbered.
Working on java projects with jdt hasn’t changed a lot. JDT offers just minor improvements like the improved java comparison editor which adopted a lot of the features you already know from the plain java editor. The most noticable change during importing my old workspace was the update of the integrated junit from version 4.4 to 4.5. In some circumstances junit 4.5 works different than junit4.4. It wouldn’t go amiss to give the developer the oppertunity to choose between junit 4.4 and 4.5.
The PDE project improved a lot since ganymede. The equinox runtime galileo predicated on, implements the OSGi specification draft version 4.2. Among other things, the OSGi Specification V4.2 addresses improvements to the OSGi security layer, transactions in osgi, a bundletracker modeled on the already known servicetracker and a common command line interface.
The enhancements of the OSGi tooling support, assures that eclipse is the cutting edge tool for OSGi developers. Primarily the target platform management made good progress. working with targets was a lot of pain in the past. now you can manage different target platforms, define targets in one sole file, share that file with your colleagues and even populate your target definitions using p2.
After starting galileo for the first time I missed a lot of plugins I used in ganymede. Since we made heavy usage of the dropins folder to manage (manually) added plugins I just copied the dropin folder of my ganymede to the galileo directory. Pleasantly surprised I was able to run my galileo dist with nearly all old plugins working. Even the JProfiler plugin, that is official only supporting ganymede is working without any problems. Merely the ajdt plugin of ganymede wasn’t working anymore. But having problems with mirrored update sites on the first galileo day and no available release version of ajdt, the ajdt guys fixed these issues in just a couple of days. By now a release version of [tag]ajdt[/tag] v2.0 is available and the update sites are working again. The second plugin I had serous trouble with was the google appengine for java plugin. Unfortunately this plugin isn’t yet available for galileo.
To sum up, one could say that updating your eclipse IDE to the latest galileo should be very easy because the eclipse guys definitely have done their homework.
regards,
René
ps: comments are welcome!
Petition gegen Internetzensur
unter https://epetitionen.bundestag.de/index.php?action=petition;sa=details;petition=3860 kann jeder mitmachen! also los und ran an die tasten!
gruß brs
freies Internet in ernster gefahr!
hallo zusammen,
der artikel des ccc zum 1.4.2009 war leider wirklich kein aprilscherz. jetzt gibt es unter http://www.zensurprovider.de eine liste der provider, die blind der zensursula ohne jede gesetzesgrundlage folgen. nur ein manitu.de stemmt sich wie ein ein kleines gallisches dorf sich gegen die grundgesetzamateure von bka und bundestag. am freitag ruft der ccc zur demo in berlin auf. also hin da!
aus aktuellem anlass hier nochmal der artikel 5 unseres irgendwie ja doch noch gültigen grundgesetzes:
(1) Jeder hat das Recht, seine Meinung in Wort, Schrift und Bild frei zu äußern und zu verbreiten und sich aus allgemein zugänglichen Quellen ungehindert zu unterrichten. Die Pressefreiheit und die Freiheit der Berichterstattung durch Rundfunk und Film werden gewährleistet. Eine Zensur findet nicht statt.
(2) Diese Rechte finden ihre Schranken in den Vorschriften der allgemeinen Gesetze, den gesetzlichen Bestimmungen zum Schutze der Jugend und in dem Recht der persönlichen Ehre.(3) Kunst und Wissenschaft, Forschung und Lehre sind frei. Die Freiheit der Lehre entbindet nicht von der Treue zur Verfassung.
in diesem sinne,
gruß brs![tags]zensur, internet[/tags]
Resturlaub
schon zu weihnachten hatte ich das zweite [tag]buch[/tag] “resturlaub” von [tag]tommy jaud[/tag] geschenkt bekommen. da ich aber noch einige bücher auf meiner todo liste hatte, bin ich erst letzte woche zu “pitschis” abenteuer gekommen. einigen dürfte herr jaud durch die verfilmung seines buches “der vollidiot” bekannt sein. aber wohl aufgrund meiner abneigung zu olli pocher, habe ich den film selbst nie gesehen und tommy war mir bis weihnachten letzten jahres kein begriff.
der [tag]resturlaub[/tag] beginnt im beschaulichen bamberg. jaud legt bei der beschreibung bambergs bzw. frankens schon ziemlich ins detail und so lernt der geneigte nicht – bamberger hier im buch endlich auch mal warum es heißt “wir gehen aufn keller”. Der Pitschi (der urlauber im buch) wegt schon oft des mannes mitgefühl. spätestens bei dem gedanken an die “eigene kieselauffahrt knapp 15 minuten ausserhalb”.
pitschis freunde sind fast alle in den hafen der ehe eingekehrt und auch er wird immer deutlicher mit den drei dingen konfrontiert die wohl irgendwann jeden mann in die ecke drängen: aufkeimender kinderwunsch ihrerseits, sowas wie midlife-crisis und freunde, die eigentlich eher altlasten aus früheren tagen sind. “resturlaub” könnte auch “auszeit” heißen. doch statt wie ein kaninchen vor der schlange ergreift pitschi (a.k.a. mausbär) die flucht nach vorn.
diese flucht beschreibt jaud mit viel humor und trifft irgendwie dabei auch immer die richtigen worte. ein hauch sarkasmus schwingt immer mit. okay, manchmal möchte man den protagonisten schon auf den hinterkopf hauen und sagen “lass es rudi, lass es sein”. aber er nimmt zielgerichtet alle fettnäpfchen die sich ihm vor die füße werfen.
mein persönliches highlight ist heidi. es ist echt unglaublich wie gut jaud ihren schwäbischen dialekt in schrift umsetzt. ich konnte quasi beim lesen ihre unsägliche stimme vernehmen.
nicht weiter erwähnen muss ich wohl, dass dieses buch gelesen sein sollte. die frau eines freundes bezeichnete das buch als “männerbuch” und genau das ist es wohl auch.
gruß brs
paradoxon
der fm4 ombudsmann machte mich heute auf ein paradoxon aufmerksam, das im gegensatz zu anderen seiner art, jeder schnell selbst gebastelt hat. ihr braucht nur butter und eine katze.
aber jetzt zum problem: jeder weiß:
- eine katze landet immer auf den pfoten.
- eine toast landet immer auf der seite mit der butter.
angenommen wir schmieren den rücken unserer katze jetzt mit butter ein und schmeißen sie vom balkon, landet sie auf den pfoten oder auf den rücken? gibts testergebnisse?
[tags]paradoxon, katze, butter[/tags]
gruß brs

