Example usage for org.apache.maven.building Source getLocation

List of usage examples for org.apache.maven.building Source getLocation

Introduction

In this page you can find the example usage for org.apache.maven.building Source getLocation.

Prototype

String getLocation();

Source Link

Document

Provides a user-friendly hint about the location of the source.

Usage

From source file:fr.brouillard.oss.jgitver.JGitverModelProcessor.java

License:Apache License

private Model provisionModel(Model model, Map<String, ?> options) throws IOException {
    try {//from  ww  w .j av  a 2s . co  m
        calculateVersionIfNecessary();
    } catch (Exception ex) {
        throw new IOException("cannot build a Model object using jgitver", ex);
    }

    Source source = (Source) options.get(ModelProcessor.SOURCE);
    //logger.debug( "JGitverModelProcessor.provisionModel source="+source );
    if (source == null) {
        return model;
    }

    File location = new File(source.getLocation());
    //logger.debug( "JGitverModelProcessor.provisionModel location="+location );
    if (!location.isFile()) {
        // their JavaDoc says Source.getLocation "could be a local file path, a URI or just an empty string."
        // if it doesn't resolve to a file then calling .getParentFile will throw an exception,
        // but if it doesn't resolve to a file then it isn't under getMultiModuleProjectDirectory,
        return model; // therefore the model shouldn't be modified.
    }

    File relativePath = location.getParentFile().getCanonicalFile();

    if (StringUtils.containsIgnoreCase(relativePath.getCanonicalPath(),
            workingConfiguration.getMultiModuleProjectDirectory().getCanonicalPath())) {

        workingConfiguration.getNewProjectVersions().put(GAV.from(model.clone()),
                workingConfiguration.getCalculatedVersion());

        if (Objects.nonNull(model.getVersion())) {
            // TODO evaluate how to set the version only when it was originally set in the pom file
            model.setVersion(workingConfiguration.getCalculatedVersion());
        }

        if (Objects.nonNull(model.getParent())) {
            // if the parent is part of the multi module project, let's update the parent version 
            File relativePathParent = new File(
                    relativePath.getCanonicalPath() + File.separator + model.getParent().getRelativePath())
                            .getParentFile().getCanonicalFile();
            if (StringUtils.containsIgnoreCase(relativePathParent.getCanonicalPath(),
                    workingConfiguration.getMultiModuleProjectDirectory().getCanonicalPath())) {
                model.getParent().setVersion(workingConfiguration.getCalculatedVersion());
            }
        }

        // we should only register the plugin once, on the main project
        if (relativePath.getCanonicalPath()
                .equals(workingConfiguration.getMultiModuleProjectDirectory().getCanonicalPath())) {
            if (Objects.isNull(model.getBuild())) {
                model.setBuild(new Build());
            }

            if (Objects.isNull(model.getBuild().getPlugins())) {
                model.getBuild().setPlugins(new ArrayList<>());
            }

            Optional<Plugin> pluginOptional = model.getBuild().getPlugins().stream()
                    .filter(x -> JGitverUtils.EXTENSION_GROUP_ID.equalsIgnoreCase(x.getGroupId())
                            && JGitverUtils.EXTENSION_ARTIFACT_ID.equalsIgnoreCase(x.getArtifactId()))
                    .findFirst();

            StringBuilder pluginVersion = new StringBuilder();

            try (InputStream inputStream = getClass()
                    .getResourceAsStream("/META-INF/maven/" + JGitverUtils.EXTENSION_GROUP_ID + "/"
                            + JGitverUtils.EXTENSION_ARTIFACT_ID + "/pom" + ".properties")) {
                Properties properties = new Properties();
                properties.load(inputStream);
                pluginVersion.append(properties.getProperty("version"));
            } catch (IOException ignored) {
                // TODO we should not ignore in case we have to reuse it
                logger.warn(ignored.getMessage(), ignored);
            }

            Plugin plugin = pluginOptional.orElseGet(() -> {
                Plugin plugin2 = new Plugin();
                plugin2.setGroupId(JGitverUtils.EXTENSION_GROUP_ID);
                plugin2.setArtifactId(JGitverUtils.EXTENSION_ARTIFACT_ID);
                plugin2.setVersion(pluginVersion.toString());

                model.getBuild().getPlugins().add(plugin2);
                return plugin2;
            });

            if (Objects.isNull(plugin.getExecutions())) {
                plugin.setExecutions(new ArrayList<>());
            }

            Optional<PluginExecution> pluginExecutionOptional = plugin.getExecutions().stream()
                    .filter(x -> "verify".equalsIgnoreCase(x.getPhase())).findFirst();

            PluginExecution pluginExecution = pluginExecutionOptional.orElseGet(() -> {
                PluginExecution pluginExecution2 = new PluginExecution();
                pluginExecution2.setPhase("verify");

                plugin.getExecutions().add(pluginExecution2);
                return pluginExecution2;
            });

            if (Objects.isNull(pluginExecution.getGoals())) {
                pluginExecution.setGoals(new ArrayList<>());
            }

            if (!pluginExecution.getGoals().contains(JGitverAttachModifiedPomsMojo.GOAL_ATTACH_MODIFIED_POMS)) {
                pluginExecution.getGoals().add(JGitverAttachModifiedPomsMojo.GOAL_ATTACH_MODIFIED_POMS);
            }

            if (Objects.isNull(plugin.getDependencies())) {
                plugin.setDependencies(new ArrayList<>());
            }

            Optional<Dependency> dependencyOptional = plugin.getDependencies().stream()
                    .filter(x -> JGitverUtils.EXTENSION_GROUP_ID.equalsIgnoreCase(x.getGroupId())
                            && JGitverUtils.EXTENSION_ARTIFACT_ID.equalsIgnoreCase(x.getArtifactId()))
                    .findFirst();

            dependencyOptional.orElseGet(() -> {
                Dependency dependency = new Dependency();
                dependency.setGroupId(JGitverUtils.EXTENSION_GROUP_ID);
                dependency.setArtifactId(JGitverUtils.EXTENSION_ARTIFACT_ID);
                dependency.setVersion(pluginVersion.toString());

                plugin.getDependencies().add(dependency);
                return dependency;
            });
        }

        try {
            legacySupport.getSession().getUserProperties().put(
                    JGitverModelProcessorWorkingConfiguration.class.getName(),
                    JGitverModelProcessorWorkingConfiguration.serializeTo(workingConfiguration));
        } catch (JAXBException ex) {
            throw new IOException("unexpected Model serialization issue", ex);
        }
    }

    return model;
}

From source file:org.kie.workbench.common.services.backend.compiler.external339.AFConfigurationProcessor.java

License:Apache License

private Object getLocation(Source source, File defaultLocation) {
    return source != null ? source.getLocation() : defaultLocation;
}

From source file:org.kie.workbench.common.services.backend.compiler.external339.AFMavenCli.java

License:Apache License

protected Object getLocation(Source source, Path defaultLocation) {
    if (source != null) {
        return source.getLocation();
    }//from w w w.j  a v a2 s .c o  m
    return defaultLocation.toString();
}

From source file:org.kie.workbench.common.services.backend.compiler.external339.AFSettingsXmlConfigurationProcessor.java

License:Apache License

private Object getLocation(Source source, Path defaultLocation) {
    if (source != null) {
        return source.getLocation();
    }// www.j ava 2s.c  o m
    return defaultLocation;
}