Java tutorial
/* * Copyright 2012 htfv (Aliaksei Lahachou) * * 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 com.github.htfv.maven.plugins.buildconfigurator.extension; import org.apache.maven.AbstractMavenLifecycleParticipant; import org.apache.maven.MavenExecutionException; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import org.codehaus.plexus.component.configurator.ComponentConfigurator; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; import org.codehaus.plexus.util.xml.Xpp3Dom; import com.github.htfv.maven.plugins.buildconfigurator.core.BuildConfigurator; import com.github.htfv.maven.plugins.buildconfigurator.core.ConfigureRequest; import com.github.htfv.maven.plugins.buildconfigurator.core.configuration.BuildConfiguratorExtensionConfiguration; /** * Configures loaded projects according to the configuration of the * {@code build-configuration-maven-plugin}. * <ul> * <li>Updates project properties from property files listed in the * {@code <propertyFiles>} element.</li> * <li>Updates project dependencies and exclusions using parameters defined in * the {@code <dependencies>} element.</li> * <li>Updates project plugins and executions using parameters defined in the * {@code <build>/<plugins>} element.</li> * </ul> * * @author htfv (Aliaksei Lahachou) */ @Component(hint = "buildConfigurator", role = AbstractMavenLifecycleParticipant.class) public class BuildConfiguratorExtension extends AbstractMavenLifecycleParticipant { /** * {@code groupId} of the Build Configurator Maven Extension. */ private static final String BUILD_CONFIGURATOR_GROUP_ID = "com.github.htfv.maven.plugins"; /** * {@code artifactId} of the Build Configurator Maven Extension. */ private static final String BUILD_CONFIGURATOR_ARTIFACT_ID = "build-configurator-maven-extension"; /** * Reference to the {@link BuildConfigurator} used to configure projects. */ @Requirement private BuildConfigurator buildConfigurator; /** * Component configurator used to convert XML to model. */ @Requirement(hint = "basic", role = ComponentConfigurator.class) private ComponentConfigurator configurator; /** * Plexus container required to obtain container realm, which is required * for component configurator. */ @Requirement private PlexusContainer container; /** * Configures loaded projects. * * @param session * session containing loaded projects. * * @throws MavenExecutionException * if projects could not be counfigured. */ @Override public void afterProjectsRead(final MavenSession session) throws MavenExecutionException { try { for (final MavenProject project : session.getProjects()) { // // Check to see if Build Configurator is configured for this project. // final Plugin plugin = getPlugin(project); if (plugin == null) { continue; } // // Configure the project. // final ConfigureRequest request = new ConfigureRequest.Builder() .extensionConfiguration(getModel(plugin)).project(project).build(); buildConfigurator.configure(request); } } catch (final Exception e) { throw new MavenExecutionException(e.getMessage(), e); } } /** * Created model from XML configuration. * * @param plugin * plugin, whose configuration shall be converted to model. * * @return Build Configurator model. * * @throws ComponentConfigurationException * if XML configuration could not be converted to model. */ private BuildConfiguratorExtensionConfiguration getModel(final Plugin plugin) throws ComponentConfigurationException { final BuildConfiguratorExtensionConfiguration model = new BuildConfiguratorExtensionConfiguration(); configurator.configureComponent(model, new XmlPlexusConfiguration((Xpp3Dom) plugin.getConfiguration()), container.getContainerRealm()); return model; } /** * Looks for Build Configurator plugin in the specified project. * * @param project * project for which Build Configurator plugin shall be return. * * @return reference to the Build Configurator plugin or {@code null} if * Build Configurator was not configured for this project. */ private Plugin getPlugin(final MavenProject project) { for (final Plugin projectPlugin : project.getBuild().getPlugins()) { if (BUILD_CONFIGURATOR_GROUP_ID.equals(projectPlugin.getGroupId()) && BUILD_CONFIGURATOR_ARTIFACT_ID.equals(projectPlugin.getArtifactId())) { return projectPlugin; } } return null; } }