You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Dennis Lundberg <de...@apache.org> on 2011/08/08 22:30:01 UTC

How do I get the configuration for a plugin in a MavenProject? Was: Re: svn commit: r1154621 - in /maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site: AbstractDeployMojo.java SiteStageDeployMojo.java

Hi

So, this is the commit that made Jenkins unhappy building plugins with
Maven 2.

I've found the problem now. In the code below I do something like this
to get the plugins for a project:

        Map<String,Plugin> map = project.getBuild().getPluginsAsMap();

This is nice, because in Maven 3 I get the complete effective set of
plugins, from both build/plugins and build/pluginManagement.

This doesn't work in Maven 2 :-(

What I have found is that I can get all plugins from build/plugins *or*
all plugins from build/pluginManagement, but not both at the same time.
I could combine the result of the two, but what happens if a plugin
exits in both? How would I merge the configuration of the two?

Is there a cleaner way to do this?

All I want is the (complete) configuration for maven-site-plugin in a
given MavenProject. Any pointer on how to do this would be most appreciated.




On 2011-08-07 01:21, dennisl@apache.org wrote:
> Author: dennisl
> Date: Sat Aug  6 23:21:05 2011
> New Revision: 1154621
> 
> URL: http://svn.apache.org/viewvc?rev=1154621&view=rev
> Log:
> [MSITE-602] The staged site is deployed to the wrong place
> 
> o Attempt at fixing this. All current ITs pass and manual testing shows that it works for the use case described in the issue. Could use review from others.
> 
> Modified:
>     maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/AbstractDeployMojo.java
>     maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java
> 
> Modified: maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/AbstractDeployMojo.java
> URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/AbstractDeployMojo.java?rev=1154621&r1=1154620&r2=1154621&view=diff
> ==============================================================================
> --- maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/AbstractDeployMojo.java (original)
> +++ maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/AbstractDeployMojo.java Sat Aug  6 23:21:05 2011
> @@ -217,7 +217,7 @@ public abstract class AbstractDeployMojo
>       *
>       * @throws MojoExecutionException
>       */
> -    private String getDeployModuleDirectory()
> +    protected String getDeployModuleDirectory()
>          throws MojoExecutionException
>      {
>          String relative = siteTool.getRelativePath( getSite( project ).getUrl(),
> @@ -785,7 +785,7 @@ public abstract class AbstractDeployMojo
>      }
>  
>      /**
> -     * Extract the distributionManagment site of the top level parent of the given MavenProject.
> +     * Extract the distributionManagement site of the top level parent of the given MavenProject.
>       * This climbs up the project hierarchy and returns the site of the last project
>       * for which {@link #getSite(org.apache.maven.project.MavenProject)} returns a site.
>       *
> 
> Modified: maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java
> URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java?rev=1154621&r1=1154620&r2=1154621&view=diff
> ==============================================================================
> --- maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java (original)
> +++ maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java Sat Aug  6 23:21:05 2011
> @@ -20,7 +20,13 @@ package org.apache.maven.plugins.site;
>   */
>  
>  
> +import org.apache.commons.lang.StringUtils;
> +import org.apache.maven.model.Build;
> +import org.apache.maven.model.Plugin;
> +import org.apache.maven.model.Site;
>  import org.apache.maven.plugin.MojoExecutionException;
> +import org.apache.maven.project.MavenProject;
> +import org.codehaus.plexus.util.xml.Xpp3Dom;
>  
>  /**
>   * Deploys the generated site to a staging or mock directory to the site URL
> @@ -71,6 +77,40 @@ public class SiteStageDeployMojo
>      private String stagingRepositoryId;
>  
>      @Override
> +    /**
> +     * Find the relative path between the distribution URLs of the parent that
> +     * supplied the staging deploy URL and the current project.
> +     *
> +     * @return the relative path or "./" if the two URLs are the same.
> +     *
> +     * @throws MojoExecutionException
> +     */
> +    protected String getDeployModuleDirectory()
> +        throws MojoExecutionException
> +    {
> +        // MSITE-602: If the user specified an explicit stagingSiteURL, use a special relative path
> +        if( StringUtils.isNotEmpty( stagingSiteURL ) )
> +        {
> +            // We need to calculate the relative path between this project and
> +            // the first one that supplied a stagingSiteURL
> +            String relative = siteTool.getRelativePath( getSite( project ).getUrl(),
> +                getSiteForFirstParentWithStagingSiteURL( project ).getUrl() );
> +
> +            // SiteTool.getRelativePath() uses File.separatorChar,
> +            // so we need to convert '\' to '/' in order for the URL to be valid for Windows users
> +            relative = relative.replace( '\\', '/' );
> +
> +            getLog().debug( "The stagingSiteURL is configured, using special way to calculate relative path." );
> +            return ( "".equals( relative ) ) ? "./" : relative;
> +        }
> +        else
> +        {
> +            getLog().debug( "No stagingSiteURL is configured, using standard way to calculate relative path." );
> +            return super.getDeployModuleDirectory();
> +        }
> +    }
> +
> +    @Override
>      protected String getDeployRepositoryID()
>          throws MojoExecutionException
>      {
> @@ -93,6 +133,83 @@ public class SiteStageDeployMojo
>      }
>  
>      /**
> +     * Extract the distributionManagement.site of the first project up the
> +     * hierarchy that specifies a stagingSiteURL, starting at the given
> +     * MavenProject.
> +     * <p/>
> +     * This climbs up the project hierarchy and returns the site of the first
> +     * project for which
> +     * {@link #getStagingSiteURL(org.apache.maven.project.MavenProject)} returns
> +     * a URL.
> +     *
> +     * @param project the MavenProject. Not null.
> +     * @return the site for the first project that has a stagingSiteURL. Not null.
> +     */
> +    protected Site getSiteForFirstParentWithStagingSiteURL( MavenProject project )
> +    {
> +        Site site = project.getDistributionManagement().getSite();
> +
> +        MavenProject parent = project;
> +
> +        // @todo Should we check that the stagingSiteURL equals the one in this project instead of being non-empty?
> +        while ( parent != null
> +                && StringUtils.isNotEmpty( getStagingSiteURL( parent ) ) )
> +        {
> +            site = parent.getDistributionManagement().getSite();
> +
> +            // MSITE-585, MNG-1943
> +            parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
> +        }
> +
> +        return site;
> +    }
> +
> +    /**
> +     * Extract the value of the stagingSiteURL configuration parameter of
> +     * maven-site-plugin for the given project.
> +     *
> +     * @param project The MavenProject, not null
> +     * @return The stagingSiteURL for the project, or null if it doesn't have one
> +     */
> +    private String getStagingSiteURL( MavenProject project )
> +    {
> +        final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
> +
> +        if ( project == null )
> +        {
> +            return null;
> +        }
> +
> +        final Build build = project.getBuild();
> +        if ( build == null )
> +        {
> +            return null;
> +        }
> +
> +        final Plugin sitePlugin = build.getPluginsAsMap().get( sitePluginKey );
> +        if( sitePlugin == null )
> +        {
> +            return null;
> +        }
> +
> +        final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
> +        if ( sitePluginConfiguration == null )
> +        {
> +            return null;
> +        }
> +
> +        final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
> +        if ( child == null )
> +        {
> +            return null;
> +        }
> +        else
> +        {
> +            return child.getValue();
> +        }
> +    }
> +
> +    /**
>       * Find the URL where staging will take place.
>       *
>       * @param usersStagingSiteURL The staging site URL as suggested by the user's configuration
> 
> 
> 


-- 
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I get the configuration for a plugin in a MavenProject? Was: Re: svn commit: r1154621 - in /maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site: AbstractDeployMojo.java SiteStageDeployMojo.java

Posted by Dennis Lundberg <de...@apache.org>.
Thanks Benjamin,

This was exactly the information I was looking for.

On 2011-08-08 23:22, Benjamin Bentmann wrote:
> Dennis Lundberg wrote:
> 
>>          Map<String,Plugin>  map = project.getBuild().getPluginsAsMap();
>>
>> This is nice, because in Maven 3 I get the complete effective set of
>> plugins, from both build/plugins and build/pluginManagement.
> 
> I hope this means "get the set of build/plugins as seen in
> help:effective-pom, including plugins contributed by lifecycle mappings,
> with their effective configuration, i.e. after merging in inheritance,
> profiles and build/pluginManagement". This map should not include
> plugins from build/pluginManagement unless that plugin is also declared
> in the build/plugins section or in a lifecycle mapping.
> 
>> This doesn't work in Maven 2
> 
> I assume you refer to the fact that plugins from lifecycle mappings do
> not show up among build/plugins.
> 
>> What I have found is that I can get all plugins from build/plugins *or*
>> all plugins from build/pluginManagement, but not both at the same time.
>> I could combine the result of the two, but what happens if a plugin
>> exits in both? How would I merge the configuration of the two?
> 
> Plugin sitePlugin = (get from build/plugins)
> if (sitePlugin == null) {
>    sitePlugin = (get from build/pluginManagement/plugins)
> }
> // continue config extraction from sitePlugin
> 
> If the plugin is declared in both build/plugins and
> build/pluginManagement, both Maven 2.x and 3.x will already have handled
> the configuration merging such that build/plugins contains effective
> configuration.
> 
> 
> Benjamin
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
> 
> 


-- 
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I get the configuration for a plugin in a MavenProject? Was: Re: svn commit: r1154621 - in /maven/plugins/trunk/maven-site-plugin/src/main/java/org/apache/maven/plugins/site: AbstractDeployMojo.java SiteStageDeployMojo.java

Posted by Benjamin Bentmann <be...@udo.edu>.
Dennis Lundberg wrote:

>          Map<String,Plugin>  map = project.getBuild().getPluginsAsMap();
>
> This is nice, because in Maven 3 I get the complete effective set of
> plugins, from both build/plugins and build/pluginManagement.

I hope this means "get the set of build/plugins as seen in 
help:effective-pom, including plugins contributed by lifecycle mappings, 
with their effective configuration, i.e. after merging in inheritance, 
profiles and build/pluginManagement". This map should not include 
plugins from build/pluginManagement unless that plugin is also declared 
in the build/plugins section or in a lifecycle mapping.

> This doesn't work in Maven 2

I assume you refer to the fact that plugins from lifecycle mappings do 
not show up among build/plugins.

> What I have found is that I can get all plugins from build/plugins *or*
> all plugins from build/pluginManagement, but not both at the same time.
> I could combine the result of the two, but what happens if a plugin
> exits in both? How would I merge the configuration of the two?

Plugin sitePlugin = (get from build/plugins)
if (sitePlugin == null) {
    sitePlugin = (get from build/pluginManagement/plugins)
}
// continue config extraction from sitePlugin

If the plugin is declared in both build/plugins and 
build/pluginManagement, both Maven 2.x and 3.x will already have handled 
the configuration merging such that build/plugins contains effective 
configuration.


Benjamin

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org