vendredi 11 janvier 2013

Création de plugin maven [2/2]

Dans l'article précédent, il a été vu comment créer un plugin maven très simple.

Il va être abordé un plugin pas plus difficile, mais avec quelques différences (nécessite un projet, paramètre obligatoire...).

Deuxième plugin maven


Dans le même projet, une classe qui liste un répertoire va être créé :
package org.test.maven.plugin;

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

/**
 * 
 * @author Emeric MARTINEAU
 * @goal listDir
 * @requiresProject true
 */
public class ListDirectory extends AbstractMojo {

 /**
  * @parameter default-value="${project}"
  * @required
  * @readonly
  */
 private MavenProject project; 
 
 /**
  * @required
  * @parameter expression="${dir}"
  */
 private String dir; 
 
 /* (non-Javadoc)
  * @see org.apache.maven.plugin.Mojo#execute()
  */
 @Override
 public void execute() throws MojoExecutionException, MojoFailureException {
  final File directory = new File(dir) ;
  
  if (!directory.exists()) {
   getLog().error(dir.concat(" doesn't exists !")) ;
   
   return ;
  }  
  
  // List file of directory
  final File[] fileList = directory.listFiles() ;
  
  File currentFile ;
  
  // For each file
  for(int index = 0; index < fileList.length; index++) {
   currentFile = fileList[index] ;
   
   if (currentFile.isDirectory()) {
    // Current file is directory
    getLog().info("<DIR> ".concat(currentFile.getName())) ;
   } else {
    getLog().info("      ".concat(currentFile.getName())) ;
   }
  }
  
  getLog().info("Basedir = " + project.getBasedir().getAbsolutePath()) ;
 }
}
Le goal est donc listDir et la classe nécessite un projet @requiresProject true.

Le paramètre dir est rendu obligatoire par la présence dans la java doc de @required.

Le paramètre project représente le projet maven (pom.xml).

Pour fonctionner, la dépendence suivante doit être ajoutée :
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-core</artifactId>
    <version>3.0.3</version>
    <scope>compile</scope>
</dependency>

Voici le résutat si lancé sans pom :
mvn org.test.maven.plugin:test-plugin-maven:listDir

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.766s
[INFO] Finished at: Thu Dec 13 11:06:23 CET 2012
[INFO] Final Memory: 4M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.test.maven.plugin:test-plugin-maven:1.0.0-SNAPSHOT:listDir (default-cli): Goal requires a project to execute but there is no POM in this directory (D:\Logiciels\Apache\apache-maven-3.0.3\bin). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
Voici le résultat si lancé avec un pom mais sans le paramètre :
mvn org.test.maven.plugin:test-plugin-maven:listDir

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building toto 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- test-plugin-maven:1.0.0-SNAPSHOT:listDir (default-cli) @ toto ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.359s
[INFO] Finished at: Thu Dec 13 11:17:21 CET 2012
[INFO] Final Memory: 4M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.test.maven.plugin:test-plugin-maven:1.0.0-SNAPSHOT:listDir (default-cli) on project toto: The parameters 'dir' for goal org.test.maven.plugin:test-plugin-maven:1.0.0-SNAPSHOT:listDir are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException
Voici le résultat lancé correctement :
mvn org.test.maven.plugin:test-plugin-maven:listDir -Ddir=/temp/

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building toto 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- test-plugin-maven:1.0.0-SNAPSHOT:listDir (default-cli) @ toto ---
[INFO]       1.jn1
[INFO]       intro.jn1
[INFO] <DIR> jill-binaries
[INFO] <DIR> jill-tiles
[INFO] <DIR> MAVEN
[INFO]       test.png
[INFO] Basedir = /temp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.578s
[INFO] Finished at: Thu Dec 13 11:17:54 CET 2012
[INFO] Final Memory: 6M/309M
[INFO] ------------------------------------------------------------------------

L'objet MavenProject


La partie de code de la classe :
/**
 * @parameter default-value="${project}"
 * @required
 * @readonly
 */
private MavenProject project;
permet de récupéré via la propriété maven ${project} le projet maven.
Il est donc possible d’accéder à toute les propriétés du projet.
L'objet MavenProject est disponible dans la librairie maven-core.

Il ne reste plus qu'à se reporter à la documentation officiele : Maven - Guide to Developing Java Plugins.

Aucun commentaire:

Enregistrer un commentaire