org.nuiton.plugin
Class AbstractPlugin

java.lang.Object
  extended by org.apache.maven.plugin.AbstractMojo
      extended by org.nuiton.plugin.AbstractPlugin
All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo, Plugin
Direct Known Subclasses:
CollectFilesMojo, SendEmailMojo, ShareServerSecretPlugin

public abstract class AbstractPlugin
extends org.apache.maven.plugin.AbstractMojo
implements Plugin

Base mojo with usefull methods and implementing Plugin contract.

Author:
chemit

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.nuiton.plugin.Plugin
Plugin.Packaging
 
Field Summary
protected  String skipAfterInitMessage
          the message to display when will skip the goal
 
Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
 
Constructor Summary
protected AbstractPlugin()
           
protected AbstractPlugin(String skipAfterInitMessage)
           
 
Method Summary
protected  boolean acceptPackaging(Plugin.Packaging... packages)
          Accept the project's packaging between some given.
protected  void addCompileSourceRoots(File srcDir)
          Add a given directory in maven project's compile source roots (if not already present).
protected  void addDirectoryToUrlsList(File directory, List<URL> urls, Set<String> done)
          Add the given directory in urls if not already included.
protected  void addResourceDir(String dir)
          Add a new resource location to the maven project (in not already present).
protected  void addTestCompileSourceRoots(File srcDir)
          Add a given directory in maven project's test compile source roots (if not already present).
protected  void addTestResourceDir(String dir)
          Add a new test resource location to the maven project (in not already present).
protected  void addUrlToUrlsList(URL url, List<URL> urls, Set<String> done)
          Add the given url in urls if not already included.
protected  boolean checkPackaging()
          Check if the project packaging is acceptable for the mojo.
protected  void checkResource(File f)
          Check that the given resource exists in a simple fs file or in the classPath.
protected  boolean checkRunOnceDone(boolean runOnce, boolean onlyForRoot, Date buildStartTime, Date newStartTime)
           
 void copyFile(File srcFile, File destFile)
          Copy a file to a given locationand logging.
protected abstract  void doAction()
          Do plugin action.
 void execute()
           
 File getFileFromBasedir(String... paths)
          Recupere le fichier donnée à partir de son chemin relatif sur le basedir du projet maven.
protected  Map<String,String> getFilesToTreate(String[] includes, String[] excludes, File srcDir, MirroredFileUpdater updater)
          Collect to some files with a mirror.
protected  void getFilesToTreateForRoots(String[] includes, String[] excludes, List<String> roots, Map<File,String[]> files, MirroredFileUpdater updater)
          Collects some file.
protected  URL getTemplate(File f)
          Obtain the url of a file, if file does not exist, try in the classPath.
protected abstract  boolean init()
          Method to initialize the mojo before doing any concrete actions.
protected  URLClassLoader initClassLoader(org.apache.maven.project.MavenProject project, File src, boolean addSourcesToClassPath, boolean testPhase, boolean addResourcesToClassPath, boolean addCompileClassPath, boolean addProjectClassPath)
          Init mojo classLoader.
protected  boolean isExecutionRoot()
           
protected  boolean isFileNewerThanPomFile(File f)
          Test if a file exists and is newer than the pom file.
protected  boolean rejectPackaging(Plugin.Packaging... packages)
          Accept the project's packaging if not in given one.
protected  void removeCompileSourceRoots(File srcDir)
          Remove a given directory in maven project's compile source roots (if present).
protected  void removeTestCompileSourceRoots(File srcDir)
          Remove a given directory in maven project's test compile source roots (if present).
 void writeFile(File destFile, String content, String encoding)
          Write a content into the given destination file for the given encoding.
 
Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.nuiton.plugin.Plugin
getProject, isVerbose, setProject, setVerbose
 
Methods inherited from interface org.apache.maven.plugin.Mojo
getLog, setLog
 

Field Detail

skipAfterInitMessage

protected String skipAfterInitMessage
the message to display when will skip the goal

Constructor Detail

AbstractPlugin

protected AbstractPlugin()

AbstractPlugin

protected AbstractPlugin(String skipAfterInitMessage)
Method Detail

init

protected abstract boolean init()
                         throws Exception
Method to initialize the mojo before doing any concrete actions.

Note: The method is invoked before the doAction() method.

Returns:
true if mojo is well initialize and something has to be done in the doAction() method, false otherwise.
Throws:
Exception - if any

doAction

protected abstract void doAction()
                          throws Exception
Do plugin action.

The method execute() invoke this method only and only if :

Throws:
Exception - if any

execute

public void execute()
             throws org.apache.maven.plugin.MojoExecutionException,
                    org.apache.maven.plugin.MojoFailureException
Specified by:
execute in interface org.apache.maven.plugin.Mojo
Throws:
org.apache.maven.plugin.MojoExecutionException
org.apache.maven.plugin.MojoFailureException

checkPackaging

protected boolean checkPackaging()
Check if the project packaging is acceptable for the mojo.

By default, accept all packaging types.

Note: This method is the first instruction to be executed in the execute().

Tip: There is two method to simplify the packaging check : acceptPackaging(org.nuiton.plugin.Plugin.Packaging[]) and rejectPackaging(org.nuiton.plugin.Plugin.Packaging[])

Returns:
true if can execute the goal for the packaging of the project, false otherwise.

acceptPackaging

protected boolean acceptPackaging(Plugin.Packaging... packages)
Accept the project's packaging between some given.

Parameters:
packages - the accepted packaging
Returns:
true if the project's packagin is one of the given ones.

rejectPackaging

protected boolean rejectPackaging(Plugin.Packaging... packages)
Accept the project's packaging if not in given one.

Parameters:
packages - the rejecting packagings
Returns:
true if the project's packaging is not in the given ones.

isExecutionRoot

protected boolean isExecutionRoot()
Returns:
true if project is the root execution project

checkRunOnceDone

protected boolean checkRunOnceDone(boolean runOnce,
                                   boolean onlyForRoot,
                                   Date buildStartTime,
                                   Date newStartTime)
Parameters:
runOnce -
onlyForRoot -
buildStartTime -
newStartTime -
Returns:
true if the goal was already invoked

getFileFromBasedir

public File getFileFromBasedir(String... paths)
Recupere le fichier donnée à partir de son chemin relatif sur le basedir du projet maven.

Parameters:
paths - les paths pour atteindre le fichier ou le répertoire
Returns:
le fichier de la destination
Since:
1.1.0

copyFile

public void copyFile(File srcFile,
                     File destFile)
              throws org.apache.maven.plugin.MojoExecutionException
Copy a file to a given locationand logging.

Parameters:
srcFile - represents the file to copy.
destFile - file name of destination file.
Throws:
org.apache.maven.plugin.MojoExecutionException - with a message if an error occurs.

writeFile

public void writeFile(File destFile,
                      String content,
                      String encoding)
               throws IOException
Write a content into the given destination file for the given encoding.

Parameters:
destFile - location where to write the content
content - content ot write in the file
encoding - the enconding of the file
Throws:
IOException - if any pb while writing the content into the file

isFileNewerThanPomFile

protected boolean isFileNewerThanPomFile(File f)
Test if a file exists and is newer than the pom file.

Parameters:
f - the file to test
Returns:
true if file exists and is newer than the pom file, false otherwise.

getFilesToTreateForRoots

protected void getFilesToTreateForRoots(String[] includes,
                                        String[] excludes,
                                        List<String> roots,
                                        Map<File,String[]> files,
                                        MirroredFileUpdater updater)
Collects some file.

Parameters:
includes - includes
excludes - excludes
roots - root directories to treate
files - cache of file detected indexed by their root directory
updater -

getFilesToTreate

protected Map<String,String> getFilesToTreate(String[] includes,
                                              String[] excludes,
                                              File srcDir,
                                              MirroredFileUpdater updater)
Collect to some files with a mirror.

Parameters:
includes - includes
excludes - excludes
srcDir - the directory to treate
updater - an updater (will give the mirrored files)
Returns:
the map of mirrored files associated to their files in srcDir

addCompileSourceRoots

protected void addCompileSourceRoots(File srcDir)
Add a given directory in maven project's compile source roots (if not already present).

Parameters:
srcDir - the location to include in compile source roots

removeCompileSourceRoots

protected void removeCompileSourceRoots(File srcDir)
Remove a given directory in maven project's compile source roots (if present).

Parameters:
srcDir - the location to remove from compile source roots

addTestCompileSourceRoots

protected void addTestCompileSourceRoots(File srcDir)
Add a given directory in maven project's test compile source roots (if not already present).

Parameters:
srcDir - the location to include in test compile source roots

removeTestCompileSourceRoots

protected void removeTestCompileSourceRoots(File srcDir)
Remove a given directory in maven project's test compile source roots (if present).

Parameters:
srcDir - the location to remove from test compile source roots

addResourceDir

protected void addResourceDir(String dir)
Add a new resource location to the maven project (in not already present).

Parameters:
dir - the new resource location to add

addTestResourceDir

protected void addTestResourceDir(String dir)
Add a new test resource location to the maven project (in not already present).

Parameters:
dir - the new resource location to add

initClassLoader

protected URLClassLoader initClassLoader(org.apache.maven.project.MavenProject project,
                                         File src,
                                         boolean addSourcesToClassPath,
                                         boolean testPhase,
                                         boolean addResourcesToClassPath,
                                         boolean addCompileClassPath,
                                         boolean addProjectClassPath)
                                  throws MalformedURLException
Init mojo classLoader.

Parameters:
project - the maven project
src - the source directory
addSourcesToClassPath - a flag to a maven sources to classLoader
testPhase - a flag to specify if we are in a test phase (changes the classLoader)
addResourcesToClassPath - flag to add maven's resources to classLoader
addCompileClassPath - flag to add maven's project compile classPath to classLoader
addProjectClassPath - flag to add maven'es project dependecies to classLoader
Returns:
the new classLoader
Throws:
MalformedURLException

addDirectoryToUrlsList

protected void addDirectoryToUrlsList(File directory,
                                      List<URL> urls,
                                      Set<String> done)
                               throws MalformedURLException
Add the given directory in urls if not already included. Note: We use a extra list to store file string representation, since we do NOT want any url resolution and the URL.equals(java.lang.Object) is doing some...

Parameters:
directory - the directory to insert in urls
urls - list of urls
done - list of string representation of urls
Throws:
MalformedURLException - if pb while converting file to url
Since:
1.1.0

addUrlToUrlsList

protected void addUrlToUrlsList(URL url,
                                List<URL> urls,
                                Set<String> done)
Add the given url in urls if not already included. Note: We use a extra list to store file string representation, since we do NOT want any url resolution and the URL.equals(java.lang.Object) is doing some..

Parameters:
url - the url to insert in urls
urls - list of urls
done - list of string representation of urls
Since:
1.1.0

getTemplate

protected URL getTemplate(File f)
                   throws IOException
Obtain the url of a file, if file does not exist, try in the classPath.

Parameters:
f - the required resource file.
Returns:
the url of the resource
Throws:
IOException - for any error while looking up for the url of the resources

checkResource

protected void checkResource(File f)
                      throws IOException
Check that the given resource exists in a simple fs file or in the classPath.

Parameters:
f - the required resource file.
Throws:
IOException - for any error while looking up for the resources content.


Copyright © 2009 CodeLutin. All Rights Reserved.