While this is a really old topic, the process to develop a maven plugin still need some time to sort our especially for people who are not familiar with Maven, like me. So here is a very short introduction on how to develop a maven plugin and how to integrate it with your existing application.
Prerequisite: of course, you need Maven, and all other things will totally depend on your wish. For me, I use Eclipse with m2eclipse plugin which saves me some time to create the archetype of Maven plugin project. However, this is a rather simple process and everyone can do it in command line with only a few typing.
1. Create a new Maven plugin project in Eclipse. Here, we name the project groupid: Featheast, artifactid: maven-test-plugin. Please pay attention to the naming convention of artifactid which we will use a little bit later.
2. Under the src directory, create a new Class naming MyMojo which extends AbstractMojo. AbstractMojo is the class you must inherit for plugin to work, and the only method you have to implement is execute(), where the real business will happens.
3. In order for other project to recognize your plugin function, you have to specify what the Mojo does. In Maven, this is accomplished by add an annotation @goal for the class in the comment of class. This goal name will be used later for other projects to reference this function.
4. You can create any number of variables in the class which acts like a parameter for later process. Consider the whole class as a function, then this variables will be the arguments you passed in. For each variable, another annotation @parameter will be used to specify how to like the variable to external usage.
5. Set the real business logic in the execute() function. You can use the Maven log to print out or debug for your convenience. An internal method getLog() is always there for you to do so and the usage of it is quite similar to the log4j.
/**
*
* @author yudong
*
* @goal realmojo
*/
public class MyRealMojo extends AbstractMojo{
/**
* @parameter expression="${mymojo.username}"
*/
private String username;
/**
* @parameter expression="${mymojo.password}"
*/
private String password;
public void execute() throws MojoExecutionException, MojoFailureException {
if(password.length()<10){
getLog().info("Hey" + username +", your password is too short!");
}else{
getLog().info("Congratulations " + username + ", your password is all right!");
}
Set set = getPluginContext().keySet();
getLog().info("The context include " + set.size() + " entries");
Iterator iterator = set.iterator();
while(iterator.hasNext()){
Object key = iterator.next();
getLog().info(key.toString() + " : " + getPluginContext().get(key));
}
}
}
6. In the pom.xml, add any dependency you need, then add the maven-plugin-plugin to build the plugin. Specify any goals that you want to be included in the build output.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<goalPrefix>Plugin.Test</goalPrefix>
<username>Featheast</username>
</configuration>
<goals>
<goal>
realmojo
</goal>
</goals>
</plugin>
</plugins>
</build>
7. Now your Maven plugin is created, build it with standard command: mvn install.
8. Create another project to use this plugin. Add the plugin configuration in the pom.xml, and specify the goal.
<build>
<plugins>
<plugin>
<groupId>Featheast</groupId>
<artifactId>maven-test-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>realmojo</goal>
</goals>
<configuration>
<username>This is the usernmae</username>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
9. You could specify the parameters in the configuration tag, or you can add -Dusername = XXX in the command line to pass in the parameters.
10. Finally what you did will be print on the console, once you build the new project.
Showing posts with label Plugin. Show all posts
Showing posts with label Plugin. Show all posts
Wednesday, August 4, 2010
Friday, April 9, 2010
Install spring 3.0 plug-in in Eclipse 3.5
Actually this should be a pretty old topic, since I have done the same thing more than 5 times. However, without note down all the details, each time I have to google to solve some issue. Every time I just wondered why in the first place I keep this details down, now finally I decide to write all this down in case next time I have to google again and again.
So what you need to do is simply use the install new software tool in the Help menu. The address of spring plug-in is:http://springide.org/updatesite .And actually all you need is pretty clearly written on the web site of http://springide.org/project/wiki/SpringideInstall.
However, there might be several issues you will encounter. In my situation, you will always get a complaint about the missing component of "package org.eclipse.ajdt.internal.ui.lazystart 0.0.0". This package belongs to AJDT (which stands for AspectJ Development Tools) could be downloaded through the site of http://download.eclipse.org/tools/ajdt/35/update, and relevant info is here: http://www.eclipse.org/ajdt/downloads/.
After that try the spring plugin once again, in my case no complain any more and you should be able to get dirty with Spring finally.
PS: I have tried this method both in Ubuntu 9.10 and Mac OS Snow Leopard. Haven't try on Windows so please forgive me if it fails with Microsoft.
So what you need to do is simply use the install new software tool in the Help menu. The address of spring plug-in is:http://springide.org/updatesite .And actually all you need is pretty clearly written on the web site of http://springide.org/project/wiki/SpringideInstall.
However, there might be several issues you will encounter. In my situation, you will always get a complaint about the missing component of "package org.eclipse.ajdt.internal.ui.lazystart 0.0.0". This package belongs to AJDT (which stands for AspectJ Development Tools) could be downloaded through the site of http://download.eclipse.org/tools/ajdt/35/update, and relevant info is here: http://www.eclipse.org/ajdt/downloads/.
After that try the spring plugin once again, in my case no complain any more and you should be able to get dirty with Spring finally.
PS: I have tried this method both in Ubuntu 9.10 and Mac OS Snow Leopard. Haven't try on Windows so please forgive me if it fails with Microsoft.
Subscribe to:
Posts (Atom)