001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2020-2024 Andres Almiray.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.kordamp.maven.enforcer.checker;
019
020import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
021import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
022import org.apache.maven.project.MavenProject;
023import org.kordamp.maven.checker.BomChecker;
024import org.kordamp.maven.checker.PomCheckException;
025
026import javax.inject.Inject;
027import javax.inject.Named;
028
029/**
030 * Checks if a POM file is a minimal BOM file.
031 * Minimal BOM files contain the following elements:
032 * <ul>
033 * <li>&lt;groupId&gt;</li>
034 * <li>&lt;artifactId&gt;</li>
035 * <li>&lt;version&gt;</li>
036 * <li>&lt;dependencyManagement&gt;</li>
037 * </ul>
038 *
039 * @author Andres Almiray
040 * @since 1.0.0
041 */
042@Named("checkBom")
043public class CheckBom extends AbstractEnforcerRule {
044    private boolean failOnError;
045
046    public boolean isFailOnError() {
047        return failOnError;
048    }
049
050    public CheckBom setFailOnError(boolean failOnError) {
051        this.failOnError = failOnError;
052        return this;
053    }
054
055    @Inject
056    private MavenProject project;
057
058    @Override
059    public void execute() throws EnforcerRuleException {
060        try {
061            BomChecker.check(new MavenEnforcerLoggerAdapter(getLog()), project, new BomChecker.Configuration()
062                .withFailOnError(failOnError));
063        } catch (PomCheckException e) {
064            throw new EnforcerRuleException(e.getMessage());
065        }
066    }
067}