Skip to content

Conversation

@Pankraz76
Copy link

@Pankraz76 Pankraz76 commented May 17, 2025

image

mvn pmd:check > pmd.txt

workaround:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.impl;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;

public class ExcludeFromFailureFile {

    private static final Pattern PMD_PATTERN = Pattern.compile(
            "PMD Failure: ([\\w\\.]+):(\\d+) Rule:([\\w]+) Priority:\\d+");

    public static void main(String[] args) throws IOException {
        Path logPath = Paths.get("pmd.txt"); // path to pmd.log
        Path propsPath = Paths.get(".pmd/exclude.properties"); // path to exclude.properties

        // Load existing properties
        Properties excludeProps = new Properties();
        if (Files.exists(propsPath)) {
            try (InputStream in = Files.newInputStream(propsPath)) {
                excludeProps.load(in);
            }
        }

        // Parse PMD log and collect new rules
        Map<String, Set<String>> newEntries = new HashMap<>();

        try (BufferedReader reader = Files.newBufferedReader(logPath)) {
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher matcher = PMD_PATTERN.matcher(line);
                if (matcher.find()) {
                    String className = matcher.group(1);
                    String rule = matcher.group(3);

                    newEntries.computeIfAbsent(className, k -> new HashSet<>()).add(rule);
                }
            }
        }

        // Merge with existing properties
        for (Map.Entry<String, Set<String>> entry : newEntries.entrySet()) {
            String key = entry.getKey();
            Set<String> newRules = entry.getValue();

            String existing = excludeProps.getProperty(key);
            Set<String> merged = new TreeSet<>(newRules); // TreeSet to sort and deduplicate

            if (existing != null && !existing.isEmpty()) {
                merged.addAll(Arrays.asList(existing.split(",")));
            }

            excludeProps.setProperty(key, String.join(",", merged));
        }

        // Ensure output directory exists
        Files.createDirectories(propsPath.getParent());

        // Write back merged properties
        try (OutputStream out = Files.newOutputStream(propsPath)) {
            excludeProps.store(out, "Merged PMD exclusions");
        }

        System.out.println("Exclude file updated successfully.");
    }
}

private void logExcludeFromFailureFileSuppressions() {
log.info("logExcludeFromFailureFileSuppressions");
log.info(violations.stream()
.map(violation -> String.format("%s:%s", violation.getViolationClass(), violation.getRule()))
Copy link
Author

@Pankraz76 Pankraz76 May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(violation -> String.format("%s:%s", violation.getViolationClass(), violation.getRule()))
.map(violation -> String.format("%s=%s", violation.getViolationClass(), violation.getRule()))

collision: (only works for one)

org.apache.maven.building.DefaultProblem=MissingOverride
org.apache.maven.model.merge.MavenModelMerger=ForLoopVariableCount
org.apache.maven.model.merge.MavenModelMerger=OneDeclarationPerLine

merge:

org.apache.maven.building.DefaultProblem=MissingOverride
org.apache.maven.model.merge.MavenModelMerger=ForLoopVariableCount,OneDeclarationPerLine

@Pankraz76
Copy link
Author

Pankraz76 commented May 17, 2025

is this any good @oowekyala @adangel?

A simple log to support manual c&p would give quick fix, without breaking anything as just logging.

Then writing the keys into file could be next step, but not needed anymore. This is unique action, need to be done only once. So automation would be questionable.

@Pankraz76 Pankraz76 changed the title [POC]: Issue#5735 support legacy automating excludeFromFailureFile [POC-Issue#5735]: support legacy automating excludeFromFailureFile May 17, 2025
@Pankraz76 Pankraz76 changed the title [POC-Issue#5735]: support legacy automating excludeFromFailureFile [POC-Issue#5735]: support legacy automate excludeFromFailureFile May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant