001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang.SystemUtils;
023import org.apache.maven.execution.MavenSession;
024import org.apache.maven.execution.RuntimeInformation;
025import org.apache.maven.plugin.AbstractMojo;
026import org.apache.maven.plugin.MojoExecution;
027import org.apache.maven.plugin.MojoExecutionException;
028import org.apache.maven.plugins.annotations.Component;
029import org.apache.maven.plugins.annotations.Mojo;
030import org.apache.maven.plugins.annotations.Parameter;
031import org.apache.maven.project.MavenProject;
032import org.apache.maven.project.path.PathTranslator;
033import org.codehaus.plexus.PlexusConstants;
034import org.codehaus.plexus.PlexusContainer;
035import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
036import org.codehaus.plexus.context.Context;
037import org.codehaus.plexus.context.ContextException;
038import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
039
040/**
041 * This goal displays the current platform information.
042 *
043 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
044 * @version $Id: DisplayInfoMojo.java 1649120 2015-01-02 21:01:18Z khmarbaise $
045 */
046@Mojo( name = "display-info", threadSafe = true )
047public class DisplayInfoMojo
048    extends AbstractMojo
049    implements Contextualizable
050{
051
052    /**
053     * Path Translator needed by the ExpressionEvaluator
054     */
055    @Component( role = PathTranslator.class )
056    protected PathTranslator translator;
057
058    /**
059     * MojoExecution needed by the ExpressionEvaluator
060     */
061    @Parameter( defaultValue = "${mojoExecution}", readonly = true, required = true )
062    protected MojoExecution mojoExecution;
063
064    /**
065     * The MavenSession
066     */
067    @Parameter( defaultValue = "${session}", readonly = true, required = true )
068    protected MavenSession session;
069
070    /**
071     * POM
072     */
073    @Parameter( defaultValue = "${project}", readonly = true, required = true )
074    protected MavenProject project;
075
076    // set by the contextualize method. Only way to get the
077    // plugin's container in 2.0.x
078    protected PlexusContainer container;
079
080    public void contextualize ( Context context )
081        throws ContextException
082    {
083        container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
084    }
085
086    /**
087     * Entry point to the mojo
088     */
089    public void execute ()
090        throws MojoExecutionException
091    {
092        try
093        {
094            EnforcerExpressionEvaluator evaluator = new EnforcerExpressionEvaluator( session, translator, project, 
095                                                                                     mojoExecution );
096            DefaultEnforcementRuleHelper helper = new DefaultEnforcementRuleHelper( session, evaluator, getLog(),
097                                                                                    container );
098            RuntimeInformation rti = (RuntimeInformation) helper.getComponent( RuntimeInformation.class );
099            getLog().info( "Maven Version: " + rti.getApplicationVersion() );
100            getLog().info( "JDK Version: " + SystemUtils.JAVA_VERSION + " normalized as: "
101                               + RequireJavaVersion.normalizeJDKVersion( SystemUtils.JAVA_VERSION_TRIMMED ) );
102            RequireOS os = new RequireOS();
103            os.displayOSInfo( getLog(), true );
104        }
105        catch ( ComponentLookupException e )
106        {
107            getLog().warn( "Unable to Lookup component: " + e.getLocalizedMessage() );
108        }
109    }
110
111}