All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.robtimus.maven.plugins.buildhelper.MojoUtils Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
 * MojoUtils.java
 * Copyright 2023 Rob Spoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.robtimus.maven.plugins.buildhelper;

import java.nio.file.Files;
import java.nio.file.Path;

final class MojoUtils {

    private MojoUtils() {
    }

    static Path getProjectRoot(Path baseDir) {
        Path dir = baseDir.toAbsolutePath().normalize();
        if (isGitRoot(dir)) {
            return dir;
        }
        Path parent = dir.getParent();
        while (parent != null) {
            if (isGitRoot(parent)) {
                return parent;
            }
            if (!isMavenProjectFolder(parent)) {
                return dir;
            }
            dir = parent;
            parent = parent.getParent();
        }
        return dir;
    }

    static boolean isMavenProjectFolder(Path dir) {
        return Files.isRegularFile(dir.resolve("pom.xml")); //$NON-NLS-1$
    }

    static boolean isGitRoot(Path dir) {
        return Files.isDirectory(dir.resolve(".git")); //$NON-NLS-1$
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy