Pages

March 19, 2010

FlexPMD on Maven

Scroll down for English translation...

מכירים PMD? לא?!
לקצר את ההסבר, זהו כלי מצויין לווריפיקציה של קוד, כלומר, עובר על הקוד שלנו ומוצא בו הפרות של קונבנציות וכיו"ב לפי סט של חוקים שאותם אפילו ניתן להגדיר בצורה קאסטומאלית.
מצויין, אז יש את PMD, ויותר מכך, יש PMD עבור פלקס, שעובר על הקוד שלנו ומוצא לנו את המפתח הסורר שהכניס שטויות לקוד שאח"כ כולנו נשלם על כך. זה גם אחלא כלי ללמוד ממנו קונבנציות קוד ו- good practices.
יש 3 דרכים עיקריות להפעיל את flexPMD - ע"י CMD, ע"י ANT וע"י MAVEN (אני בכוונה מתעלם מהדרך ה MACית בשלב הזה). אני רוצה להתרכז באחרון.
בשביל להכלול את הדו"ח של PMD, אנחנו צריכים להוסיף חלק קוד קטן ל POM שלנו. בצורה שאדובי ממליצים עליה, הדו"ח יווצר בשלב של ה reporting, שקורה כשבונים את ה site בעזרת מייבן (mvn site). זה לא ממש עוזר לי, כי אני מעניין שהיצירה של הדו"ח תעשה בזמן הווריפיקציה של מייבן (אתם יכולים לקרא פה על "שלבי החיים" של מייבן). בכדי לעשות את זה, אני הוספתי את הפלאגין של מייבן כך - תגללו מטה ותראו את Code 1.

אחלא. אז יש לנו יצירה של PMD, לתוך ספריית target/pmd.
את ה PMD הזה אנחנו יכולים לראות באחד ה viewers המוצעים, אחד מהם של אדובי. זה די טוב, ברם הייתי רוצה שהבילד שלי יכשל קליל ברגע שיש הפרה של PMD, ולכן הוספתי בקונפיגורציה של הפלאגין את השורות הבאות - תגללו מטה ותראו את Code 2.

מה שזה עושה הוא, שברגע שיש הפרה הכי מינימאלית, תעיף את הבילד. אבל, אליה וקוץ בה. הפלאגין של אדובי לא ממש מכבד את הקונפיגורציה הזו, אז סאחבק פתח באג (שוב, איזה גלעד ארדן אני, הא?), ממש פה, ובאופן מפתיע כבר עובדים על תיקונו ועוד מעט אני אדע בדיוק באיזו גרסה התיקון נעשה.
זה הכל להפעם, אנשים. מקווה שתשכילו להשתמש בכלי הנהדר הזה ו... סופ"ש נעים.

Do you know PMD? You don't?
To make a story short, it's an excellent tool for varifing code, which means that it goes over our code and finds conventions violations according to a set of rules which you can even be custom defined.
So great! We have PMD and even better, we have FlexPMD that goes through our code and finds the remains of a misfit developer that we're all going to pay on later. It's also a great tool to learn code conventions and good practices.
There are 3 main ways to invoke flexPMD - command line, Ant and Maven (I'm not mentioning the Mac way on purpose). I would like to talk about the later.
In order to include reporting for PMD we need to add a bit of code to our POM file. In the manner that Adobe suggests, the report will be generated in the reporting phase, which occurs when building the Maven site (mvn site). This doesn't really help me, cause I want the report will be generated on the "verify" phase of Maven life cycle (you can read about Maven's life cycle here). In order to do that I've added the Maven plug-in like so - Scroll down to see Code 1.

Good. So now we have PMD report generation into the target/pmd directory. We can view this report in one of the offered PMD viewers. You can check out the one offered by Adobe.
It's pretty good, but I eould like my build to fail on violating the rule set of PMD, therefore I've added these following line to the plug in configuration - Scroll down to see Code 2.

What it does is, that when there is a minimum violation of PMD rules, Maven should fail the build, but unfortunately Adobe's plug-in does not respect this code, so I've opened a bug (yet another one...) right here, and supprisingly enough they started working on it and soon we shall now in what version the fix resides.
That's all for now. Hope this tool will help you write better code and… have a good one.

Cheers.

Code 1

<build>
...
<plugins>
...
<plugin>
<groupId>com.adobe.ac</groupId>
<artifactId>flex-pmd-maven-plugin</artifactId>
<version>1.0.RC5</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${basedir}/target/pmd</outputDirectory>
</configuration>
</plugin>
...
</plugins>
...
</build>



Code 2

<failOnViolation>true</failOnViolation>
<failurePriority>0</failurePriority>
<verbose>true</verbose>

2 comments:

atarsh said...

better late than never -
Thanks :)
I don't really care for Maven at this point, but FlexPMD looks great.
BTW, it can be run as an Eclipse (or builder) plugin as well, which for the less-automated amongst us, is excellent.

ps: TITHADESH for the new layout, just needs decent code highlighting now :)

Flashmattic said...

yeah, I know. the code hinting... working on it.
:)
Cheers