Search in sources :

Example 21 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class PropertiesUtils method internalUpdateProperty.

private static PropertyUpdate internalUpdateProperty(ManipulationSession session, Project p, boolean ignoreStrict, String key, String newValue, String resolvedValue, Properties props) throws ManipulationException {
    final CommonState state = session.getState(CommonState.class);
    final String oldValue = props.getProperty(key);
    logger.info("Updating property {} / {} with {} ", key, oldValue, newValue);
    PropertyUpdate found = PropertyUpdate.FOUND;
    // update it with a portion of the new value.
    if (oldValue != null && oldValue.startsWith("${") && oldValue.endsWith("}") && !(StringUtils.countMatches(oldValue, "${") > 1)) {
        logger.debug("Recursively resolving {} ", oldValue.substring(2, oldValue.length() - 1));
        if (updateProperties(session, p, ignoreStrict, oldValue.substring(2, oldValue.length() - 1), newValue) == PropertyUpdate.NOTFOUND) {
            logger.error("Recursive property not found for {} with {} ", oldValue, newValue);
            return PropertyUpdate.NOTFOUND;
        }
    } else {
        if (state.getStrict() && !ignoreStrict) {
            if (!checkStrictValue(session, resolvedValue, newValue)) {
                if (state.getFailOnStrictViolation()) {
                    throw new ManipulationException("Replacing original property version {} (fully resolved: {} ) with new version {} for {} violates the strict version-alignment rule!", oldValue, resolvedValue, newValue, key);
                } else {
                    logger.warn("Replacing original property version {} with new version {} for {} violates the strict version-alignment rule!", oldValue, newValue, key);
                    // a new property either.
                    return found;
                }
            }
        }
        // TODO: Does not handle explicit overrides.
        if (oldValue != null && oldValue.contains("${") && !(oldValue.startsWith("${") && oldValue.endsWith("}")) || (StringUtils.countMatches(oldValue, "${") > 1)) {
            if (ignoreStrict) {
                throw new ManipulationException("NYI : handling for versions with explicit overrides (" + oldValue + ") with multiple embedded properties is NYI. ");
            }
            if (resolvedValue.equals(newValue)) {
                logger.warn("Nothing to update as original key {} value matches new value {} ", key, newValue);
                found = PropertyUpdate.IGNORE;
            }
            newValue = oldValue + StringUtils.removeStart(newValue, resolvedValue);
            logger.info("Ignoring new value due to embedded property {} and appending {} ", oldValue, newValue);
        }
        props.setProperty(key, newValue);
    }
    return found;
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) ManipulationException(org.commonjava.maven.ext.common.ManipulationException)

Example 22 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class PropertiesUtils method cacheProperty.

/**
 * This will check if the old version (e.g. in a plugin or dependency) is a property and if so
 * store the mapping in a map.
 *
 * @param project the current project the needs to cache the value.
 * @param state CommonState to retrieve property clash value QoS.
 * @param versionPropertyUpdateMap the map to store any updates in
 * @param oldVersion original property value
 * @param newVersion new property value
 * @param originalType that this property is used in (i.e. a plugin or a dependency)
 * @param force Whether to check for an existing property or force the insertion
 * @return true if a property was found and cached.
 * @throws ManipulationException if an error occurs.
 */
public static boolean cacheProperty(Project project, CommonState state, Map<Project, Map<String, String>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force) throws ManipulationException {
    boolean result = false;
    Map<String, String> projectProps = versionPropertyUpdateMap.get(project);
    if (projectProps == null) {
        versionPropertyUpdateMap.put(project, (projectProps = new HashMap<>()));
    }
    if (oldVersion != null && oldVersion.contains("${")) {
        final int endIndex = oldVersion.indexOf('}');
        final String oldProperty = oldVersion.substring(2, endIndex);
        // combined with a hardcoded value.
        if (oldVersion.contains("${") && !(oldVersion.startsWith("${") && oldVersion.endsWith("}")) || (StringUtils.countMatches(oldVersion, "${") > 1)) {
            logger.debug("For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion);
        } else if ("project.version".equals(oldProperty)) {
            logger.debug("For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion);
        } else {
            logger.debug("For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project);
            final String oldVersionProp = oldVersion.substring(2, oldVersion.length() - 1);
            // We check if we are replacing a property and there is already a mapping. While we don't allow
            // a property to be updated to two different versions, if a dependencyExclusion (i.e. a force override)
            // has been specified this will bypass the check.
            String existingPropertyMapping = projectProps.get(oldVersionProp);
            if (existingPropertyMapping != null && !existingPropertyMapping.equals(newVersion)) {
                if (force) {
                    logger.debug("Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion);
                } else {
                    if (state.getPropertyClashFails()) {
                        logger.error("Replacing property '{}' with a new version but the existing version does not match. Old value is {} and new is {}", oldVersionProp, existingPropertyMapping, newVersion);
                        throw new ManipulationException("Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion);
                    } else {
                        logger.warn("Replacing property '{}' with a new version would clash with existing version which does not match. Old value is {} and new is {}. Purging update of existing property.", oldVersionProp, existingPropertyMapping, newVersion);
                        projectProps.remove(oldVersionProp);
                        return false;
                    }
                }
            }
            projectProps.put(oldVersionProp, newVersion);
            result = true;
        }
    }
    return result;
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException)

Example 23 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class Project method resolveDeps.

private void resolveDeps(MavenSessionHandler session, List<Dependency> deps, boolean includeManagedDependencies, HashMap<ArtifactRef, Dependency> resolvedDependencies) throws ManipulationException {
    ListIterator<Dependency> iterator = deps.listIterator(deps.size());
    // Iterate in reverse order so later deps take precedence
    while (iterator.hasPrevious()) {
        Dependency d = iterator.previous();
        String g = PropertyResolver.resolveInheritedProperties(session, this, "${project.groupId}".equals(d.getGroupId()) ? getGroupId() : d.getGroupId());
        String a = PropertyResolver.resolveInheritedProperties(session, this, "${project.artifactId}".equals(d.getArtifactId()) ? getArtifactId() : d.getArtifactId());
        String v = PropertyResolver.resolveInheritedProperties(session, this, d.getVersion());
        if (includeManagedDependencies && isEmpty(v)) {
            v = "*";
        }
        if (isNotEmpty(g) && isNotEmpty(a) && isNotEmpty(v)) {
            SimpleArtifactRef sar = new SimpleArtifactRef(g, a, v, d.getType(), d.getClassifier());
            // the indexing as we don't have duplicate entries. Given they are exact matches, remove older duplicate.
            if (resolvedDependencies.containsKey(sar)) {
                logger.error("Found duplicate entry within dependency list. Key of {} and dependency {}", sar, d);
                iterator.remove();
            } else {
                Dependency old = resolvedDependencies.put(sar, d);
                if (old != null) {
                    logger.error("Internal project dependency resolution failure ; replaced {} in store by {}:{}:{}.", old, g, a, v);
                    throw new ManipulationException("Internal project dependency resolution failure ; replaced " + old + " by " + d);
                }
            }
        }
    }
}
Also used : SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Dependency(org.apache.maven.model.Dependency)

Example 24 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class Project method resolvePlugins.

private void resolvePlugins(MavenSessionHandler session, List<Plugin> plugins, HashMap<ProjectVersionRef, Plugin> resolvedPlugins) throws ManipulationException {
    ListIterator<Plugin> iterator = plugins.listIterator(plugins.size());
    // Iterate in reverse order so later plugins take precedence
    while (iterator.hasPrevious()) {
        Plugin p = iterator.previous();
        String g = PropertyResolver.resolveInheritedProperties(session, this, "${project.groupId}".equals(p.getGroupId()) ? getGroupId() : p.getGroupId());
        String a = PropertyResolver.resolveInheritedProperties(session, this, "${project.artifactId}".equals(p.getArtifactId()) ? getArtifactId() : p.getArtifactId());
        String v = PropertyResolver.resolveInheritedProperties(session, this, p.getVersion());
        // comparison purposes.
        if (isEmpty(g)) {
            g = PLUGIN_DEFAULTS.getDefaultGroupId(a);
        }
        // this means managed plugins would be included which confuses things.
        if (isNotEmpty(g) && isNotEmpty(a) && isNotEmpty(v)) {
            SimpleProjectVersionRef spv = new SimpleProjectVersionRef(g, a, v);
            // the indexing as we don't have duplicate entries. Given they are exact matches, remove older duplicate.
            if (resolvedPlugins.containsKey(spv)) {
                logger.error("Found duplicate entry within plugin list. Key of {} and plugin {}", spv, p);
                iterator.remove();
            } else {
                Plugin old = resolvedPlugins.put(spv, p);
                if (old != null) {
                    logger.error("Internal project plugin resolution failure ; replaced {} in store by {}.", old, spv);
                    throw new ManipulationException("Internal project plugin resolution failure ; replaced " + old + " by " + spv);
                }
            }
        }
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) Plugin(org.apache.maven.model.Plugin)

Example 25 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class Cli method run.

public int run(String[] args) {
    Options options = new Options();
    options.addOption("h", false, "Print this help message.");
    options.addOption(Option.builder("d").longOpt("debug").desc("Enable debug").build());
    options.addOption(Option.builder("t").longOpt("trace").desc("Enable trace").build());
    options.addOption(Option.builder("h").longOpt("help").desc("Print help").build());
    options.addOption(Option.builder("f").longOpt("file").hasArgs().numberOfArgs(1).desc("POM file").build());
    options.addOption(Option.builder().longOpt("log-context").desc("Add log-context ID").numberOfArgs(1).build());
    options.addOption(Option.builder("l").longOpt("log").desc("Log file to output logging to").numberOfArgs(1).build());
    options.addOption(Option.builder("s").longOpt("settings").hasArgs().numberOfArgs(1).desc("Optional settings.xml file").build());
    options.addOption(Option.builder("P").longOpt("activeProfiles").desc("Comma separated list of active profiles.").numberOfArgs(1).build());
    options.addOption(Option.builder("o").longOpt("outputFile").desc("outputFile to output dependencies to. Only used with '-p' (Print all project dependencies)").numberOfArgs(1).build());
    options.addOption(Option.builder("p").longOpt("printDeps").desc("Print all project dependencies").build());
    options.addOption(Option.builder().longOpt("printGAVTC").desc("Print all project dependencies in group:artifact:version:type:classifier with scope information").build());
    options.addOption(Option.builder("D").hasArgs().numberOfArgs(2).valueSeparator('=').desc("Java Properties").build());
    options.addOption(Option.builder("x").hasArgs().numberOfArgs(2).desc("XPath tester ( file : xpath )").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        logger.debug("Caught problem parsing ", e);
        System.err.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("...", options);
        return 10;
    }
    if (cmd.hasOption('h')) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("...", options);
        System.exit(0);
    }
    if (cmd.hasOption('D')) {
        userProps = cmd.getOptionProperties("D");
    }
    if (cmd.hasOption('f')) {
        target = new File(cmd.getOptionValue('f'));
    }
    if (cmd.hasOption('s')) {
        settings = new File(cmd.getOptionValue('s'));
    }
    if (cmd.hasOption("log-context")) {
        String mdc = cmd.getOptionValue("log-context");
        if (isNotEmpty(mdc)) {
            // Append a space to split up level and log-context markers.
            MDC.put("LOG-CONTEXT", mdc + ' ');
        }
    }
    createSession(target, settings);
    final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    if (cmd.hasOption('l')) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.reset();
        PatternLayoutEncoder ple = new PatternLayoutEncoder();
        ple.setPattern("%mdc{LOG-CONTEXT}%level %logger{36} %msg%n");
        ple.setContext(loggerContext);
        ple.start();
        FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
        fileAppender.setEncoder(ple);
        fileAppender.setContext(loggerContext);
        fileAppender.setName("fileLogging");
        fileAppender.setAppend(false);
        fileAppender.setFile(cmd.getOptionValue("l"));
        fileAppender.start();
        root.addAppender(fileAppender);
        root.setLevel(Level.INFO);
    }
    // creation stuff.
    if (cmd.hasOption('d')) {
        root.setLevel(Level.DEBUG);
    }
    if (cmd.hasOption('t')) {
        root.setLevel(Level.TRACE);
    }
    if (!session.isEnabled()) {
        logger.info("Manipulation engine disabled via command-line option");
        return 0;
    }
    if (!target.exists()) {
        logger.info("Manipulation engine disabled. Project {} cannot be found.", target);
        return 10;
    } else // Don't bother skipping if we're just trying to analyse deps.
    if (new File(target.getParentFile(), ManipulationManager.MARKER_FILE).exists() && !cmd.hasOption('p')) {
        logger.info("Skipping manipulation as previous execution found.");
        return 0;
    }
    try {
        Properties config = new ConfigIO().parse(target.getParentFile());
        String value = session.getUserProperties().getProperty("allowConfigFilePrecedence");
        if (isNotEmpty(value) && "true".equalsIgnoreCase(value)) {
            session.getUserProperties().putAll(config);
        } else {
            for (String key : config.stringPropertyNames()) {
                if (!session.getUserProperties().containsKey(key)) {
                    session.getUserProperties().setProperty(key, config.getProperty(key));
                }
            }
        }
    } catch (ManipulationException e) {
        logger.error("POM Manipulation failed: Unable to read config file ", e);
        return 10;
    }
    try {
        // Note : don't print out settings information earlier (like when we actually read it) as the logging
        // isn't setup then.
        logger.debug("Using local repository \n{} and found global settings file in {} with contents \n{} and user settings file in {} with contents \n{}", session.getLocalRepository(), DEFAULT_GLOBAL_SETTINGS_FILE, DEFAULT_GLOBAL_SETTINGS_FILE.exists() ? FileUtils.readFileToString(DEFAULT_GLOBAL_SETTINGS_FILE) : "** File does not exist **", settings, (settings != null && settings.exists()) ? FileUtils.readFileToString(settings) : "** File does not exist **");
        manipulationManager.init(session);
        Set<String> activeProfiles = null;
        if (cmd.hasOption('P')) {
            activeProfiles = new HashSet<>();
            Collections.addAll(activeProfiles, cmd.getOptionValue('P').trim().split(","));
            session.getUserProperties().setProperty(PROFILE_SCANNING, "true");
            session.getActiveProfiles().addAll(activeProfiles);
        }
        if (cmd.hasOption('x')) {
            String[] params = cmd.getOptionValues('x');
            if (params.length != 2) {
                throw new ManipulationException("Invalid number of parameters (" + params.length + "); should be <file> <xpath>");
            }
            XMLIO xmlIO = new XMLIO();
            Document doc = xmlIO.parseXML(new File(params[0]));
            XPath xPath = XPathFactory.newInstance().newXPath();
            NodeList nodeList = (NodeList) xPath.evaluate(params[1], doc, XPathConstants.NODESET);
            logger.info("Found {} node", nodeList.getLength());
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                logger.info("Found node {} and value {} ", node.getNodeName(), node.getTextContent());
            }
        } else if (cmd.hasOption('p') || cmd.hasOption("printGAVTC")) {
            Set<ArtifactRef> ts = RESTCollector.establishAllDependencies(session, pomIO.parseProject(session.getPom()), activeProfiles);
            logger.info("Found {} dependencies. {}", ts.size(), ts);
            File output = null;
            if (cmd.hasOption('o')) {
                output = new File(cmd.getOptionValue('o'));
                output.delete();
            }
            for (ArtifactRef a : ts) {
                String scope = null;
                if (a instanceof SimpleScopedArtifactRef) {
                    scope = ((SimpleScopedArtifactRef) a).getScope();
                }
                if (cmd.hasOption('o')) {
                    if (cmd.hasOption("printGAVTC")) {
                        FileUtils.writeStringToFile(output, String.format("%-80s%10s\n", a, scope), true);
                    } else {
                        FileUtils.writeStringToFile(output, a.asProjectVersionRef().toString() + '\n', true);
                    }
                } else {
                    if (cmd.hasOption("printGAVTC")) {
                        System.out.format("%-80s%10s\n", a, scope);
                    } else {
                        System.out.println(a.asProjectVersionRef());
                    }
                }
            }
        } else {
            manipulationManager.scanAndApply(session);
        }
    } catch (ManipulationException e) {
        logger.error("POM Manipulation failed; original error is {}", e.getMessage());
        logger.debug("POM Manipulation error trace is", e);
        return 10;
    } catch (RestException e) {
        logger.error("REST communication with {} failed. {}", userProps.getProperty("restURL"), e.getMessage());
        logger.trace("Exception trace is", e);
        return 100;
    } catch (Exception e) {
        logger.error("POM Manipulation failed.", e);
        return 100;
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) Set(java.util.Set) HashSet(java.util.HashSet) Node(org.w3c.dom.Node) Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Properties(java.util.Properties) Document(org.w3c.dom.Document) ConfigIO(org.commonjava.maven.ext.io.ConfigIO) SimpleScopedArtifactRef(org.commonjava.maven.ext.common.model.SimpleScopedArtifactRef) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) XPath(javax.xml.xpath.XPath) XMLIO(org.commonjava.maven.ext.io.XMLIO) NodeList(org.w3c.dom.NodeList) SimpleScopedArtifactRef(org.commonjava.maven.ext.common.model.SimpleScopedArtifactRef) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) LoggerContext(ch.qos.logback.classic.LoggerContext) PlexusContainerException(org.codehaus.plexus.PlexusContainerException) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) ParseException(org.apache.commons.cli.ParseException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) CommandLine(org.apache.commons.cli.CommandLine) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Aggregations

ManipulationException (org.commonjava.maven.ext.common.ManipulationException)42 IOException (java.io.IOException)13 File (java.io.File)11 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedHashMap (java.util.LinkedHashMap)7 Project (org.commonjava.maven.ext.common.model.Project)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)6 Properties (java.util.Properties)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)5 JsonPathException (com.jayway.jsonpath.JsonPathException)4 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)4 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)4 CommonState (org.commonjava.maven.ext.core.state.CommonState)4 Test (org.junit.Test)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 FileInputStream (java.io.FileInputStream)3 Dependency (org.apache.maven.model.Dependency)3