<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MDA, MDSD, MDE, MDWhatever! &#187; plugins</title>
	<atom:link href="http://mdwhatever.free.fr/index.php/tag/plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://mdwhatever.free.fr</link>
	<description>About model driven engineering</description>
	<lastBuildDate>Mon, 12 Mar 2012 19:56:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to debug your MagicDraw plugin with Eclipse</title>
		<link>http://mdwhatever.free.fr/index.php/2010/07/how-to-debug-your-magicdraw-plugin/</link>
		<comments>http://mdwhatever.free.fr/index.php/2010/07/how-to-debug-your-magicdraw-plugin/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 05:08:10 +0000</pubDate>
		<dc:creator>xavier</dc:creator>
				<category><![CDATA[Model Driven]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[magicdraw]]></category>
		<category><![CDATA[magicdraw open API]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://mdwhatever.free.fr/?p=143</guid>
		<description><![CDATA[After reading this post, you are now MagicDraw open API experts! But you still need to debug your plugins! Here is a short presentation on how to debug your MagicDraw plugin with Eclipse. Create your MagicDraw user library First of all you need to create a user library (Window&#62;Preferences&#62;Java&#62;Build Path&#62;User Libraries) in Eclipse containing every]]></description>
			<content:encoded><![CDATA[<p>After reading this <a title="post" href="http://mdwhatever.free.fr/index.php/2010/03/how-to-develop-your-magicdraw-plugin/" target="_blank">post</a>, you are now MagicDraw open API experts! But you still need to debug your plugins! Here is a short presentation on how to debug your MagicDraw plugin with Eclipse.</p>
<p><span id="more-143"></span></p>
<h2>Create your MagicDraw user library</h2>
<p>First of all you need to create a user library (Window&gt;Preferences&gt;Java&gt;Build Path&gt;User Libraries) in Eclipse containing <strong>every *.jar</strong> from the &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;/lib</strong>&#8221; and its subfolders.</p>
<p style="text-align: center;"><a href="http://mdwhatever.free.fr/wp-content/uploads/MagicDrawUserLibrary.png" rel="shadowbox[post-143];player=img;" title="MagicDraw User Library"><img class="size-medium wp-image-148 aligncenter" style="border: 1px solid black;" title="MagicDraw User Library" src="http://mdwhatever.free.fr/wp-content/uploads/MagicDrawUserLibrary-300x187.png" alt="" width="300" height="187" /></a></p>
<h2>Create your debug project</h2>
<p>Create a new Java Project, and then you need to reference your plugin project in the build path of your debug project : (Right click ﻿on the project&gt;Configure build path)</p>
<p style="text-align: center;"><a href="http://mdwhatever.free.fr/wp-content/uploads/Properties-for-magicdraw-debug-.png" rel="shadowbox[post-143];player=img;" title="Add your plugin project to the build path"><img class="aligncenter size-medium wp-image-144" style="border: 1px solid black;" title="Add your plugin project to the build path" src="http://mdwhatever.free.fr/wp-content/uploads/Properties-for-magicdraw-debug--300x199.png" alt="" width="300" height="199" /></a></p>
<p>Here I take the Eclipse project given in this <a title="post" href="http://mdwhatever.free.fr/index.php/2010/03/how-to-develop-your-magicdraw-plugin/" target="_blank">post</a>.</p>
<p>Then you need to create a main class which will init your plugin and launch MagicDraw :</p>
<pre lang="java">
package fr.free.mdwhatever.magicdrawDebug;

import com.nomagic.magicdraw.core.Application;
import com.nomagic.runtime.ApplicationExitedException;

import fr.free.mdwhatever.simplePlugin.SimplePlugin;
/**
 * This class allows you to debug your MagicDraw plugin
 *
 * @author Xavier Seignard
 */
public class Debug {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SimplePlugin plugin = new SimplePlugin();
			Application app = Application.getInstance();
			// Start the application (visible, not silent,
			// don't try to load a project, the given args,
			// the StartUp participant)
			app.start(true, false, false, args, null);
			plugin.init();
		} catch (ApplicationExitedException e) {
			e.printStackTrace();
		}
	}
}
</pre>
<p>Then all you need to do is to right-click on this class and choose &#8220;Debug as&#8221; to launch MagicDraw within the Eclipse debugger and use all the functionalities it gives us!</p>
<p><strong>Nota :</strong> Maybe you&#8217;ll need to tune up a bit more your debug configuration by adding &#8220;-Xmx1024m&#8221; as VM argument as shown below :</p>
<p style="text-align: center;"><a href="http://mdwhatever.free.fr/wp-content/uploads/RunConfiguration.png" rel="shadowbox[post-143];player=img;" title="Run Configuration VM argument"><img class="aligncenter size-medium wp-image-154" style="border: 1px solid black;" title="Run Configuration VM argument" src="http://mdwhatever.free.fr/wp-content/uploads/RunConfiguration-300x197.png" alt="" width="300" height="197" /></a></p>
<p>Hope this helps! See you!</p>
]]></content:encoded>
			<wfw:commentRss>http://mdwhatever.free.fr/index.php/2010/07/how-to-debug-your-magicdraw-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to develop your MagicDraw plugin</title>
		<link>http://mdwhatever.free.fr/index.php/2010/03/how-to-develop-your-magicdraw-plugin/</link>
		<comments>http://mdwhatever.free.fr/index.php/2010/03/how-to-develop-your-magicdraw-plugin/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:18:38 +0000</pubDate>
		<dc:creator>xavier</dc:creator>
				<category><![CDATA[Model Driven]]></category>
		<category><![CDATA[magicdraw]]></category>
		<category><![CDATA[magicdraw open API]]></category>
		<category><![CDATA[Maven2]]></category>
		<category><![CDATA[mde]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://mdwhatever.free.fr/?p=44</guid>
		<description><![CDATA[MagicDraw is a great modeling tool¹ for lot of reasons. First of all : this is a mature tool, enterprise ready, and easy to use. Then there&#8217;s a lot of great side functionalities like : reporting templates, dependency matrix, active validation and even more²! But in a model driven environment, MagicDraw is great because of]]></description>
			<content:encoded><![CDATA[<p>MagicDraw is a great modeling tool<a href="#note1">¹</a> for lot of reasons. First of all :</p>
<ul>
<li>this is a mature tool,</li>
<li>enterprise ready,</li>
<li>and easy to use.</li>
</ul>
<p>Then there&#8217;s a lot of great side functionalities like :</p>
<ul>
<li>reporting templates,</li>
<li>dependency matrix,</li>
<li>active validation</li>
<li>and even more<a href="#note2">²</a>!</li>
</ul>
<p>But in a model driven environment, MagicDraw is great because of its <a title="open API" href="http://www.magicdraw.com/main.php?ts=navig&amp;cmd_show=1&amp;menu=feature_list#Open_API" target="_blank">open API</a> which allows you to manipulate models and diagrams really easily.</p>
<p>So it&#8217;s time to introduce a simple plugin to get familiar with this open API!<span id="more-44"></span></p>
<h1>Some resources to start</h1>
<p>First of all, you&#8217;ll find javadoc and samples in the &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;\openapi</strong>&#8221; directory and documentation in &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;\manual\MagicDraw OpenAPI UserGuide.pdf</strong>&#8220;.</p>
<p>The documentation is really good, so I think you&#8217;ll find all what you need there.</p>
<h1>How plugins are working?</h1>
<p>Each time MagicDraw starts, it scans the &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;\plugin</strong>&#8221; directory to find subdirectories containing two main things :</p>
<ul>
<li>A &#8220;<strong>plugin.xml</strong>&#8221; file which describes your plugin, the required libraries, and how to start it.</li>
<li>A jar containing a class which extends the &#8220;<strong>com.nomagic.magicdraw.plugins.Plugin</strong>&#8221; and implements its &#8220;<strong>init()</strong>&#8221; method.</li>
</ul>
<h2>Plugin.xml</h2>
<pre lang="xml" escaped="true">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!-- The id must be unique, I generally put the class that extends the Plugin class as id. --&gt;
&lt;plugin
    &lt;!-- The id must be unique, I generally put the class that extends the Plugin class as id. --&gt;
    id="fr.free.mdwhatever.simplePlugin.magicDraw.SimplePlugin"
    name="SimplePlugin"

    &lt;!-- Here I put project.version as version because Maven will filter it and put the same version you find in the pom.xml. --&gt;
    version="${project.version}" 

    &lt;!-- Who is providing this plugin (i.e. your company or whatever). --&gt;
    provider-name="Xavier Seignard"

    &lt;!-- The class which extends the com.nomagic.magicdraw.plugins.Plugin class
    in order to init your plugin within MagicDraw. --&gt;
    class="fr.free.mdwhatever.simplePlugin.magicDraw.SimplePlugin"&gt;

    &lt;!-- MagicDraw API version required by plug-in. Plug-ins and their requires versions required by plug-in. --&gt;
    &lt;requires&gt;
        &lt;api version="1.0" /&gt;
    &lt;/requires&gt;

    &lt;!-- The runtime libraries your plugin needs. --&gt;
    &lt;runtime&gt;
        &lt;library name="${artifactId}-${version}.${packaging}" /&gt;
    &lt;/runtime&gt;
&lt;/plugin&gt;</pre>
<h2>The SimplePlugin class</h2>
<p>Here is the plugin entrance, take a look at the commented code.</p>
<pre lang="java">package fr.free.mdwhatever.simplePlugin;

import com.nomagic.magicdraw.plugins.Plugin;
import com.nomagic.actions.ActionsCategory;
import com.nomagic.magicdraw.actions.ActionsConfiguratorsManager;
import com.nomagic.magicdraw.core.Application;

/**
 * This class allows you to load your plugin in MagicDraw
 *
 * @author Xavier Seignard
 */
public class SimplePlugin extends Plugin {

    // Methods
    /**
     * Plugin initialization and registration of the action in MagicDraw
     */
    public void init() {
        try {
            // We create a new actions category
            ActionsCategory category = new ActionsCategory(null, null);
            // We add our plugin action in this newly created category
            category.addAction(new SimplePluginAction());
            // We get the MagicDraw action manager
            ActionsConfiguratorsManager manager = ActionsConfiguratorsManager.getInstance();
            // We add our new configuration for our category in the MagicDraw action manager
            manager.addContainmentBrowserContextConfigurator(new BrowserContextConfigurator(category));
            // If everything is OK we log it to the MagicDraw GUI logger
            // Not really clever to put it there, but it's just to show how to log something in MagicDraw
            Application.getInstance().getGUILog().log("[Simple Plugin] Loading OK");
        }
        catch (Exception e) {
            // If something goes wrong we log it to the MagicDraw GUI logger
            Application.getInstance().getGUILog().log(
                    "[Simple Plugin] Could not instantiate plugin : " + e.toString());
        }
    }

    /**
     * The plugin is always supported, no matter the version of MagicDraw, or the perspective, or what you want.
     *
     * @return always true
     */
    public boolean isSupported() {
        return true;
    }

    /**
     * Return true always, because this plugin does not have any close specific actions.
     *
     * @return always true
     */
    public boolean close() {
        return true;
    }
}</pre>
<h1>This plugin has to do something!</h1>
<h2>How to trigger it?</h2>
<p>Some plugin can just listen to some event, or it can be triggered by a contextual menu item or a button or whatever. In the case below we add a contextual menu item. (Have a look at the &#8220;<em>com.nomagic.magicdraw.actions.ActionsConfiguratorsManager</em>&#8220;)</p>
<pre lang="java">package fr.free.mdwhatever.simplePlugin;

import com.nomagic.actions.ActionsManager;
import com.nomagic.magicdraw.actions.BrowserContextAMConfigurator;
import com.nomagic.magicdraw.ui.browser.Tree;
import com.nomagic.actions.ActionsCategory;
import com.nomagic.actions.NMAction;
import com.nomagic.magicdraw.actions.MDActionsCategory;
import com.nomagic.actions.AMConfigurator;

/**
 * This class allows you to add your plugin action (i.e. SimplePluginAction.java) in MagicDraw contextual menu.
 * @author Xavier Seignard
 */
public class BrowserContextConfigurator implements BrowserContextAMConfigurator {
    // Attributes

    // Here is the category of the contextual menu where you'll find you plugin
    public final static String SIMPLE_PLUGIN_MENU_CATEGORY = "Simple Plugin Menu Category";
    // Here is the ID of your category
    public final static String SIMPLE_PLUGIN_MENU_ID = "SimplePluginMenuID";
    // Plugin action
    private NMAction action;

    /**
     * Default constructor.
     *
     * @param nmAction the NMAction to set
     */
    public BrowserContextConfigurator(NMAction nmAction) {
        this.action = nmAction;
    }

    // Methods
    /**
     * Adds the plugin in the MagicDraw ui.
     *
     * @param mngr the action manager of MagicDraw to be configured
     * @param tree the containement tree of MagicDraw, where the plugin will be added
     */
    public void configure(ActionsManager mngr, Tree tree) {
        // We try to find the existing category
        ActionsCategory category = (ActionsCategory) mngr.getActionFor(SIMPLE_PLUGIN_MENU_ID);
        // If it doesn't exist we create it
        if (category == null) {
            category = new MDActionsCategory(SIMPLE_PLUGIN_MENU_ID, SIMPLE_PLUGIN_MENU_CATEGORY);
            category.setNested(true);
            mngr.addCategory(category);
        }
        // Then we add the action to the category
        category.addAction(action);
    }

    /**
     * Getter for priority.
     *
     * @return the priority int
     */
    public int getPriority() {
        return AMConfigurator.MEDIUM_PRIORITY;
    }
}</pre>
<h2>Set its behaviour</h2>
<p>You have to extend the corresponding action, in this case : <strong>DefaultBrowserAction</strong>.</p>
<pre lang="java">package fr.free.mdwhatever.simplePlugin;

import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import com.nomagic.magicdraw.ui.browser.actions.DefaultBrowserAction;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;

/**
 * This class defines the behavior of the action in your plugin.
 *
 * @author Xavier Seignard
 */
public class SimplePluginAction extends DefaultBrowserAction {
    // Attributes
    // Serial version UID.
    private static final long serialVersionUID = 1L;
    // The name of the action which will be displayed in MagicDraw
    private static final String SIMPLE_PLUGIN_ACTION_NAME = "Simple Plugin Action";
    // The id of the action which will be displayed in MagicDraw
    private static final String SIMPLE_PLUGIN_ACTION_ID = "SimplePluginActionID";

    /**
     * Default constructor.
     *
     * @throws Exception if the super(...) call fails
     */
    public SimplePluginAction() throws Exception {
        super(SIMPLE_PLUGIN_ACTION_ID, SIMPLE_PLUGIN_ACTION_NAME, null, null);
    }

    // Methods
    /**
     * Here you define what does your action
     *
     * @param actionEvent the triggering event
     */
    public void actionPerformed(ActionEvent actionEvent) {
        SimplePluginBehavior simplePluginBehavior = new SimplePluginBehavior();
        CountingVisitor countingVisitor = simplePluginBehavior.getVisitor();
        // Clear the map for a clean count
        countingVisitor.getMap().clear();

        // Visit the children for counting elements of each type
        simplePluginBehavior.visitChildren((Element) getSelectedObject());
        // Display the counting results
        String result = simplePluginBehavior.returnResults(countingVisitor.getMap());
        JOptionPane.showMessageDialog(null, result);
    }

    /**
     * Defines when your action is available.
     */
    public void updateState() {
        // This action is only available when your click on an instance of Element in the containment tree
        if (this.getSelectedObject() != null) {
            if (this.getSelectedObject() instanceof Element) {
                setEnabled(true);
            }
            else {
                setEnabled(false);
            }
        }
        else {
            setEnabled(false);
        }
    }
}</pre>
<h2>This visitor pattern</h2>
<p>The visitor pattern is well implemented in the Magicdraw open API, and it saves you a lot of efforts, so use it!</p>
<pre lang="java">package fr.free.mdwhatever.simplePlugin;

import java.util.HashMap;
import com.nomagic.magicdraw.uml.BaseElement;
import com.nomagic.magicdraw.uml.ClassTypes;
import com.nomagic.magicdraw.uml.InheritanceVisitor;
import com.nomagic.uml2.ext.jmi.reflect.VisitorContext;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;

/**
 * This class extends InheritanceVisitor to count element through the visitor pattern.
 * @author Xavier Seignard
 *
 */
public class CountingVisitor extends InheritanceVisitor {

    // Map for counting results. Class type is a key, number is a value.
    private HashMap countMap = new HashMap();

    /**
     * The visitation of the element.
     *
     * @param element the element to visit.
     * @param context the context of the visitor.
     */
    public void visitElement(Element element, VisitorContext context) {
        super.visitElement(element, context);
        // Call the count
        countElements(element);
    }

    /**
     * Count elements of each type.
     *
     * @param element the element to count.
     */
    public void countElements(BaseElement element) {
        // We get the human readable name of the element
        String classType = ClassTypes.getShortName(element.getClassType());
        // Then we get the count of this type of elements
        Integer count = countMap.get(classType);
        // If it is the first element of this type we visit, we need to create a new value for the counting
        if (count == null) {
            count = Integer.valueOf(0);
        }
        // Finally we increase the count
        countMap.put(classType, Integer.valueOf(count.intValue() + 1));
    }

    /**
     * Getter for countMap.
     * @return the countMap
     */
    public HashMap getMap() {
        return countMap;
    }
}</pre>
<h2>The implementation of the behavior</h2>
<p>Finally here is the implementation of the behaviour which allows you to calculate some metric like classes per package, or methods per classes :</p>
<pre lang="java">package fr.free.mdwhatever.simplePlugin;

import java.util.HashMap;
import com.nomagic.magicdraw.core.Application;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;

/**
 * The behavior of our MagicDraw plugin
 *
 * @author Xavier Seignard
 */
public class SimplePluginBehavior {  

    // Our visitor to count the type of each element.
    private CountingVisitor visitor = new CountingVisitor();

    /**
     * Returns a string containing the counting results
     *
     * @param map the HashMap containing the counting results
     * @return string describing the counting results
     */
    public String returnResults(HashMap map)
    {
        // The string to return
        String textToReturn = "";
        textToReturn += "Number of elements for each types :\n";
        for (String key : map.keySet()) {
            textToReturn += "\t"  + key + " : " + (map.get(key)).intValue() + "\n";
        }

        // Calculation for some means (the real interesting part)
        Integer packages = map.get("Package");
        Integer classes = map.get("Class");
        Integer attributes = map.get("Property");
        Integer operations = map.get("Operation");

        if (packages != null &amp;&amp; classes != null) {
            textToReturn += "Average classes per package : " + (classes.doubleValue()/packages.doubleValue()) + "\n";
        }

        if (classes!=null &amp;&amp; operations!=null) {
            textToReturn += "Average operations per class : " + (operations.doubleValue()/classes.doubleValue()) + "\n";
        }

        if (classes!=null &amp;&amp; attributes!=null)
            textToReturn += "Average attributes per class : " + (attributes.doubleValue()/classes.doubleValue()) + "\n";

        // if map was empty and no elements was found.
        if (textToReturn.length()==0)
        {
            textToReturn = "No elements found!";
        }

        return textToReturn;
    }

    /**
     * We parse all the children of the given root element.
     * For each element, we visit it to count the types of the elements
     *
     * @param rootElement the root element to start counting
     */
    public void visitChildren(Element rootElement)
    {
        try {
            rootElement.accept(visitor);
        }
        catch (Exception e) {
            // If something goes wrong we log it to the MagicDraw GUI logger
            Application.getInstance().getGUILog().log(
                    "[Simple Plugin] Exception occured : " + e.toString());
        }
        // We visit the children elements
        for (Element child : rootElement.getOwnedElement()) {
              visitChildren(child);
        }
    }

    /**
     * Getter for the visitor.
     * @return the visitor
     */
    public CountingVisitor getVisitor() {
        return visitor;
    }
}</pre>
<h1>Conclusion</h1>
<p>You&#8217;re now familiar with the MagicDraw openAPI! So lets go and develop your own plugin!</p>
<p>You&#8217;ll find a packaged version of this Eclipse/Maven2 project there : <a title="magicdraw-simple-plugin.tar.gz" href="http://mdwhatever.free.fr/wp-content/uploads/magicdraw-simple-plugin.tar.gz" target="_blank">magicdraw-simple-plugin.tar.gz</a><br />
To set up your environement, you&#8217;ll have to upload some MagicDraw libs to your maven2 local repo. You&#8217;ll find them in the &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;\lib\</strong>&#8221; directory. The list of the needed libs are described in the &#8220;<strong>dependencies</strong>&#8221; section in the &#8220;<strong>pom.xml</strong>&#8221; file.</p>
<p>You&#8217;ll also need to declare an environement variable &#8220;<strong>MDUML_HOME</strong>&#8221; which is the &#8220;<strong>&lt;MagicDraw_Install_Dir&gt;</strong>&#8220;.</p>
<p>Then if you execute a &#8220;<strong>mvn clean install</strong>&#8221; it will compile the plugin and install it on your MagicDraw installation. And you&#8217;ll just have to start MagicDraw to test the plugin.</p>
<p>In a next blog post we&#8217;ll see how to debug your plugins within Eclipse.</p>
<hr />
<p id="note1">1 : Have a look here : <a title="MagicDraw testimonials" href="http://www.magicdraw.com/main.php?ts=navig&amp;=&amp;cmd_show=1&amp;menu=testimonials" target="_blank">http://www.magicdraw.com/main.php?ts=navig&amp;=&amp;cmd_show=1&amp;menu=testimonials</a>, you&#8217;ll find my testimonial!</p>
<p id="note2">2 : Here is the features list of MagicDraw: <a title="MagicDraw features" href="http://www.magicdraw.com/main.php?ts=navig&amp;cmd_show=1&amp;menu=feature_list" target="_blank">http://www.magicdraw.com/main.php?ts=navig&amp;cmd_show=1&amp;menu=feature_list</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mdwhatever.free.fr/index.php/2010/03/how-to-develop-your-magicdraw-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Créer son plugin pour le serveur d&#8217;intégration continue TeamCity</title>
		<link>http://mdwhatever.free.fr/index.php/2010/01/creer-son-plugin-pour-le-serveur-dintegration-continue-teamcity/</link>
		<comments>http://mdwhatever.free.fr/index.php/2010/01/creer-son-plugin-pour-le-serveur-dintegration-continue-teamcity/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 11:55:46 +0000</pubDate>
		<dc:creator>xavier</dc:creator>
				<category><![CDATA[Intégration continue]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[teamCity]]></category>

		<guid isPermaLink="false">http://mdwhatever.free.fr/?p=3</guid>
		<description><![CDATA[Voici un exemple bête et méchant de plugin vous permettant d&#8217;utiliser le pattern {date} dans votre numéro de build. Rien de bien révolutionnaire, mais c&#8217;est un bon point de départ pour apprendre à créer un plugin pour TeamCity. Voici le fichier DateBuildNumber.java package com.sncf.gl.apps.teamcity.plugins.datebuilnnumber; import java.text.SimpleDateFormat; import java.util.Date; import jetbrains.buildServer.serverSide.BuildServerAdapter; import jetbrains.buildServer.serverSide.SBuildServer; import jetbrains.buildServer.serverSide.SRunningBuild; import]]></description>
			<content:encoded><![CDATA[<p>Voici un exemple bête et méchant de plugin vous permettant d&#8217;utiliser le pattern <strong>{date} </strong>dans votre numéro de build.</p>
<p>Rien de bien révolutionnaire, mais c&#8217;est un bon point de départ pour apprendre à créer un plugin pour TeamCity. <span id="more-3"></span>Voici le fichier DateBuildNumber.java</p>
<pre lang="java">
package com.sncf.gl.apps.teamcity.plugins.datebuilnnumber;

import java.text.SimpleDateFormat;
import java.util.Date;

import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.serverSide.SRunningBuild;

import com.intellij.openapi.diagnostic.Logger;

public class DateBuildNumber extends BuildServerAdapter
{
   private final static Logger LOG = Logger.getInstance(DateBuildNumber.class.getName());

   private static final String DATE = "{date}"; 

   // Constructor
   public DateBuildNumber(SBuildServer aBuildServer)
   {
       System.out.println( "### DateBuildNumber adding listener=" + this );
       LOG.info("### DateBuildNumber adding listener=" + this);

       // Register with TC
       aBuildServer.addListener(this);
   }

   public void buildStarted(SRunningBuild build)
   {
       String buildNumber = build.getBuildNumber();

       // Logging in "teamcity_install_dir\logs\stdout_XXXX.log"
       System.out.println("### DateBuildNumber plugin : buildStarted");
       LOG.info("### DateBuildNumber plugin : buildStarted");

       // If the build number contains the DATE = "{date}"
       // pattern we replace it by the current date.
       if(buildNumber.lastIndexOf(DATE) > -1)
       {
           build.setBuildNumber(buildNumber.replace(DATE, createBuildNumber()));
       }
       else
       {
           System.out.println("### DateBuildNumber plugin, no {date} pattern found");
           LOG.info("### DateBuildNumber plugin, no {date} pattern found");
       }
   }

   public String createBuildNumber()
   {
       Date currentBuild = new Date();
       // Here you can modify the date format
       // Please refer to the javaDoc :
       // http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html
       SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy-HH'h'mm");
       return sdf.format(currentBuild);
   }
}
</pre>
<p>Et voici le fichier build-server-plugin.xml :</p>
<pre lang="xml" escaped="true">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="constructor">
  <bean id="date" class="com.sncf.gl.apps.teamcity.plugins.datebuilnnumber.DateBuildNumber"/>
</beans>
</pre>
<p>Enfin voici une version packagée de ce plugin (pour TeamCity 4.xx) :  <a  href="http://www.jetbrains.net/confluence/download/attachments/9536304/datePlugin.jar?version=1">datePlugin.jar</a></p>
<p>Pour aller plus loin : <a title="http://www.jetbrains.net/confluence/display/TCD5/Developing+TeamCity+Plugins" href="http://www.jetbrains.net/confluence/display/TCD5/Developing+TeamCity+Plugins">Developing TeamCity plugins</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mdwhatever.free.fr/index.php/2010/01/creer-son-plugin-pour-le-serveur-dintegration-continue-teamcity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
