Index: src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java =================================================================== --- src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java (revision 0) +++ src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java (revision 0) @@ -0,0 +1,200 @@ +/** + * ======================================================================== + * + * Copyright 2005 Rahul Thakur. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.apache.maven.plugins.help; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Profile; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.profiles.DefaultMavenProfilesBuilder; +import org.apache.maven.profiles.DefaultProfileManager; +import org.apache.maven.profiles.ProfileManager; +import org.apache.maven.profiles.ProfilesConversionUtils; +import org.apache.maven.profiles.ProfilesRoot; +import org.apache.maven.profiles.activation.ProfileActivationException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +/** + * Displays a list of available targets under the current BARE automated + * project.

+ * Usage: mvn bare:list-targets
+ * Note that this Mojo lists only the active profiles for a project, so + * there might be a need to set profile activation switches in case it prints no + * active profiles. + *

+ * To view description of this Mojo/Goal.
+ * mvn help:describe -Dplugin=help -Dmojo=all-profiles
+ * Usage example:
+ *
mvn help:all-profiles
+ * + * @author Rahul Thakur + * @version $Id$ + * @goal all-profiles + * @description Displays a list of available targets under the current BARE + * automated project + */ +public class AllProfilesMojo + extends AbstractMojo +{ + + /** + * This is the list of projects currently slated to be built by Maven. + * + * @parameter expression="${reactorProjects}" + * @required + * @readonly + */ + private List projects; + + /** + * Flag to display all profiles - active/inactive. + * + * @parameter expression="${all}" default-value="true" + */ + private boolean all; + + /** + * The current build session instance. This is used for plugin manager API + * calls. + * + * @parameter expression="${session}" + * @required + * @readonly + */ + private MavenSession session; + + /** + * Lists details for all profiles discovered. Note that this would depend on + * the fact the {@link SetupMojo#PROFILES_XML} has been checked out. + */ + public void execute() + throws MojoExecutionException, MojoFailureException + { + List profiles = null; + for ( Iterator iter = projects.iterator(); iter.hasNext(); ) + { + MavenProject project = (MavenProject) iter.next(); + if ( getLog().isInfoEnabled() ) + getLog().info( "Listing Profiles for Project: " + project.getId() ); + + if ( all ) + { + if ( getLog().isDebugEnabled() ) + getLog().debug( "Obtaining all profiles for the project (active and inactive)" ); + // Obtain all Profiles here + DefaultProfileManager pm = new DefaultProfileManager( session.getContainer() ); + try + { + loadProjectExternalProfiles( pm, project.getBasedir() ); + profiles = pm.getActiveProfiles(); + } + catch ( ProfileActivationException e ) + { + String error = "Error obtaining external Profiles."; + if ( getLog().isErrorEnabled() ) + getLog().error( error, e ); + throw new MojoExecutionException( error, e ); + } + } + else + { + if ( getLog().isDebugEnabled() ) + getLog().debug( "Obtaining only active profiles for the project" ); + profiles = project.getActiveProfiles(); + } + + // now display + if ( null == profiles || profiles.size() == 0 ) + { + if ( getLog().isWarnEnabled() ) + getLog() + .warn( + "No profiles detected! Check if there is an activation criteria that needs be met to activate profile(s)" ); + } + else + { + for ( Iterator it = profiles.iterator(); it.hasNext(); ) + { + Profile prof = (Profile) it.next(); + getLog().info( + "\tProfile Id: " + prof.getId() + " (Active: " + + prof.getActivation().isActiveByDefault() + ", Source: " + prof.getSource() + + ")" ); + } + } + } + } + + /** + * Loads up external Profiles using profiles.xml (if any) + * located in the current project's ${basedir}. + * + * @param profileManager + * ProfileManager instance to use to load profiles from external + * Profiles. + * @param projectDir + * location of the current project. + * @throws ProfileActivationException, + * if there was an error loading profiles. + */ + private void loadProjectExternalProfiles( ProfileManager profileManager, File projectDir ) + throws ProfileActivationException + { + if ( projectDir != null ) + { + try + { + ProfilesRoot root = null; + DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder(); + root = profilesBuilder.buildProfiles( projectDir ); + if ( root != null ) + { + for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); ) + { + org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next(); + Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ); + profileManager.addProfile( converted ); + profileManager.explicitlyActivate( converted.getId() ); + } + } + else if ( getLog().isDebugEnabled() ) + getLog().debug( "ProfilesRoot was found to be NULL" ); + } + catch ( IOException e ) + { + throw new ProfileActivationException( + "Cannot read profiles.xml resource from directory: " + projectDir, + e ); + } + catch ( XmlPullParserException e ) + { + throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: " + + projectDir, e ); + } + } + } +}