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:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static void populateSurefireReportsPath(MavenProject pom, Properties props) {
    String surefireReportsPath = MavenUtils.getPluginSetting(pom, MavenUtils.GROUP_ID_APACHE_MAVEN,
            ARTIFACTID_MAVEN_SUREFIRE_PLUGIN, "reportsDirectory",
            pom.getBuild().getDirectory() + File.separator + "surefire-reports");
    File path = resolvePath(surefireReportsPath, pom.getBasedir());
    if (path != null && path.exists()) {
        props.put(SUREFIRE_REPORTS_PATH_PROPERTY, path.getAbsolutePath());
    }//  w  ww.  jav a2  s. co m
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static void populateLibraries(MavenProject pom, Properties props, boolean test)
        throws MojoExecutionException {
    List<File> libraries = new ArrayList<>();
    try {/*from  w w  w  . ja  va 2  s  .  c  o  m*/
        List<String> classpathElements = test ? pom.getTestClasspathElements()
                : pom.getCompileClasspathElements();
        if (classpathElements != null) {
            for (String classPathString : classpathElements) {
                if (!classPathString.equals(
                        test ? pom.getBuild().getTestOutputDirectory() : pom.getBuild().getOutputDirectory())) {
                    File libPath = resolvePath(classPathString, pom.getBasedir());
                    if (libPath != null && libPath.exists()) {
                        libraries.add(libPath);
                    }
                }
            }
        }
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException("Unable to populate" + (test ? " test" : "") + " libraries", e);
    }
    if (!libraries.isEmpty()) {
        String librariesValue = StringUtils.join(toPaths(libraries), SEPARATOR);
        if (test) {
            props.setProperty(JAVA_PROJECT_TEST_LIBRARIES, librariesValue);
        } else {
            // Populate both deprecated and new property for backward compatibility
            props.setProperty(PROJECT_LIBRARIES, librariesValue);
            props.setProperty(JAVA_PROJECT_MAIN_LIBRARIES, librariesValue);
        }
    }
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static void populateBinaries(MavenProject pom, Properties props) {
    File mainBinaryDir = resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir());
    if (mainBinaryDir != null && mainBinaryDir.exists()) {
        String binPath = mainBinaryDir.getAbsolutePath();
        // Populate both deprecated and new property for backward compatibility
        props.setProperty(PROJECT_BINARY_DIRS, binPath);
        props.setProperty(JAVA_PROJECT_MAIN_BINARY_DIRS, binPath);
        props.setProperty(GROOVY_PROJECT_MAIN_BINARY_DIRS, binPath);
    }/*from ww  w  .ja v a 2 s  .com*/
    File testBinaryDir = resolvePath(pom.getBuild().getTestOutputDirectory(), pom.getBasedir());
    if (testBinaryDir != null && testBinaryDir.exists()) {
        String binPath = testBinaryDir.getAbsolutePath();
        props.setProperty(JAVA_PROJECT_TEST_BINARY_DIRS, binPath);
    }
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static void removeTarget(MavenProject pom, Collection<String> relativeOrAbsolutePaths) {
    final Path baseDir = pom.getBasedir().toPath().toAbsolutePath().normalize();
    final Path target = Paths.get(pom.getBuild().getDirectory()).toAbsolutePath().normalize();
    final Path targetRelativePath = baseDir.relativize(target);

    relativeOrAbsolutePaths.removeIf(pathStr -> {
        Path path = Paths.get(pathStr).toAbsolutePath().normalize();
        Path relativePath = baseDir.relativize(path);
        return relativePath.startsWith(targetRelativePath);
    });//ww  w.j  a v a 2  s  . co  m
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private List<File> sourcePaths(MavenProject pom, String propertyKey, Collection<String> mavenPaths)
        throws MojoExecutionException {
    List<File> filesOrDirs;
    boolean userDefined = false;
    String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey),
            envProperties.getProperty(propertyKey));
    prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey));

    if (prop != null) {
        List<String> paths = Arrays.asList(StringUtils.split(prop, ","));
        filesOrDirs = resolvePaths(paths, pom.getBasedir());
        userDefined = true;/*from w ww .  j  a  v  a 2  s  . c o  m*/
    } else {
        removeTarget(pom, mavenPaths);
        filesOrDirs = resolvePaths(mavenPaths, pom.getBasedir());
    }

    if (userDefined && !MAVEN_PACKAGING_POM.equals(pom.getModel().getPackaging())) {
        return existingPathsOrFail(filesOrDirs, pom, propertyKey);
    } else {
        // Maven provides some directories that do not exist. They
        // should be removed. Same for pom module were sonar.sources and sonar.tests
        // can be defined only to be inherited by children
        return removeNested(keepExistingPaths(filesOrDirs));
    }
}

From source file:org.sonatype.install4j.maven.AntHelper.java

License:Open Source License

public AntHelper(final Mojo owner, final MavenProject project) {
    assert owner != null;
    assert project != null;

    this.owner = owner;
    this.log = owner.getLog();
    this.project = project;

    ant = new Project();
    ant.setBaseDir(project.getBasedir());
    initAntLogger(ant);//from w  ww.  j a v  a2s .c om
    ant.init();

    // Inherit properties from Maven
    inheritProperties();
}

From source file:org.sonatype.m2e.webby.internal.config.WarConfigurationExtractor.java

License:Open Source License

public String getWorkDirectory(MavenProject mvnProject) {
    String basedir = mvnProject.getBasedir().getAbsolutePath();
    return resolve(basedir, mvnProject.getBuild().getDirectory() + "/m2e-webby");
}

From source file:org.sonatype.m2e.webby.internal.config.WarConfigurationExtractor.java

License:Open Source License

public WarConfiguration getConfiguration(IMavenProjectFacade mvnFacade, MavenProject mvnProject,
        MavenSession mvnSession, IProgressMonitor monitor) throws CoreException {
    SubMonitor pm = SubMonitor.convert(monitor, "Reading WAR configuration...", 100);
    try {/*from  w  w  w.ja va2  s .c o m*/
        WarConfiguration warConfig = new WarConfiguration();

        List<MojoExecution> mojoExecs = mvnFacade.getMojoExecutions(WAR_PLUGIN_GID, WAR_PLUGIN_AID,
                pm.newChild(90), "war");

        IMaven maven = MavenPlugin.getMaven();

        String basedir = mvnProject.getBasedir().getAbsolutePath();

        String encoding = mvnProject.getProperties().getProperty("project.build.sourceEncoding");

        warConfig.setWorkDirectory(getWorkDirectory(mvnProject));

        warConfig.setClassesDirectory(resolve(basedir, mvnProject.getBuild().getOutputDirectory()));

        if (!mojoExecs.isEmpty()) {
            MojoExecution mojoExec = mojoExecs.get(0);

            Set<String> overlayKeys = new HashSet<String>();
            Object[] overlays = maven.getMojoParameterValue(mvnSession, mojoExec, "overlays", Object[].class);
            if (overlays != null) {
                boolean mainConfigured = false;
                for (Object overlay : overlays) {
                    OverlayConfiguration overlayConfig = new OverlayConfiguration(overlay);
                    if (overlayConfig.isMain()) {
                        if (mainConfigured) {
                            continue;
                        }
                        mainConfigured = true;
                    }
                    warConfig.getOverlays().add(overlayConfig);
                    overlayKeys.add(overlayConfig.getArtifactKey());
                }
                if (!mainConfigured) {
                    warConfig.getOverlays().add(0, new OverlayConfiguration(null, null, null, null));
                }
            }

            Map<String, Artifact> overlayArtifacts = new LinkedHashMap<String, Artifact>();
            for (Artifact artifact : mvnProject.getArtifacts()) {
                if ("war".equals(artifact.getType())) {
                    overlayArtifacts.put(artifact.getDependencyConflictId(), artifact);
                }
            }

            for (Map.Entry<String, Artifact> e : overlayArtifacts.entrySet()) {
                if (!overlayKeys.contains(e.getKey())) {
                    Artifact a = e.getValue();
                    OverlayConfiguration warOverlay = new OverlayConfiguration(a.getGroupId(),
                            a.getArtifactId(), a.getClassifier(), a.getType());
                    warConfig.getOverlays().add(warOverlay);
                }
            }

            for (OverlayConfiguration overlay : warConfig.getOverlays()) {
                overlay.setEncoding(encoding);
            }

            String warSrcDir = maven.getMojoParameterValue(mvnSession, mojoExec, "warSourceDirectory",
                    String.class);
            String warSrcInc = maven.getMojoParameterValue(mvnSession, mojoExec, "warSourceIncludes",
                    String.class);
            String warSrcExc = maven.getMojoParameterValue(mvnSession, mojoExec, "warSourceExcludes",
                    String.class);
            warConfig.getResources()
                    .add(new ResourceConfiguration(warSrcDir, split(warSrcInc), split(warSrcExc)));

            ResourceConfiguration[] resources = maven.getMojoParameterValue(mvnSession, mojoExec,
                    "webResources", ResourceConfiguration[].class);
            if (resources != null) {
                warConfig.getResources().addAll(Arrays.asList(resources));
            }

            for (ResourceConfiguration resource : warConfig.getResources()) {
                resource.setDirectory(resolve(basedir, resource.getDirectory()));
                resource.setEncoding(encoding);
            }

            String filenameMapping = maven.getMojoParameterValue(mvnSession, mojoExec, "outputFileNameMapping",
                    String.class);
            warConfig.setFilenameMapping(filenameMapping);

            String escapeString = maven.getMojoParameterValue(mvnSession, mojoExec, "escapeString",
                    String.class);
            warConfig.setEscapeString(escapeString);

            String webXml = maven.getMojoParameterValue(mvnSession, mojoExec, "webXml", String.class);
            warConfig.setWebXml(resolve(basedir, webXml));

            Boolean webXmlFiltered = maven.getMojoParameterValue(mvnSession, mojoExec,
                    "filteringDeploymentDescriptors", Boolean.class);
            warConfig.setWebXmlFiltered(webXmlFiltered.booleanValue());

            Boolean backslashesEscaped = maven.getMojoParameterValue(mvnSession, mojoExec,
                    "escapedBackslashesInFilePath", Boolean.class);
            warConfig.setBackslashesInFilePathEscaped(backslashesEscaped.booleanValue());

            String[] nonFilteredFileExtensions = maven.getMojoParameterValue(mvnSession, mojoExec,
                    "nonFilteredFileExtensions", String[].class);
            warConfig.getNonFilteredFileExtensions().addAll(Arrays.asList(nonFilteredFileExtensions));

            String[] filters = maven.getMojoParameterValue(mvnSession, mojoExec, "filters", String[].class);
            for (String filter : filters) {
                warConfig.getFilters().add(resolve(basedir, filter));
            }

            String packagingIncludes = maven.getMojoParameterValue(mvnSession, mojoExec, "packagingIncludes",
                    String.class);
            warConfig.setPackagingIncludes(split(packagingIncludes));
            String packagingExcludes = maven.getMojoParameterValue(mvnSession, mojoExec, "packagingExcludes",
                    String.class);
            warConfig.setPackagingExcludes(split(packagingExcludes));
        } else {
            throw WebbyPlugin.newError(
                    "Could not locate configuration for maven-war-plugin in POM for " + mvnProject.getId(),
                    null);
        }

        return warConfig;
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}

From source file:org.sonatype.maven.plugin.emma.EmmaUtils.java

License:Open Source License

/**
 * Fix EMMA data file locations. EMMA generates data files in wrong locations: this method moves these files to
 * valid locations./*from ww  w .  j  av a2  s  .c  o  m*/
 * 
 * @param project
 *            current Maven project
 * @param dataFiles
 *            to fix
 * @return new data file locations
 */
public static File[] fixDataFileLocations(MavenProject project, File[] dataFiles) {
    final List newDataFiles = new ArrayList();
    for (int i = 0; i < dataFiles.length; ++i) {
        final File src = dataFiles[i];
        if (!src.exists()) {
            // if the file does not exist, we cannot use it
            continue;
        }

        if (src.getParentFile().equals(project.getBasedir())) {
            // EMMA generates coverage data files in project root
            // (as it is actually the current directory):
            // move these files to the "target" directory
            final File dst = new File(project.getBuild().getDirectory(), "coverage-" + i + ".ec");
            try {
                FileUtils.rename(src, dst);
            } catch (IOException e) {
                throw new IllegalStateException("Failed to move coverage data file: " + src.getAbsolutePath(),
                        e);
            }
            newDataFiles.add(dst);
        } else {
            newDataFiles.add(src);
        }
    }

    return (File[]) newDataFiles.toArray(new File[newDataFiles.size()]);
}

From source file:org.sonatype.nexus.maven.staging.AbstractStagingMojo.java

License:Open Source License

/**
 * Returns the working directory root (the one containing all the staged and deferred deploys), that is either set
 * explicitly by user in plugin configuration (see {@link #altStagingDirectory} parameter), or it's location is
 * calculated taking as base the first project in this reactor that will/was executing this plugin.
 *//* www .  j  a v a2 s.  c om*/
protected File getWorkDirectoryRoot() {
    if (altStagingDirectory != null) {
        return altStagingDirectory;
    } else {
        final MavenProject firstWithThisMojo = getFirstProjectWithThisPluginDefined();
        if (firstWithThisMojo != null) {
            final File firstWithThisMojoBuildDir;
            if (firstWithThisMojo.getBuild() != null && firstWithThisMojo.getBuild().getDirectory() != null) {
                firstWithThisMojoBuildDir = new File(firstWithThisMojo.getBuild().getDirectory())
                        .getAbsoluteFile();
            } else {
                firstWithThisMojoBuildDir = new File(firstWithThisMojo.getBasedir().getAbsoluteFile(),
                        "target");
            }
            return new File(firstWithThisMojoBuildDir, "nexus-staging");
        } else {
            // top level (invocation place with some sensible defaults)
            // TODO: what can we do here? Do we have MavenProject at all?
            return new File(getMavenSession().getExecutionRootDirectory() + "/target/nexus-staging");
        }
    }
}