com.github.robtimus.maven.plugins.buildhelper.MojoUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of build-helper-maven-plugin Show documentation
Show all versions of build-helper-maven-plugin Show documentation
A plugin that contains several utility goals
/*
* 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$
}
}