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:
CheckAutoContainerPlugin, 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:
tchemit

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.nuiton.plugin.Plugin
Plugin.InitState, Plugin.Packaging
 
Field Summary
 
Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
 
Constructor Summary
AbstractPlugin()
           
 
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(File dir, String... includes)
          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(File dir, String... includes)
          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  void afterExecute()
          A call back to execute after the execute() is done
protected  void backupFile(File f)
          Backups the given file using the getBackupFile(File) as destination file.
protected  void checkEncoding()
          Method to be invoked in init phase to check sanity of PluginWithEncoding.getEncoding().
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)
          Check if an execution was already done according to the given parameters.
protected  boolean checkSkip()
          Checks if the mojo execution should be skipped.
 void copyFile(File srcFile, File destFile)
          Copy a file to a given locationand logging.
protected  void createDirectoryIfNecessary(File dir)
          Create the directory if necessary.
protected  void createNewFile(File file)
          Create a new file.
protected  void deleteFile(File file)
          Delete the given file.
protected abstract  void doAction()
          Do plugin action.
 void execute()
           
 File getBackupFile(File file)
           
 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  org.apache.maven.project.MavenProject getRootProject(org.apache.maven.project.MavenProject project, List<?> reactors)
           
protected  URL getTemplate(File f)
          Obtain the url of a file, if file does not exist, try in the classPath.
protected  boolean hasClassPath()
           
protected abstract  void 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).
protected  void renameFile(File file, File destination)
          Rename the given file to the destination one.
 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
 

Constructor Detail

AbstractPlugin

public AbstractPlugin()
Method Detail

init

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

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

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 final 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

afterExecute

protected void afterExecute()
A call back to execute after the execute() is done


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(Plugin.Packaging...)

and

rejectPackaging(Plugin.Packaging...)

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

checkSkip

protected boolean checkSkip()
Checks if the mojo execution should be skipped.

Returns:
false if the mojo should not be executed.

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 packaging 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)
Check if an execution was already done according to the given parameters.

Parameters:
runOnce - the flag
onlyForRoot - a flag to
buildStartTime - the build statrt time (if none means must do it)
newStartTime - the current build start time
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 IOException
Copy a file to a given locationand logging.

Parameters:
srcFile - represents the file to copy.
destFile - file name of destination file.
Throws:
IOException - if could not copy file.

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 - the updater used to detect changes (if none, will get all the files)

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(File dir,
                              String... includes)
Add a new resource location to the maven project (in not already present).

Parameters:
dir - the new resource location to add
includes - files to include
Since:
1.1.1

addTestResourceDir

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

Parameters:
dir - the new resource location to add
includes - files to include
Since:
1.1.1

createDirectoryIfNecessary

protected void createDirectoryIfNecessary(File dir)
                                   throws IOException
Create the directory if necessary.

Parameters:
dir - the directory to create if not already existing
Throws:
IOException - if could not create the directory
Since:
1.1.1

createNewFile

protected void createNewFile(File file)
                      throws IOException
Create a new file.

Parameters:
file - the file to create
Throws:
IOException - if could not create the file
Since:
1.1.1

deleteFile

protected void deleteFile(File file)
                   throws IOException
Delete the given file.

Parameters:
file - the file to delete
Throws:
IOException - if could not delete the file
Since:
1.1.1

renameFile

protected void renameFile(File file,
                          File destination)
                   throws IOException
Rename the given file to the destination one.

Parameters:
file - the file to rename
destination - the destination of the file
Throws:
IOException - if could not delete the file
Since:
1.2.0

getBackupFile

public File getBackupFile(File file)
Parameters:
file - the source file
Returns:
the backup file

backupFile

protected void backupFile(File f)
                   throws IOException
Backups the given file using the getBackupFile(File) as destination file.

Parameters:
f - the file to backup
Throws:
IOException - if any pb while copying the file

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 - if an url was not correct

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(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(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.

getRootProject

protected org.apache.maven.project.MavenProject getRootProject(org.apache.maven.project.MavenProject project,
                                                               List<?> reactors)

checkEncoding

protected void checkEncoding()
Method to be invoked in init phase to check sanity of PluginWithEncoding.getEncoding().

If no encoding was filled, then use the default for system (via file.encoding environement property).

Note: If mojo is not implementing PluginWithEncoding, nothing is done.

Since:
1.26

hasClassPath

protected boolean hasClassPath()
Returns:
true if project is not a pom, false otherwise.
Since:
1.2.6


Copyright © 2009-2011 CodeLutin. All Rights Reserved.