001package org.apache.maven.project;
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 java.io.File;
023import java.io.PrintWriter;
024import java.io.StringWriter;
025import java.util.List;
026
027import org.apache.maven.model.building.ModelProblem;
028import org.apache.maven.model.building.ModelProblemUtils;
029
030/**
031 * @author Jason van Zyl
032 */
033public class ProjectBuildingException
034    extends Exception
035{
036    private final String projectId;
037
038    private File pomFile;
039
040    private List<ProjectBuildingResult> results;
041
042    public ProjectBuildingException( String projectId, String message, Throwable cause )
043    {
044        super( createMessage( message, projectId, null ), cause );
045        this.projectId = projectId;
046    }
047
048    /**
049     * @param projectId
050     * @param message
051     * @param pomFile   pom file location
052     */
053    public ProjectBuildingException( String projectId, String message, File pomFile )
054    {
055        super( createMessage( message, projectId, pomFile ) );
056        this.projectId = projectId;
057        this.pomFile = pomFile;
058    }
059
060    /**
061     * @param projectId
062     * @param message
063     * @param pomFile   pom file location
064     * @param cause
065     */
066    protected ProjectBuildingException( String projectId, String message, File pomFile, Throwable cause )
067    {
068        super( createMessage( message, projectId, pomFile ), cause );
069        this.projectId = projectId;
070        this.pomFile = pomFile;
071    }
072
073    public ProjectBuildingException( List<ProjectBuildingResult> results )
074    {
075        super( createMessage( results ) );
076        this.projectId = "";
077        this.results = results;
078    }
079
080    public File getPomFile()
081    {
082        return pomFile;
083    }
084
085    /**
086     * @deprecated use {@link #getPomFile()}
087     */
088    public String getPomLocation()
089    {
090        if ( getPomFile() != null )
091        {
092            return getPomFile().getAbsolutePath();
093        }
094        else
095        {
096            return "null";
097        }
098    }
099
100    public String getProjectId()
101    {
102        return projectId;
103    }
104
105    public List<ProjectBuildingResult> getResults()
106    {
107        return results;
108    }
109
110    private static String createMessage( String message, String projectId, File pomFile )
111    {
112        StringBuilder buffer = new StringBuilder( 256 );
113        buffer.append( message );
114        buffer.append( " for project " ).append( projectId );
115        if ( pomFile != null )
116        {
117            buffer.append( " at " ).append( pomFile.getAbsolutePath() );
118        }
119        return buffer.toString();
120    }
121
122    private static String createMessage( List<ProjectBuildingResult> results )
123    {
124        StringWriter buffer = new StringWriter( 1024 );
125
126        PrintWriter writer = new PrintWriter( buffer );
127        writer.println( "Some problems were encountered while processing the POMs:" );
128        for ( ProjectBuildingResult result : results )
129        {
130            for ( ModelProblem problem : result.getProblems() )
131            {
132                writer.print( "[" );
133                writer.print( problem.getSeverity() );
134                writer.print( "] " );
135                writer.print( problem.getMessage() );
136                writer.print( " @ " );
137                writer.println( ModelProblemUtils.formatLocation( problem, result.getProjectId() ) );
138            }
139        }
140        writer.close();
141
142        return buffer.toString();
143    }
144
145}