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.MavenCentralChecker;
024import org.kordamp.maven.checker.PomCheckException;
025
026import javax.inject.Inject;
027import javax.inject.Named;
028
029/**
030 * Checks if a POM complies with the rules for uploading to Maven Central.
031 *
032 * @author Andres Almiray
033 * @since 1.0.0
034 */
035@Named("checkMavenCentral")
036public class CheckMavenCentral extends AbstractEnforcerRule {
037    private boolean release = true;
038    private boolean strict = true;
039    private boolean failOnError = true;
040    private boolean failOnWarning;
041
042    public boolean isRelease() {
043        return release;
044    }
045
046    public CheckMavenCentral setRelease(boolean release) {
047        this.release = release;
048        return this;
049    }
050
051    public boolean isStrict() {
052        return strict;
053    }
054
055    public CheckMavenCentral setStrict(boolean strict) {
056        this.strict = strict;
057        return this;
058    }
059
060    public boolean isFailOnError() {
061        return failOnError;
062    }
063
064    public CheckMavenCentral setFailOnError(boolean failOnError) {
065        this.failOnError = failOnError;
066        return this;
067    }
068
069    public boolean isFailOnWarning() {
070        return failOnWarning;
071    }
072
073    public CheckMavenCentral setFailOnWarning(boolean failOnWarning) {
074        this.failOnWarning = failOnWarning;
075        return this;
076    }
077
078    @Inject
079    private MavenProject project;
080
081    @Override
082    public void execute() throws EnforcerRuleException {
083        try {
084            MavenCentralChecker.check(new MavenEnforcerLoggerAdapter(getLog()), project, new MavenCentralChecker.Configuration()
085                .withRelease(release)
086                .withStrict(strict)
087                .withFailOnError(failOnError)
088                .withFailOnWarning(failOnWarning));
089        } catch (PomCheckException e) {
090            throw new EnforcerRuleException(e.getMessage());
091        }
092    }
093}