AbstractInvokerMojo.java

  1. package com.soebes.maven.plugins.iterator;

  2. /*
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  */

  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Properties;

  26. import org.apache.maven.plugins.annotations.Parameter;
  27. import org.apache.maven.project.MavenProject;
  28. import org.apache.maven.shared.invoker.DefaultInvocationRequest;
  29. import org.apache.maven.shared.invoker.InvocationRequest;

  30. /**
  31.  * @author Karl-Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
  32.  */
  33. public abstract class AbstractInvokerMojo
  34.     extends AbstractIteratorMojo
  35. {

  36.     /**
  37.      * The list of goals which will be called during each invocation of Maven. TODO: Change the name, cause we are
  38.      * calling life cycle phases instead of goal but we can if we like things like site:site ?
  39.      */
  40.     @Parameter
  41.     private List<String> goals = Collections.singletonList( "clean" );

  42.     /**
  43.      * This will set <code>--batch-mode</code>
  44.      */
  45.     @Parameter( defaultValue = "false" )
  46.     private boolean interactive;

  47.     /**
  48.      * This will <code>--also-make</code>
  49.      */
  50.     @Parameter( defaultValue = "false" )
  51.     private boolean alsoMake;

  52.     /**
  53.      * This will activate <code>--also-make-dependents</code>
  54.      */
  55.     @Parameter( defaultValue = "false" )
  56.     private boolean alsoMakeDependents;

  57.     /**
  58.      * Sets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return
  59.      * <code>null</code>, this setting only affects the working directory for the Maven invocation. Here you can use the
  60.      * placeholder like: EXAMPLE
  61.      */
  62.     @Parameter
  63.     private File baseDirectory;

  64.     /**
  65.      * <code>-Ddebug=true</code>
  66.      */
  67.     @Parameter( defaultValue = "false" )
  68.     private boolean debug;

  69.     /**
  70.      * The valid values are {@link InvocationRequest#REACTOR_FAIL_AT_END}, {@link InvocationRequest#REACTOR_FAIL_FAST},
  71.      * {@link InvocationRequest#REACTOR_FAIL_NEVER}.
  72.      *
  73.      * @see {@link InvocationRequest#REACTOR_FAIL_AT_END}, {@link InvocationRequest#REACTOR_FAIL_FAST},
  74.      *      {@link InvocationRequest#REACTOR_FAIL_NEVER}
  75.      * @FIXME: Check if we need to create a setter which checks the valid values
  76.      */
  77.     @Parameter( defaultValue = InvocationRequest.REACTOR_FAIL_FAST )
  78.     private String failureBehaviour;

  79.     /**
  80.      * Sets the checksum mode of the Maven invocation. Equivalent of {@code -c} or {@code --lax-checksums}, {@code -C}
  81.      * or {@code --strict-checksums}
  82.      *
  83.      * @param globalChecksumPolicy The checksum mode, must be one of {@link InvocationRequest#CHECKSUM_POLICY_WARN} and
  84.      *            {@link InvocationRequest#CHECKSUM_POLICY_FAIL}.
  85.      */
  86.     @Parameter( defaultValue = InvocationRequest.CHECKSUM_POLICY_WARN )
  87.     private String globalChecksumPolicy;

  88.     /**
  89.      * The path to the global settings for the Maven invocation or <code>null</code> to load the global settings from
  90.      * the default location.
  91.      */
  92.     @Parameter
  93.     private File globalSettingsFile;

  94.     /**
  95.      * If <code>null</code> the default location from <code>settings.xml</code> will be used.
  96.      */
  97.     @Parameter
  98.     private File localRepositoryDirectory;

  99.     /**
  100.      * If <code>null</code>.
  101.      */
  102.     @Parameter
  103.     private String mavenOpts;

  104.     /**
  105.      * If true <code>--no-plugin-updates</code> will be used.
  106.      */
  107.     @Parameter
  108.     private boolean nonPluginUpdates;

  109.     /**
  110.      * <code>true</code> Maven will be executed in off-line mode ( <code>--offline</code>).
  111.      */
  112.     @Parameter
  113.     private boolean offline;

  114.     /**
  115.      * The list of profiles.
  116.      */
  117.     @Parameter
  118.     private List<String> profiles;

  119.     /**
  120.      * Sets the reactor project list. Equivalent of {@code -pl} or {@code --projects} projects the reactor project list
  121.      */
  122.     @Parameter
  123.     private List<String> projects;

  124.     /**
  125.      * Sets the system properties for the Maven invocation, may be <code>null</code> if not set.
  126.      */
  127.     @Parameter
  128.     private Properties properties;

  129.     /**
  130.      * Sets the recursion behavior of a reactor invocation. <em>Inverse</em> equivalent of {@code -N} and
  131.      * {@code --non-recursive}
  132.      */
  133.     @Parameter( defaultValue = "false" )
  134.     private boolean recursive;

  135.     /**
  136.      * Specify the reactor project to resume from.
  137.      */
  138.     @Parameter
  139.     private String resumeFrom;

  140.     /**
  141.      * Indicates whether the environment variables of the current process should be propagated to the Maven invocation.
  142.      * By default, the current environment variables are inherited by the new Maven invocation.
  143.      */
  144.     @Parameter( defaultValue = "true" )
  145.     private boolean shellEnvironmentInherited;

  146.     /**
  147.      * Gets the exception output mode of the Maven invocation. By default, Maven will not print stack traces of build
  148.      * exceptions.
  149.      */
  150.     @Parameter( defaultValue = "false" )
  151.     private boolean showErrors;

  152.     /**
  153.      * The show version behaviour {@code -V} option.
  154.      */
  155.     @Parameter( defaultValue = "false" )
  156.     private boolean showVersion;

  157.     /**
  158.      * {@code -T} option of Maven.
  159.      */
  160.     @Parameter
  161.     protected String threads;

  162.     /**
  163.      * Set the path to the custom toolchains file
  164.      */
  165.     @Parameter
  166.     private File toolchains;

  167.     /**
  168.      * Indicates whether Maven should enforce an update check for plugins and snapshots. By default, no update check is
  169.      * performed {@code --update-snapshots}.
  170.      */
  171.     @Parameter( defaultValue = "false" )
  172.     private boolean updateSnapshots;

  173.     /**
  174.      * Sets the path to the user settings for the Maven invocation.
  175.      */
  176.     @Parameter
  177.     private File userSettings;
  178.        
  179.     private File getBaseDirectoryAfterPlaceHolderIsReplaced( String currentValue )
  180.     {
  181.         File baseDir = getBaseDirectory();
  182.         if ( baseDir != null && baseDir.toString().contains( getPlaceHolder() ) )
  183.         {
  184.             baseDir = new File( baseDir.toString().replaceAll( getPlaceHolder(), currentValue ) );
  185.         }
  186.         return baseDir;
  187.     }

  188.     private List<String> replacePlaceholderInElements( String currentValue, List<String> goals )
  189.     {
  190.         List<String> result = new ArrayList<String>();
  191.         for ( String string : goals )
  192.         {
  193.             if ( string.contains( getPlaceHolder() ) )
  194.             {
  195.                 result.add( string.replaceAll( getPlaceHolder(), currentValue ) );
  196.             }
  197.             else
  198.             {
  199.                 result.add( string );
  200.             }
  201.         }
  202.         return result;
  203.     }

  204.     private List<String> getGoalsAfterPlaceHolderIsReplaced( String currentValue )
  205.     {
  206.         List<String> goals = getGoals();
  207.         if ( goals == null )
  208.         {
  209.             return null;
  210.         }

  211.         List<String> result = replacePlaceholderInElements( currentValue, goals );
  212.         return result;
  213.     }

  214.     private List<String> getProfilesAfterPlaceHolderIsReplaced( String currentValue )
  215.     {
  216.         List<String> profiles = getProfiles();
  217.         if ( profiles == null )
  218.         {
  219.             return null;
  220.         }

  221.         List<String> result = replacePlaceholderInElements( currentValue, profiles );
  222.         return result;
  223.     }

  224.     private List<String> getProjectsAfterPlaceHolderIsReplaced( String currentValue )
  225.     {
  226.         List<String> projects = getProjects();
  227.         if ( projects == null )
  228.         {
  229.             return null;
  230.         }

  231.         List<String> result = replacePlaceholderInElements( currentValue, projects );
  232.         return result;
  233.     }

  234.     protected InvocationRequest createAndConfigureAnInvocationRequest( ItemWithProperties currentValue )
  235.     {
  236.         InvocationRequest request = new DefaultInvocationRequest();

  237.         request.setAlsoMake( isAlsoMake() );
  238.         request.setAlsoMakeDependents( isAlsoMakeDependents() );
  239.         request.setDebug( isDebug() );
  240.         request.setFailureBehavior( getFailureBehaviour() );
  241.         request.setGlobalChecksumPolicy( getGlobalChecksumPolicy() );
  242.         request.setGlobalSettingsFile( getGlobalSettingsFile() );
  243.         request.setInteractive( isInteractive() );

  244.         request.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
  245.         request.setMavenOpts( getMavenOpts() );
  246.         request.setNonPluginUpdates( isNonPluginUpdates() );
  247.         request.setOffline( isOffline() );

  248. //        request.setProperties( properties )
  249. //        ;
  250.         // @TODO: Think about it.
  251.         // request.setPomFile(pomFile);
  252.         // @TODO: Think about it.
  253.         // request.setPomFileName(pomFilename);

  254.         // The following parameter do make sense to use a placeholder
  255.         // base directory
  256.         // cd @item@
  257.         // mvn clean package
  258.         request.setBaseDirectory( getBaseDirectoryAfterPlaceHolderIsReplaced( currentValue.getName() ) );
  259.         // goals:
  260.         // mvn plugin-name:@item@
  261.         //
  262.         request.setGoals( getGoalsAfterPlaceHolderIsReplaced( currentValue.getName() ) );
  263.         // Profiles:
  264.         // mvn -Pxyz-@item@ clean package
  265.         // mvn -P@item@
  266.         request.setProfiles( getProfilesAfterPlaceHolderIsReplaced( currentValue.getName() ) );
  267.         // Projects:
  268.         // mvn -pl xyz-@item@ clean package
  269.         request.setProjects( getProjectsAfterPlaceHolderIsReplaced( currentValue.getName() ) );

  270.         Properties props = getMergedProperties(currentValue );
  271.         request.setProperties( props );

  272.         request.setRecursive( isRecursive() );
  273.         request.setResumeFrom( getResumeFrom() );
  274.         request.setShellEnvironmentInherited( isShellEnvironmentInherited() );
  275.         request.setShowErrors( isShowErrors() );
  276.         request.setShowVersion( isShowVersion() );
  277.         request.setThreads( getThreads() );
  278.         request.setToolchainsFile( getToolchains() );
  279.         request.setUpdateSnapshots( isUpdateSnapshots() );
  280.         request.setUserSettingsFile( getUserSettings() );
  281.        
  282.         return request;
  283.     }

  284.     private Properties getMergedProperties( ItemWithProperties item )
  285.     {
  286.         Properties props = new Properties();
  287.         if ( getMavenProject().getProperties() != null )
  288.         {
  289.             props.putAll( getMavenProject().getProperties() );
  290.         }
  291.        
  292.         if ( getProperties() != null )
  293.         {
  294.             props.putAll( getProperties() );
  295.         }
  296.        
  297.         if ( item.getProperties() != null )
  298.         {
  299.             props.putAll( item.getProperties() );
  300.         }
  301.        
  302.         for ( Map.Entry<Object, Object> property : props.entrySet() )
  303.         {
  304.             String key = (String) property.getKey();
  305.             String systemPropertyValue = System.getProperty( key );

  306.             if ( systemPropertyValue != null )
  307.             {
  308.                 props.put( key, systemPropertyValue );
  309.             }
  310.         }
  311.        
  312.         return props;
  313.     }

  314.     public List<String> getGoals()
  315.     {
  316.         return goals;
  317.     }

  318.     public boolean isInteractive()
  319.     {
  320.         return interactive;
  321.     }

  322.     public void setInteractive( boolean interactive )
  323.     {
  324.         this.interactive = interactive;
  325.     }

  326.     public boolean isAlsoMake()
  327.     {
  328.         return alsoMake;
  329.     }

  330.     public void setAlsoMake( boolean alsoMake )
  331.     {
  332.         this.alsoMake = alsoMake;
  333.     }

  334.     public boolean isAlsoMakeDependents()
  335.     {
  336.         return alsoMakeDependents;
  337.     }

  338.     public void setAlsoMakeDependents( boolean alsoMakeDependents )
  339.     {
  340.         this.alsoMakeDependents = alsoMakeDependents;
  341.     }

  342.     public File getBaseDirectory()
  343.     {
  344.         return baseDirectory;
  345.     }

  346.     public void setBaseDirectory( File baseDirectory )
  347.     {
  348.         this.baseDirectory = baseDirectory;
  349.     }

  350.     public boolean isDebug()
  351.     {
  352.         return debug;
  353.     }

  354.     public void setDebug( boolean debug )
  355.     {
  356.         this.debug = debug;
  357.     }

  358.     public String getFailureBehaviour()
  359.     {
  360.         return failureBehaviour;
  361.     }

  362.     public void setFailureBehaviour( String failureBehaviour )
  363.     {
  364.         this.failureBehaviour = failureBehaviour;
  365.     }

  366.     public String getGlobalChecksumPolicy()
  367.     {
  368.         return globalChecksumPolicy;
  369.     }

  370.     public void setGlobalChecksumPolicy( String globalChecksumPolicy )
  371.     {
  372.         this.globalChecksumPolicy = globalChecksumPolicy;
  373.     }

  374.     public File getGlobalSettingsFile()
  375.     {
  376.         return globalSettingsFile;
  377.     }

  378.     public void setGlobalSettingsFile( File globalSettingsFile )
  379.     {
  380.         this.globalSettingsFile = globalSettingsFile;
  381.     }

  382.     public File getLocalRepositoryDirectory()
  383.     {
  384.         return localRepositoryDirectory;
  385.     }

  386.     public void setLocalRepositoryDirectory( File localRepositoryDirectory )
  387.     {
  388.         this.localRepositoryDirectory = localRepositoryDirectory;
  389.     }

  390.     public String getMavenOpts()
  391.     {
  392.         return mavenOpts;
  393.     }

  394.     public void setMavenOpts( String mavenOpts )
  395.     {
  396.         this.mavenOpts = mavenOpts;
  397.     }

  398.     public boolean isNonPluginUpdates()
  399.     {
  400.         return nonPluginUpdates;
  401.     }

  402.     public void setNonPluginUpdates( boolean nonPluginUpdates )
  403.     {
  404.         this.nonPluginUpdates = nonPluginUpdates;
  405.     }

  406.     public boolean isOffline()
  407.     {
  408.         return offline;
  409.     }

  410.     public void setOffline( boolean offline )
  411.     {
  412.         this.offline = offline;
  413.     }

  414.     public List<String> getProfiles()
  415.     {
  416.         return profiles;
  417.     }

  418.     public void setProfiles( List<String> profiles )
  419.     {
  420.         this.profiles = profiles;
  421.     }

  422.     public List<String> getProjects()
  423.     {
  424.         return projects;
  425.     }

  426.     public void setProjects( List<String> projects )
  427.     {
  428.         this.projects = projects;
  429.     }

  430.     public Properties getProperties()
  431.     {
  432.         return properties;
  433.     }

  434.     public void setProperties( Properties properties )
  435.     {
  436.         this.properties = properties;
  437.     }

  438.     public boolean isRecursive()
  439.     {
  440.         return recursive;
  441.     }

  442.     public void setRecursive( boolean recursive )
  443.     {
  444.         this.recursive = recursive;
  445.     }

  446.     public String getResumeFrom()
  447.     {
  448.         return resumeFrom;
  449.     }

  450.     public void setResumeFrom( String resumeFrom )
  451.     {
  452.         this.resumeFrom = resumeFrom;
  453.     }

  454.     public boolean isShellEnvironmentInherited()
  455.     {
  456.         return shellEnvironmentInherited;
  457.     }

  458.     public void setShellEnvironmentInherited( boolean shellEnvironmentInherited )
  459.     {
  460.         this.shellEnvironmentInherited = shellEnvironmentInherited;
  461.     }

  462.     public boolean isShowErrors()
  463.     {
  464.         return showErrors;
  465.     }

  466.     public void setShowErrors( boolean showErrors )
  467.     {
  468.         this.showErrors = showErrors;
  469.     }

  470.     public boolean isShowVersion()
  471.     {
  472.         return showVersion;
  473.     }

  474.     public void setShowVersion( boolean showVersion )
  475.     {
  476.         this.showVersion = showVersion;
  477.     }

  478.     public String getThreads()
  479.     {
  480.         return threads;
  481.     }

  482.     public File getToolchains()
  483.     {
  484.         return toolchains;
  485.     }

  486.     public void setToolchains( File toolchains )
  487.     {
  488.         this.toolchains = toolchains;
  489.     }

  490.     public boolean isUpdateSnapshots()
  491.     {
  492.         return updateSnapshots;
  493.     }

  494.     public void setUpdateSnapshots( boolean updateSnapshots )
  495.     {
  496.         this.updateSnapshots = updateSnapshots;
  497.     }

  498.     public File getUserSettings()
  499.     {
  500.         return userSettings;
  501.     }

  502.     public void setUserSettings( File userSettings )
  503.     {
  504.         this.userSettings = userSettings;
  505.     }

  506.     public void setGoals( List<String> goals )
  507.     {
  508.         this.goals = goals;
  509.     }

  510. }