Here you can find the source of getOptionalPathRelativeToMavenProjectRoot(File absoluteFile)
static Optional<File> getOptionalPathRelativeToMavenProjectRoot(File absoluteFile)
//package com.java2s; /*// w ww .ja v a 2 s . c om * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ import java.io.File; import java.nio.file.Path; import java.util.Optional; public class Main { static Optional<File> getOptionalPathRelativeToMavenProjectRoot(File absoluteFile) { if (!absoluteFile.isAbsolute()) { return Optional.of(absoluteFile); } File projectRoot = absoluteFile; while (!isProjectRootDir(projectRoot) && projectRoot.getParentFile() != null) { projectRoot = projectRoot.getParentFile(); } if (isProjectRootDir(projectRoot)) { Path absolutePath = absoluteFile.toPath(); Path basePath = projectRoot.toPath(); Path relativePath = basePath.relativize(absolutePath); return Optional.of(relativePath.toFile()); } return Optional.empty(); } private static boolean isProjectRootDir(File file) { return new File(file, "pom.xml").exists(); } }