This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-jdeps-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new 14a474e Add PATH fallback when JAVA_HOME is unset (#72)
14a474e is described below
commit 14a474ef441e85b152fbb629406e086fcecd9d9f
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Dec 26 11:01:25 2025 +0100
Add PATH fallback when JAVA_HOME is unset (#72)
* Add PATH fallback when JAVA_HOME is unset
- Modified AbstractJDepsMojo.getJDepsExecutable() to search for jdeps in
PATH when JAVA_HOME is not set or jdeps is not found in JAVA_HOME
- Changed JAVA_HOME from required to optional
- Improved error message to inform users about both JAVA_HOME and PATH
options
- Added integration test to verify PATH fallback functionality works
correctly
This change allows the plugin to work in environments where JAVA_HOME is
not set,
as long as jdeps is available in the system PATH, which is common in many
development environments and CI/CD systems.
Fixes: Support for running jdeps when JAVA_HOME is not configured
* Update src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
Co-authored-by: Copilot <[email protected]>
* Update src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
Co-authored-by: Copilot <[email protected]>
* Resolve PR review comments: improve PATH lookup and integration test
- Reuse env Properties object to avoid duplicate retrieval
- Use canonical paths consistently for both JAVA_HOME and PATH lookups
- Add check to skip empty PATH entries
- Add canExecute() verification for executable permissions
- Improve error message to be more generic
- Unset JAVA_HOME in integration test to properly test PATH fallback
* Update src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
Co-authored-by: Copilot <[email protected]>
* Update src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
Co-authored-by: Copilot <[email protected]>
---------
Co-authored-by: GitHub Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
---
src/it/path-fallback/invoker.properties | 20 ++++++++
src/it/path-fallback/pom.xml | 55 ++++++++++++++++++++++
src/it/path-fallback/src/main/java/Main.java | 24 ++++++++++
src/it/path-fallback/verify.groovy | 32 +++++++++++++
.../maven/plugins/jdeps/AbstractJDepsMojo.java | 52 +++++++++++++++-----
5 files changed, 171 insertions(+), 12 deletions(-)
diff --git a/src/it/path-fallback/invoker.properties
b/src/it/path-fallback/invoker.properties
new file mode 100644
index 0000000..1638836
--- /dev/null
+++ b/src/it/path-fallback/invoker.properties
@@ -0,0 +1,20 @@
+# 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.goals = clean compile jdeps:jdkinternals
+invoker.debug = true
+invoker.environmentVariables.JAVA_HOME=
diff --git a/src/it/path-fallback/pom.xml b/src/it/path-fallback/pom.xml
new file mode 100644
index 0000000..442678e
--- /dev/null
+++ b/src/it/path-fallback/pom.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.plugins.jdeps.it</groupId>
+ <artifactId>path-fallback</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <description>
+ Verify jdeps plugin works when JAVA_HOME is not set but jdeps is available
in PATH
+ </description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <maven.compiler.source>1.8</maven.compiler.source>
+ <maven.compiler.target>1.8</maven.compiler.target>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jdeps-plugin</artifactId>
+ <version>@project.version@</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>jdkinternals</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/src/it/path-fallback/src/main/java/Main.java
b/src/it/path-fallback/src/main/java/Main.java
new file mode 100644
index 0000000..4212e2e
--- /dev/null
+++ b/src/it/path-fallback/src/main/java/Main.java
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Hello World!");
+ }
+}
diff --git a/src/it/path-fallback/verify.groovy
b/src/it/path-fallback/verify.groovy
new file mode 100644
index 0000000..106f5b9
--- /dev/null
+++ b/src/it/path-fallback/verify.groovy
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+def buildLog = new File( basedir, 'build.log' )
+
+// Verify that the build succeeded even if JAVA_HOME was not explicitly set
+// This test validates the PATH fallback functionality
+def found = false;
+
+buildLog.eachLine { String line ->
+ if ( line.contains( 'BUILD SUCCESS' ) ) {
+ found = true;
+ }
+}
+
+assert found : "Build should have succeeded using jdeps from PATH"
diff --git
a/src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
b/src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
index 619cc55..b44fa6d 100644
--- a/src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jdeps/AbstractJDepsMojo.java
@@ -389,27 +389,55 @@ public abstract class AbstractJDepsMojo extends
AbstractMojo {
//
----------------------------------------------------------------------
// Try to find jdepsExe from JAVA_HOME environment variable
//
----------------------------------------------------------------------
+ Properties env = CommandLineUtils.getSystemEnvVars();
if (!jdepsExe.exists() || !jdepsExe.isFile()) {
- Properties env = CommandLineUtils.getSystemEnvVars();
String javaHome = env.getProperty("JAVA_HOME");
- if (StringUtils.isEmpty(javaHome)) {
- throw new IOException("The environment variable JAVA_HOME is
not correctly set.");
- }
- if ((!new File(javaHome).getCanonicalFile().exists())
- || (new File(javaHome).getCanonicalFile().isFile())) {
- throw new IOException("The environment variable JAVA_HOME=" +
javaHome
- + " doesn't exist or is not a valid directory.");
- }
+ if (!StringUtils.isEmpty(javaHome)) {
+ if ((!new File(javaHome).getCanonicalFile().exists())
+ || (new File(javaHome).getCanonicalFile().isFile())) {
+ throw new IOException("The environment variable
JAVA_HOME=" + javaHome
+ + " doesn't exist or is not a valid directory.");
+ }
- jdepsExe = new File(javaHome + File.separator + "bin",
jdepsCommand);
+ jdepsExe = new File(javaHome + File.separator + "bin",
jdepsCommand);
+ }
}
if (!jdepsExe.getCanonicalFile().exists()
|| !jdepsExe.getCanonicalFile().isFile()) {
- throw new IOException("The jdeps executable '" + jdepsExe
- + "' doesn't exist or is not a file. Verify the JAVA_HOME
environment variable.");
+ //
----------------------------------------------------------------------
+ // Try to find jdepsExe from PATH environment variable
+ //
----------------------------------------------------------------------
+ String path = env.getProperty("PATH");
+ if (path == null) {
+ path = env.getProperty("Path");
+ }
+ if (path == null) {
+ path = env.getProperty("path");
+ }
+ if (path != null) {
+ String[] pathDirs = path.split(File.pathSeparator);
+ for (String pathDir : pathDirs) {
+ if (StringUtils.isBlank(pathDir)) {
+ continue;
+ }
+ File pathJdepsExe = new File(pathDir, jdepsCommand);
+ File canonicalPathJdepsExe =
pathJdepsExe.getCanonicalFile();
+ if (canonicalPathJdepsExe.exists()
+ && canonicalPathJdepsExe.isFile()
+ && canonicalPathJdepsExe.canExecute()) {
+ return canonicalPathJdepsExe.getAbsolutePath();
+ }
+ }
+ }
+
+ throw new IOException(
+ "Unable to locate the jdeps executable. Verify that
JAVA_HOME is set correctly or ensure that jdeps is available on the system
PATH.");
}
+ if (!jdepsExe.canExecute()) {
+ throw new IOException("The jdeps executable '" + jdepsExe + "' is
not executable.");
+ }
return jdepsExe.getAbsolutePath();
}