github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/jib/jvm.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package jib
    18  
    19  import (
    20  	"context"
    21  	"os/exec"
    22  	"sync"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  )
    27  
    28  var (
    29  	// JVMFound is replaceable for testing
    30  	JVMFound = jvmFound
    31  
    32  	// JVMFound() returns true if a Java VM was found and works.
    33  	resolveJVMOnce sync.Once
    34  	jvmPresent     bool
    35  )
    36  
    37  // jvmFound returns true if a Java VM was found and works.
    38  func jvmFound(ctx context.Context) bool {
    39  	// Check on demand: performing the check in an init() causes the
    40  	// check to be run even when no jib functionality was used.
    41  	resolveJVMOnce.Do(func() {
    42  		jvmPresent = resolveJVM(ctx)
    43  	})
    44  	return jvmPresent
    45  }
    46  
    47  // resolveJVM returns true if a Java VM was found and works.  It is intended for
    48  // `skaffold init` on macOS where calling out to the Maven Wrapper script (mvnw) can
    49  // hang if there is no installed Java VM found.
    50  func resolveJVM(ctx context.Context) bool {
    51  	// Note that just checking for the existence of `java` is insufficient
    52  	// as macOS ships with /usr/bin/java that tries to hand off to a JVM
    53  	// installed in /Library/Java/JavaVirtualMachines
    54  	cmd := exec.Command("java", "-version")
    55  	err := util.RunCmd(ctx, cmd)
    56  	if err != nil {
    57  		log.Entry(context.TODO()).Warnf("Skipping Jib: no JVM: %v failed: %v", cmd.Args, err)
    58  	}
    59  	return err == nil
    60  }