From fcc7c8c52306b651e57201174b62e33c8d250446 Mon Sep 17 00:00:00 2001 From: opencode Date: Wed, 29 Jul 2026 13:24:38 +0000 Subject: [PATCH] [MSHADE-434] Restore project.getFile() after dependency-reduced POM creation Calling project.setFile(dependencyReducedPomLocation) changes the project file reference for subsequent plugins, causing them to resolve relative paths against the DRP directory (e.g., target/) instead of the project basedir. This breaks plugins like apache-rat-plugin and checkstyle that rely on project.getFile() for path resolution. Save and restore the original project file around DRP creation to prevent this side-effect. Includes integration test MSHADE-434_ratSideEffect that verifies project.file still points to the original pom.xml after shading. --- .../projects/MSHADE-434_ratSideEffect/pom.xml | 85 +++++++++++++++++++ .../MSHADE-434_ratSideEffect/verify.groovy | 41 +++++++++ .../maven/plugins/shade/mojo/ShadeMojo.java | 2 + 3 files changed, 128 insertions(+) create mode 100644 src/it/projects/MSHADE-434_ratSideEffect/pom.xml create mode 100644 src/it/projects/MSHADE-434_ratSideEffect/verify.groovy diff --git a/src/it/projects/MSHADE-434_ratSideEffect/pom.xml b/src/it/projects/MSHADE-434_ratSideEffect/pom.xml new file mode 100644 index 00000000..fdca43d3 --- /dev/null +++ b/src/it/projects/MSHADE-434_ratSideEffect/pom.xml @@ -0,0 +1,85 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade + mshade-434 + 1.0 + jar + + MSHADE-434 + + Test that project.getFile() is restored to the original pom.xml after shading, + preventing side effects on subsequent plugins (e.g., RAT, checkstyle). + + + + + org.apache.maven.its.shade.fac + a + 0.1 + + + + + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + shade + package + + shade + + + false + true + ${project.build.directory}/dependency-reduced-pom.xml + + + + + + org.apache.maven.plugins + maven-help-plugin + 3.3.0 + + + check-project-file + package + + evaluate + + + project.file + true + + + + + + + diff --git a/src/it/projects/MSHADE-434_ratSideEffect/verify.groovy b/src/it/projects/MSHADE-434_ratSideEffect/verify.groovy new file mode 100644 index 00000000..46474c2a --- /dev/null +++ b/src/it/projects/MSHADE-434_ratSideEffect/verify.groovy @@ -0,0 +1,41 @@ +/* + * 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. + */ + +// Verify the dependency-reduced POM was created in the expected location +File drpFile = new File(basedir, "target/dependency-reduced-pom.xml") +assert drpFile.isFile() : "DRP should exist at " + drpFile + +// Verify the shaded JAR was created +File shadedJar = new File(basedir, "target/mshade-434-1.0.jar") +assert shadedJar.isFile() : "Shaded JAR should exist at " + shadedJar + +// Verify that project.getFile() was restored to the original pom.xml after shading +// by checking the build log for the help:evaluate output +File buildLog = new File(basedir, "../build.log") +if (!buildLog.isFile()) { + buildLog = new File(basedir, "build.log") +} +assert buildLog.isFile() : "build.log should exist" + +def lines = buildLog.readLines() +// help:evaluate with forceStdout prints the expression value on its own line +// Look for a bare absolute path that ends with "/pom.xml" (not "dependency-reduced-pom.xml") +def projectFileLine = lines.find { it =~ /^\// && it.endsWith("/pom.xml") } +assert projectFileLine != null : "project.file value not found in build log (expected a path ending with /pom.xml)" +assert projectFileLine.endsWith("/pom.xml") : "project.file should be pom.xml, but was: " + projectFileLine diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java index 09e3956f..644ca93c 100644 --- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java +++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java @@ -544,6 +544,7 @@ public void execute() throws MojoExecutionException { List resourceTransformers = getResourceTransformers(); if (createDependencyReducedPom) { + File originalProjectFile = project.getFile(); createDependencyReducedPom(artifactIds); if (useDependencyReducedPomInJar) { @@ -551,6 +552,7 @@ public void execute() throws MojoExecutionException { resourceTransformers = new ArrayList<>(resourceTransformers); resourceTransformers.addAll(createPomReplaceTransformers(project, dependencyReducedPomLocation)); } + project.setFile(originalProjectFile); } ShadeRequest shadeRequest =