Gradle build null console object

后端 未结 10 2311
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 22:26

I\'m trying to get my gradle builds to prompt at the console for a password using examples from stack overflow

When I have a statment such as:

def pa         


        
10条回答
  •  佛祖请我去吃肉
    2020-12-02 22:57

    create a simple function to request a password:

    import javax.swing.JOptionPane
    
    def askPass() {
      def msg = 'Enter keystore password'
      if (System.console() != null) {
        return System.console().readLine(msg)
      } else {
        return javax.swing.JOptionPane.showInputDialog(msg)
      }
    }
    

    or if you want Y/n answer:

    import javax.swing.JOptionPane
    
    def ask(msg) {
      if (System.console() != null) {
        return System.console().readLine(msg + ' [y/n]') == 'y'
      } else {
        def res = JOptionPane.showConfirmDialog(null, msg, "Confirm operation", JOptionPane.YES_NO_OPTION)
        return res == JOptionPane.YES_OPTION
      }
    }
    
    // usage:
    
    task run() {
      doFirst {
        if (file('out.txt').exists() && !ask('overwrite output?')) {
          System.exit(2)
        }
      }
      ...
    }
    

提交回复
热议问题