[ https://issues.apache.org/jira/browse/MPMD-379?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17834698#comment-17834698 ]
ASF GitHub Bot commented on MPMD-379: ------------------------------------- michael-o commented on code in PR #144: URL: https://github.com/apache/maven-pmd-plugin/pull/144#discussion_r1555006832 ########## src/it/MPMD-379-JDK21/invoker.properties: ########## @@ -0,0 +1,28 @@ +# 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. + +invoker.java.version = 1.8+ Review Comment: This one is redundant because it won't run below 8 anyway. ########## src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java: ########## @@ -155,46 +149,50 @@ private CpdResult run() throws MavenReportException { CPDConfiguration cpdConfiguration = new CPDConfiguration(); cpdConfiguration.setMinimumTileSize(request.getMinimumTokens()); + cpdConfiguration.setIgnoreAnnotations(request.isIgnoreAnnotations()); + cpdConfiguration.setIgnoreLiterals(request.isIgnoreLiterals()); + cpdConfiguration.setIgnoreIdentifiers(request.isIgnoreIdentifiers()); - Language cpdLanguage; - if ("java".equals(request.getLanguage()) || null == request.getLanguage()) { - cpdLanguage = new JavaLanguage(request.getLanguageProperties()); - } else if ("javascript".equals(request.getLanguage())) { - cpdLanguage = new EcmascriptLanguage(); - } else if ("jsp".equals(request.getLanguage())) { - cpdLanguage = new JSPLanguage(); - } else { - cpdLanguage = LanguageFactory.createLanguage(request.getLanguage(), request.getLanguageProperties()); + String languageId = request.getLanguage(); + if ("javascript".equals(languageId)) { + languageId = "ecmascript"; + } else if (languageId == null) { + languageId = "java"; // default } + Language cpdLanguage = cpdConfiguration.getLanguageRegistry().getLanguageById(languageId); - cpdConfiguration.setLanguage(cpdLanguage); - cpdConfiguration.setSourceEncoding(request.getSourceEncoding()); + cpdConfiguration.setOnlyRecognizeLanguage(cpdLanguage); + cpdConfiguration.setSourceEncoding(Charset.forName(request.getSourceEncoding())); - CPD cpd = new CPD(cpdConfiguration); - try { - cpd.add(request.getFiles()); - } catch (IOException e) { - throw new MavenReportException(e.getMessage(), e); - } + request.getFiles().forEach(f -> cpdConfiguration.addInputPath(f.toPath())); LOG.debug("Executing CPD..."); - cpd.go(); - LOG.debug("CPD finished."); // always create XML format. we need to output it even if the file list is empty or we have no duplications // so the "check" goals can check for violations - writeXmlReport(cpd); + try (CpdAnalysis cpd = CpdAnalysis.create(cpdConfiguration)) { + cpd.performAnalysis(report -> { + try { + writeXmlReport(report); - // html format is handled by maven site report, xml format has already been rendered - String format = request.getFormat(); - if (!"html".equals(format) && !"xml".equals(format)) { - writeFormattedReport(cpd); + // html format is handled by maven site report, xml format has already been rendered + String format = request.getFormat(); + if (!"html".equals(format) && !"xml".equals(format)) { + writeFormattedReport(report); + } + } catch (MavenReportException e) { + LOG.error(e.getMessage(), e); Review Comment: This produces duplicate messages. Please provide a reasonable message and then pass `e` ########## src/it/MPMD-379-JDK21/invoker.properties: ########## @@ -0,0 +1,28 @@ +# 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. + +invoker.java.version = 1.8+ + +# available toolchains under linux: +# https://github.com/apache/infrastructure-p6/blob/production/modules/build_nodes/files/toolchains.xml + +# the jdk toolchain "21:openjdk" is selected in pom.xml +invoker.toolchain.jdk.version = 21 +invoker.toolchain.jdk.vendor = openjdk Review Comment: Really only `openjdk`? ########## src/main/java/org/apache/maven/plugins/pmd/PmdReport.java: ########## @@ -63,16 +63,19 @@ @Mojo(name = "pmd", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST) public class PmdReport extends AbstractPmdReport { /** - * The target JDK to analyze based on. Should match the source used in the compiler plugin. Valid values - * with the default PMD version are + * The target JDK to analyze based on. Should match the source used in the compiler plugin. + * Valid values depend on the used PMD version. With the default PMD version valid values are * currently <code>1.3</code>, <code>1.4</code>, <code>1.5</code>, <code>1.6</code>, <code>1.7</code>, * <code>1.8</code>, <code>9</code>, <code>10</code>, <code>11</code>, <code>12</code>, <code>13</code>, * <code>14</code>, <code>15</code>, <code>16</code>, <code>17</code>, <code>18</code>, <code>19</code>, - * and <code>20</code>. + * <code>20</code>, <code>21</code>, and <code>22</code>. Review Comment: Are those old version before 7 still supported? ########## src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java: ########## @@ -155,46 +149,50 @@ private CpdResult run() throws MavenReportException { CPDConfiguration cpdConfiguration = new CPDConfiguration(); cpdConfiguration.setMinimumTileSize(request.getMinimumTokens()); + cpdConfiguration.setIgnoreAnnotations(request.isIgnoreAnnotations()); + cpdConfiguration.setIgnoreLiterals(request.isIgnoreLiterals()); + cpdConfiguration.setIgnoreIdentifiers(request.isIgnoreIdentifiers()); - Language cpdLanguage; - if ("java".equals(request.getLanguage()) || null == request.getLanguage()) { - cpdLanguage = new JavaLanguage(request.getLanguageProperties()); - } else if ("javascript".equals(request.getLanguage())) { - cpdLanguage = new EcmascriptLanguage(); - } else if ("jsp".equals(request.getLanguage())) { - cpdLanguage = new JSPLanguage(); - } else { - cpdLanguage = LanguageFactory.createLanguage(request.getLanguage(), request.getLanguageProperties()); + String languageId = request.getLanguage(); + if ("javascript".equals(languageId)) { + languageId = "ecmascript"; + } else if (languageId == null) { + languageId = "java"; // default } + Language cpdLanguage = cpdConfiguration.getLanguageRegistry().getLanguageById(languageId); - cpdConfiguration.setLanguage(cpdLanguage); - cpdConfiguration.setSourceEncoding(request.getSourceEncoding()); + cpdConfiguration.setOnlyRecognizeLanguage(cpdLanguage); + cpdConfiguration.setSourceEncoding(Charset.forName(request.getSourceEncoding())); - CPD cpd = new CPD(cpdConfiguration); - try { - cpd.add(request.getFiles()); - } catch (IOException e) { - throw new MavenReportException(e.getMessage(), e); - } + request.getFiles().forEach(f -> cpdConfiguration.addInputPath(f.toPath())); LOG.debug("Executing CPD..."); - cpd.go(); - LOG.debug("CPD finished."); // always create XML format. we need to output it even if the file list is empty or we have no duplications // so the "check" goals can check for violations - writeXmlReport(cpd); + try (CpdAnalysis cpd = CpdAnalysis.create(cpdConfiguration)) { + cpd.performAnalysis(report -> { + try { + writeXmlReport(report); - // html format is handled by maven site report, xml format has already been rendered - String format = request.getFormat(); - if (!"html".equals(format) && !"xml".equals(format)) { - writeFormattedReport(cpd); + // html format is handled by maven site report, xml format has already been rendered + String format = request.getFormat(); + if (!"html".equals(format) && !"xml".equals(format)) { + writeFormattedReport(report); + } + } catch (MavenReportException e) { + LOG.error(e.getMessage(), e); + } + }); + } catch (IOException e) { + LOG.error("Error while executing CPD: {}", e.getMessage(), e); Review Comment: Duplicate message, remove `e.getMessage()` > Upgrade to use PMD 7.0.0 by default > ----------------------------------- > > Key: MPMD-379 > URL: https://issues.apache.org/jira/browse/MPMD-379 > Project: Maven PMD Plugin > Issue Type: Improvement > Components: CPD, PMD > Reporter: Andreas Dangel > Assignee: Andreas Dangel > Priority: Major > > Add support for the new major version of PMD. > This gives support for analyzing Java 21 code. > The upgrade from PMD 6 to PMD 7 is a major upgrade, that might impact > end-users, if they use custom rulesets (see > [https://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html]) > or if they override the dependencies to upgrade PMD at runtime and currently > use PMD 6.x (see > [https://maven.apache.org/plugins/maven-pmd-plugin/examples/upgrading-PMD-at-runtime.html]). > > Most likely, end-users have to review their rulesets and migrate them to PMD > 7. Rules might have been renamed or replaced. See > [https://docs.pmd-code.org/latest/pmd_release_notes_pmd7.html] and > [https://docs.pmd-code.org/latest/pmd_userdocs_migrating_to_pmd7.html] . > -- This message was sent by Atlassian Jira (v8.20.10#820010)