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.checker.cli;
019
020import org.apache.maven.project.MavenProject;
021import org.kordamp.maven.checker.MavenCentralChecker;
022import org.kordamp.maven.checker.PomCheckException;
023import org.kordamp.maven.checker.cli.internal.PomParser;
024import picocli.CommandLine;
025
026/**
027 * @author Andres Almiray
028 * @since 1.1.0
029 */
030@CommandLine.Command(name = "check-maven-central",
031    description = "Checks if a POM complies with the rules for uploading to Maven Central")
032public class CheckMavenCentral extends AbstractCommand<Main> {
033    @CommandLine.Option(names = {"--strict"},
034        negatable = true,
035        defaultValue = "true", fallbackValue = "true",
036        description = "Checks if <repositories> and <pluginRepositories> are present.")
037    boolean strict;
038
039    @CommandLine.Option(names = {"--release"},
040        negatable = true,
041        defaultValue = "true", fallbackValue = "true",
042        description = "Checks if version is -SNAPSHOT.")
043    boolean release;
044
045    @CommandLine.Option(names = {"--fail-on-error"},
046        negatable = true,
047        defaultValue = "true", fallbackValue = "true",
048        description = "Fails the build on error.")
049    boolean failOnError;
050
051    @CommandLine.Option(names = {"--fail-on-warning"},
052        negatable = true,
053        defaultValue = "true", fallbackValue = "true",
054        description = "Fails the build on warning.")
055    boolean failOnWarning;
056
057    @Override
058    protected void execute() {
059        try {
060            logger.info("Maven Central checks: {}", pomFile.toAbsolutePath().toString());
061            MavenProject project = PomParser.createMavenProject(pomFile.toFile());
062            MavenCentralChecker.check(logger, project, new MavenCentralChecker.Configuration()
063                .withRelease(release)
064                .withStrict(strict)
065                .withFailOnError(failOnError)
066                .withFailOnWarning(failOnWarning));
067        } catch (PomCheckException e) {
068            throw new HaltExecutionException(e);
069        }
070    }
071}