Andrii Radistao created MJDEPRSCAN-9:
----------------------------------------

             Summary: jdeprscan plugin does not work when JAVA_HOME is not set
                 Key: MJDEPRSCAN-9
                 URL: https://issues.apache.org/jira/browse/MJDEPRSCAN-9
             Project: Maven JDeprScan Plugin
          Issue Type: Wish
    Affects Versions: 3.0.0-alpha-1
         Environment: Ubuntu 22.04.2 LTS
5.15.0-69-generic #76-Ubuntu SMP x86_64 GNU/Linux

Installed JDK:
Package: openjdk-11-jdk
Version: 11.0.18+10-0ubuntu1~22.04

openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed 
mode, sharing)

$JAVA_HOME is empty

            Reporter: Andrii Radistao


When running command

 
{code:java}
mvn jdeprscan:jdeprscan{code}
 

(or any other maven workflow, which uses jdeprscan)

on _any_ project without  specified system environment variable {{JAVA_HOME}} - 
the build fails with the error:
{quote}[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-jdeprscan-plugin:3.0.0-alpha-1:jdeprscan 
(default-cli) on project cloud-backend-internal-client: Unable to find 
jdeprscan command: The environment variable JAVA_HOME is not correctly set. -> 
[Help 1] 
{quote}
all other maven tools ({{{}javac{}}}, {{{}jar{}}}, tests) and most of other 
plugins work fine without {{JAVA_HOME}} specified

Note, that we use default java-11 installation on Debian (Ubuntu):

 
{code:java}
openjdk-11-jdk:amd64          11.0.18+10-0ubuntu1~22.04 amd64{code}
which doesn't set JAVA_HOME by default, but still has {{jdeprscan}} installed 
and provided in the PATH (same as {{javac}} or {{{}jar{}}}):

 

 
{code:java}
$ command -v jdeprscan
/usr/bin/jdeprscan

$ jdeprscan --version
11.0.18{code}
 

The issue happens because {{maven-jdeprscan-plugin:3.0.0-alpha-1}} resolves 
executable path only by toolchain or JAVA_HOME, but ignores default {{PATH}} 
(like {{javac}} or {{jar}} do) or JDK installation configuration:

[https://github.com/apache/maven-jdeprscan-plugin/blob/36641ba0b618bf1e8bf84c51ac4f07e60cf0c01b/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java#L148-L154]

 
 
{code:java}
if ( !jdeprscanExe.exists() || !jdeprscanExe.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." ); 
    }{code}
*Proposition:*

consider resolving {{jdeprscan}} executable location either by default {{PATH}} 
or by {{{}SystemUtils#getJavaHome{}}}, like in [this 
line|https://github.com/apache/maven-jdeprscan-plugin/blob/36641ba0b618bf1e8bf84c51ac4f07e60cf0c01b/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java#L142-L144]:
{code:java}
jdeprscanExe =
    new File( SystemUtils.getJavaHome() + File.separator + ".." + 
File.separator + "sh", jdepsCommand );{code}
*but* without {{".." + File.separator + "sh"}}

 i tried and it proved to work:
{code:java}
new File( SystemUtils.getJavaHome() + File.separator + "bin", jdepsCommand 
);{code}
I can supply a PR if this helps.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to