Search code examples
javaintellij-ideabuildplayframeworksbt

Java Play - How run Play in IntelliJ with custom sbt configurations options


The problem

I need to run a Play2 Java application build with sbt using IntelliJ Ultimate. When using the option 'Run Play 2 App` it works but it runs with default configuration.

How can I supply custom sbt configuration in IntelliJ?

More in Details

I can run in the terminal and it works with this command

sbt  -J-XX:+UnlockExperimentalVMOptions "start -Dconfig.file=/conf/application.conf -Dpidfile.path=/dev/null -Djava.net.preferIPv4Stack=true -Dlogger.file=/conf/logback.xml"

Now, in the application.conf there are many custom configuration but the most noticable is the Play web port which is play.server.http.port=5000 So I can see straigh away that it picked up the correct configuration at the start.

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Alt+D to stop

Now in IntelliJ there is a build in 'Play 2 App' Run/Debug configurations but really I don't see any way to:

  1. Supply sbt arguments
  2. Run sbt with command 'start' but instead it runs always as sbt run' (I know this because the sbt start` is in production mode and the application need to run in that way at the moment

enter image description here

I also try with run/debug as 'Application' following this old answer here : https://stackoverflow.com/a/5390609/13903942

I almost works. I can give the stb option but I can't choose to run as sbt run or sbt start it seems it always run as sbt start

Ultimatelly I need any other run/debug option.

I don't need running from terminal solution, this I know how to do it. THe whole point to pay a Ultimate version of Intellij is to be able to run and Run the debugger with the IDE (otherwise I ll use something else)

Is there a way?

Honestly, is probably one of the worst un-documented and twisted situation I've being facing in relation to the fact that, we are talking about Java is very known programming language, Play framework stand basically at top position on Java frameworks and well IntelliJ integration, I'm astonish on how bad is and the amount of hours I'm spending just to make this run.


Solution

  • I had some time and I was able to validate what I said in the comments.

    • port can be set changing the value of Url To Open
    • params for the JVM can be set changing the value of JVM Options

    I just created a play java project using the play-java-seed template with the sbt command

    sbt new playframework/play-java-seed.g8
    

    Refactored the HelloController to log some messages before return the 200 Ok response

    import com.typesafe.config.Config;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import play.mvc.*;
    
    import javax.inject.Inject;
    
    public class HomeController extends Controller {
    
        // started the logger
        final Logger logger = LoggerFactory.getLogger(this.getClass().getCanonicalName());
    
        final Config config;
    
        // injected the config
        @Inject
        public HomeController(Config config) {
            this.config = config;
        }
    
        public Result index() {
            // logged some messages using different log levels
            logger.debug("A super debug message");
            logger.info("An info message");
            logger.warn("A warning message");
            // logged a message using a value provided in the config file
            logger.info("the value of some-key is " + config.getString("some-key"));
            return ok(views.html.index.render());
        }
    
    }
    

    I created two run configurations in Intellij:

    prod

    • Url To Open
    http://localhost:9020
    
    • JVM Options:
    -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled
    
    • set the log level root to INFO and added the prefix PROD STDOUT - for the log messages in logback.xml file

    • added the line some-key = "A Prod Value" to application.conf file

    run web app prod

    Got the following output

    /<path to java home>/bin/java 
      -Dfile.encoding=UTF8 
      -Djline.terminal=none 
      -Dsbt.global.base=/private/var/folders/n3/7h5c5s2x1d1f_m_vz22h73280000gn/T/sbt-global-pluginstub 
      -Dsbt.log.noformat=true 
      -Xms512M 
      -Xmx1024M 
      -Xss1M
      -XX:+CMSClassUnloadingEnabled -Didea.managed=true 
      -classpath "<intellij app path>/IntelliJIdea2023.1/plugins/Scala/launcher/sbt-launch.jar" xsbt.boot.Boot "project root" "run 9020" # new value for the port
    
    #############################################
    some init log messages from play
    #############################################
    
    # next you can see the prefix added
    PROD STDOUT - 2023-08-05 18:23:10 INFO  play.api.Play  Application started (Dev) (no global state)
    PROD STDOUT - 2023-08-05 18:23:11 INFO  controllers.HomeController  An info message
    PROD STDOUT - 2023-08-05 18:23:11 WARN  controllers.HomeController  A warning message
    PROD STDOUT - 2023-08-05 18:23:11 INFO  controllers.HomeController  the value of some-key is A Prod Value
    # above you can see the value of the key from the config file
    

    dev

    • Url To Open
    http://localhost:9010
    
    • JVM Options: set path for config and logback files, replaced the values for Xms, Xmx and Xss
    -Xmx2048M -Xss2M -XX:+CMSClassUnloadingEnabled -Dconfig.file=conf/dev-application.conf -Dlogger.file=conf/dev-logback.xml
    
    • set the log level root to DEBUG and added the prefix DEV STDOUT - for the log messages in dev-logback.xml file

    • added the line some-key = "A Dev Value" to dev-application.conf file

    run web app dev

    Got the following output

    /<java home path>/bin/java 
      -Dfile.encoding=UTF8 
      -Djline.terminal=none 
      -Dsbt.global.base=/private/var/folders/n3/7h5c5s2x1d1f_m_vz22h73280000gn/T/sbt-global-pluginstub 
      -Dsbt.log.noformat=true 
      -Xms1024M # param edited 
      -Xmx2048M # param edited
      -Xss2M    # param edited
      -XX:+CMSClassUnloadingEnabled 
      -Dconfig.file=conf/dev-application.conf # param added
      -Dlogger.file=conf/dev-logback.xml      # param added
      -Didea.managed=true 
      -classpath "/<intellij path>/IntelliJIdea2023.1/plugins/Scala/launcher/sbt-launch.jar" xsbt.boot.Boot "project root" "run 9010" # here you can see the value of the port I set in `Url To Open`
    
    #############################################
    some init log messages from play
    #############################################
    
    # next you can see the prefix added for dev and also the log level is set ot DEBUG
    DEV STDOUT - 2023-08-05 18:34:18 DEBUG controllers.HomeController  A super debug message
    DEV STDOUT - 2023-08-05 18:34:18 INFO  controllers.HomeController  An info message
    DEV STDOUT - 2023-08-05 18:34:18 WARN  controllers.HomeController  A warning message
    DEV STDOUT - 2023-08-05 18:34:18 INFO  controllers.HomeController  the value of some-key is A Dev Value
    # above you can see the value of the key from the config file