Example usage for org.apache.maven.project MavenProject getBasedir

List of usage examples for org.apache.maven.project MavenProject getBasedir

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getBasedir.

Prototype

public File getBasedir() 

Source Link

Usage

From source file:com.atlassian.maven.plugin.clover.CloverAggregateMojo.java

License:Apache License

private List<String> getChildrenCloverDatabases() {
    // Ideally we'd need to find out where each module stores its Clover
    // database. However that's not
    // currently possible in m2 (see
    // http://jira.codehaus.org/browse/MNG-2180). Thus we'll assume for now
    // that all modules use the cloverDatabase configuration from the top
    // level module.

    // Find out the location of the clover DB relative to the root module.
    // Note: This is a pretty buggy algorithm and we really need a proper
    // solution (see MNG-2180)
    final String resolvedCloverDb = resolveCloverDatabase();
    final String projectBaseDir = getProject().getBasedir().getPath();
    getLog().debug("Calculating relative database path of '" + resolvedCloverDb
            + "' against the project base directory '" + projectBaseDir + "'");
    final String relativeCloverDatabasePath = resolvedCloverDb.substring(projectBaseDir.length());
    getLog().debug("Relative path is '" + relativeCloverDatabasePath + "'");
    final List<String> dbFiles = new ArrayList<String>();
    final List<MavenProject> projects = getDescendantModuleProjects(getProject());

    for (MavenProject childProject : projects) {
        getLog().debug("Looking for Clover database for module " + childProject.getId() + " ("
                + childProject.getBasedir() + ")");
        final File cloverDb = new File(childProject.getBasedir(), relativeCloverDatabasePath);
        if (cloverDb.exists()) {
            getLog().debug("Database found at " + cloverDb.getAbsolutePath() + " . Adding for merge.");
            dbFiles.add(cloverDb.getPath());
        } else {/*from   ww  w.j  a va2 s.  c  o  m*/
            getLog().debug("No database at " + cloverDb.getAbsolutePath());
        }
    }

    return dbFiles;
}

From source file:com.atlassian.maven.plugin.clover.internal.AbstractCloverMojo.java

License:Apache License

/**
 * Returns true if the supplied potentialModule project is a module
 * of the specified parentProject./*  w  w w .  j a  v a  2  s  .c  om*/
 *
 * @param parentProject   the parent project.
 * @param potentialModule the potential moduleproject.
 * @return true if the potentialModule is indeed a module of the specified
 *         parent project.
 */
protected boolean isModuleOfProject(final MavenProject parentProject, final MavenProject potentialModule) {
    boolean result = false;
    final List<String> modules = parentProject.getModules();

    if (modules != null) {
        final File parentBaseDir = parentProject.getBasedir();

        for (final String module : modules) {
            File moduleBaseDir = new File(parentBaseDir, module);

            try {
                // need these to be canonical paths so we can perform a true equality
                // operation and remember <module> is a path and for flat multimodule project
                // structures they will be like this: <module>../a-project<module>
                final String lhs = potentialModule.getBasedir().getCanonicalPath();
                final String rhs = moduleBaseDir.getCanonicalPath();

                if (lhs.equals(rhs)) {
                    getLog().debug("isModuleOfProject: lhs=" + lhs + " rhs=" + rhs + " MATCH FOUND");
                    result = true;
                    break;
                } else {
                    getLog().debug("isModuleOfProject: lhs=" + lhs + " rhs=" + rhs);
                }
            } catch (IOException e) {
                // suppress the exception (?)
                getLog().error("error encountered trying to resolve canonical module paths");
            }
        }
    }

    return result;
}

From source file:com.bekioui.maven.plugin.client.initializer.ProjectInitializer.java

License:Apache License

@SuppressWarnings("unchecked")
public static Project initialize(MavenProject mavenProject, Properties properties)
        throws MojoExecutionException {
    String clientPackagename = mavenProject.getParent().getGroupId() + ".client";
    String contextPackageName = clientPackagename + ".context";
    String apiPackageName = clientPackagename + ".api";
    String implPackageName = clientPackagename + ".impl";
    String resourceBasedirPath = mavenProject.getBasedir().getAbsolutePath();
    String projectBasedirPath = resourceBasedirPath.substring(0,
            resourceBasedirPath.lastIndexOf(File.separator));

    File clientDirectory = new File(projectBasedirPath + File.separator + properties.clientArtifactId());
    if (clientDirectory.exists()) {
        try {/*ww w . ja  v  a 2s.co  m*/
            FileUtils.deleteDirectory(clientDirectory);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to delete existing client directory.", e);
        }
    }
    clientDirectory.mkdir();

    File javaSourceDirectory = new File(resourceBasedirPath + FULL_SRC_FOLDER
            + properties.resourcePackageName().replaceAll("\\.", File.separator));
    if (!javaSourceDirectory.isDirectory()) {
        throw new MojoExecutionException(
                "Java sources directory not found: " + javaSourceDirectory.getAbsolutePath());
    }

    List<String> classpathElements;
    try {
        classpathElements = mavenProject.getCompileClasspathElements();
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException("Failed to get compile classpath elements.", e);
    }

    List<URL> classpaths = new ArrayList<>();
    for (String element : classpathElements) {
        try {
            classpaths.add(new File(element).toURI().toURL());
        } catch (MalformedURLException e) {
            throw new MojoExecutionException(element + " is an invalid classpath element.", e);
        }
    }

    return Project.builder() //
            .mavenProject(mavenProject) //
            .properties(properties) //
            .clientPackageName(clientPackagename) //
            .contextPackageName(contextPackageName) //
            .apiPackageName(apiPackageName) //
            .implPackageName(implPackageName) //
            .clientDirectory(clientDirectory) //
            .javaSourceDirectory(javaSourceDirectory) //
            .classpaths(classpaths) //
            .build();
}

From source file:com.coderplus.m2e.yuicompressorcore.CoderPlusBuildParticipant.java

License:Open Source License

@Override
public Set<IProject> build(final int kind, final IProgressMonitor monitor) throws Exception {

    final MojoExecution execution = getMojoExecution();

    if (execution == null) {
        return null;
    }// ww  w. ja va  2  s.  co m
    IMaven maven = MavenPlugin.getMaven();
    MavenProject project = getMavenProjectFacade().getMavenProject();
    final BuildContext buildContext = getBuildContext();

    boolean skip = Boolean.TRUE.equals(
            maven.getMojoParameterValue(project, execution, SKIP, Boolean.class, new NullProgressMonitor()));

    if (skip) {
        return null;
    }

    if (buildContext.isIncremental()) {
        File resourcesDirectory = maven.getMojoParameterValue(project, execution, SOURCE_DIRECTORY, File.class,
                new NullProgressMonitor());
        if (resourcesDirectory == null || !resourcesDirectory.exists()) {
            resourcesDirectory = new File(project.getBasedir(), "src/main/resources");
        }
        Scanner ds = buildContext.newScanner(resourcesDirectory);
        if (ds != null) {
            ds.scan();
            String[] includedResourceFiles = ds.getIncludedFiles();
            if (includedResourceFiles == null || includedResourceFiles.length == 0) {
                //ignore if there were no changes to the resources and was an incremental build
                return null;
            }
        }
    }

    File outputDirectory = maven.getMojoParameterValue(project, execution, OUTPUT_DIRECTORY, File.class,
            new NullProgressMonitor());
    setTaskName(monitor);

    //execute the maven mojo
    final Set<IProject> result = executeMojo(kind, monitor);

    if (outputDirectory != null && outputDirectory.exists()) {
        buildContext.refresh(outputDirectory);
    }

    return result;
}

From source file:com.datacoper.maven.util.DCProjectUtil.java

private static MavenProject validateAndStartModule(MavenProject parentProjetct, String moduleName)
        throws DcRuntimeException {
    if (!parentProjetct.getModules().contains(moduleName)) {
        throw new DcRuntimeException("Module {0} is not located in parent project {0}", moduleName,
                parentProjetct.getName());
    }//from   ww  w  .j  av a 2  s . co  m

    String pathParent = parentProjetct.getBasedir().getPath();

    MavenProject mavenProject = MavenUtil.startNewProject(pathParent.concat("/").concat(moduleName));

    return mavenProject;
}

From source file:com.dooioo.se.lorik.apidoclet.maven.plugin.JavadocUtil.java

License:Apache License

/**
 * Method that removes the invalid directories in the specified directories.
 * <b>Note</b>: All elements in <code>dirs</code> could be an absolute or relative against the project's base
 * directory <code>String</code> path.
 *
 * @param project the current Maven project not null
 * @param dirs the list of <code>String</code> directories path that will be validated.
 * @return a List of valid <code>String</code> directories absolute paths.
 *///w  w w  .  ja v a2 s.  c  o  m
public static List<String> pruneDirs(MavenProject project, List<String> dirs) {
    List<String> pruned = new ArrayList<String>(dirs.size());
    for (String dir : dirs) {
        if (dir == null) {
            continue;
        }

        File directory = new File(dir);
        if (!directory.isAbsolute()) {
            directory = new File(project.getBasedir(), directory.getPath());
        }

        if (directory.isDirectory() && !pruned.contains(directory.getAbsolutePath())) {
            pruned.add(directory.getAbsolutePath());
        }
    }

    return pruned;
}

From source file:com.github.emabrey.maven.plugins.basedir.RootDirectoryGoal.java

License:Open Source License

/**
 * Generates a {@link List} populated with all non-null base directory values acquired from querying the provided
 * {@link MavenProject} instances./*from  www  .ja v  a 2 s  .  c  om*/
 *
 * @param projects
 *        The {@link List} of {@link MavenProject} instances from which base directory information should be
 *        collected.
 * @return A {@link List} of {@link File} instances generated using the base directory data of the given
 *         {@link MavenProject} instances.
 */
private List<File> getNonullProjectBaseDirectories(final List<MavenProject> projects) {

    final List<File> candidates = new ArrayList<>(projects.size());

    for (MavenProject project : projects) {
        final File baseDir = project.getBasedir();
        if (baseDir != null) {
            candidates.add(baseDir);
        }
    }

    return candidates;
}

From source file:com.github.konum.junitaward.AwardsMojo.java

License:Open Source License

public void execute() throws MojoExecutionException {

    MavenProject project = (MavenProject) getPluginContext().get("project");
    Path pathToTest = project.getBasedir().toPath();
    // TOOD: optina parameter for test root folder.
    pathToTest = pathToTest.resolve(Paths.get(testsFolder));
    try {/*from   w  w  w.  j  a va2s. co m*/
        getLog().info("Analyzing tests folder " + pathToTest.toString());
        Files.walkFileTree(pathToTest, new VisitTest());
        generateHtml(result, project.getBasedir().toPath());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:com.github.maven_nar.NarProperties.java

License:Apache License

private NarProperties(final MavenProject project) throws MojoFailureException {

    final Properties defaults = PropertyUtils.loadProperties(NarUtil.class.getResourceAsStream(AOL_PROPERTIES));
    if (defaults == null) {
        throw new MojoFailureException(
                "NAR: Could not load default properties file: '" + AOL_PROPERTIES + "'.");
    }/*w  ww. j  a  v a2 s .c o m*/

    this.properties = new Properties(defaults);
    FileInputStream fis = null;
    String customPropertyLocation = null;
    try {
        if (project != null) {
            customPropertyLocation = project.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);
            if (customPropertyLocation == null) {
                // Try and read from the system property in case it's specified there
                customPropertyLocation = System.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);
            }
            fis = new FileInputStream(customPropertyLocation != null ? customPropertyLocation
                    : project.getBasedir() + File.separator + AOL_PROPERTIES);
            this.properties.load(fis);
        }
    } catch (final FileNotFoundException e) {
        if (customPropertyLocation != null) {
            // We tried loading from a custom location - so throw the exception
            throw new MojoFailureException(
                    "NAR: Could not load custom properties file: '" + customPropertyLocation + "'.");
        }
    } catch (final IOException e) {
        // ignore (FIXME)
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (final IOException e) {
            // ignore
        }
    }

}

From source file:com.github.spyhunter99.jacoco.report.plugin.JacocoReport.java

License:Apache License

private List<JacocoItem> copyResources(MavenProject project) throws IOException {
    if (project == null) {
        return Collections.EMPTY_LIST;
    }//from   w  w w  .java  2s.c o m
    List<JacocoItem> outDirs = new ArrayList<>();

    if ("pom".equalsIgnoreCase(project.getPackaging())) {
        for (int k = 0; k < project.getCollectedProjects().size(); k++) {
            outDirs.addAll(copyResources((MavenProject) project.getCollectedProjects().get(k)));
        }
    } else {
        File moduleBaseDir = project.getBasedir();
        File target = new File(moduleBaseDir, "target");
        if (target.exists()) {
            File jacocoUt = new File(moduleBaseDir, "target/site/jacoco-ut/"); //TODO properterize
            File jacocoIt = new File(moduleBaseDir, "target/site/jacoco-it/"); //TODO properterize

            JacocoItem item = new JacocoItem();
            item.setModuleName(project.getArtifactId());

            if (jacocoIt.exists()) {
                //since all artifacts should have unique names...this should be ok
                JacocoReportMetric report = new JacocoReportMetric();
                report.setReportDir(jacocoIt);
                report.setMetric(getMetric(new File(jacocoIt, "index.html")));
                item.getReportDirs().add(report);
                File dest = new File(
                        "target/site/jacoco/" + project.getArtifactId() + "/" + jacocoIt.getName());
                dest.mkdirs();
                org.apache.commons.io.FileUtils.copyDirectory(jacocoIt, dest);

            }
            if (jacocoUt.exists()) {
                //since all artifacts should have unique names...this should be ok
                JacocoReportMetric report = new JacocoReportMetric();
                report.setReportDir(jacocoUt);
                report.setMetric(getMetric(new File(jacocoUt, "index.html")));
                item.getReportDirs().add(report);

                File dest = new File(
                        "target/site/jacoco/" + project.getArtifactId() + "/" + jacocoUt.getName());
                dest.mkdirs();
                org.apache.commons.io.FileUtils.copyDirectory(jacocoUt, dest);

            }

            outDirs.add(item);

        }
    }

    return outDirs;
}