running a maven scala project

前端 未结 1 940
臣服心动
臣服心动 2020-12-24 04:40

Im starting to learn scala and mongo , my IDE is intellij IDEA. I created a scala project using

mvn:archetype-generate

and typed a simple h

1条回答
  •  心在旅途
    2020-12-24 05:13

    maven-exec-plugin

    Try with this code:

    package com.example
    
    object Main {
        def main(args: Array[String]) {
            println(5)
            val i = 1 + 2
            println(i)
        }
    }
    

    Place it under /src/main/scala/com/example/Main.scala and run it using:

    $ mvn package exec:java -Dexec.mainClass=com.example.Main
    

    If you don't want to pass mainClass manually, you can do this in plugin configuration:

    
      
        org.codehaus.mojo
        exec-maven-plugin
        1.1
        
          com.example.Main
        
      
    
    

    There are other possibilities, this is the easiest one. Of course in IntelliJ you should be able to run the program directly.

    maven-jar-plugin

    If you want to ship the application, use maven-jar-plugin to add Main-Class and Class-Path entries to the manifest:

    Main-Class: com.example.Main
    Class-Path: lib/scala-library-2.9.0-1.jar lib/slf4j-api-1.6.1.jar ...
    

    The following configuration does that and also copies all the dependencies (including Scala runtime library) to target/lib.

    
        maven-jar-plugin
        2.3.1
        
            
                
                    com.example.Main
                    true
                    custom
                    lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}
                    
                
            
        
    
    
        maven-dependency-plugin
        2.3
        
            ${project.build.directory}/lib
        
        
            
                package
                
                    copy-dependencies
                
            
        
    
    

    Now you can simply run your application by (note the target/lib directory is required):

    $ java -jar target/your_app-VERSION.jar
    

    You can ship your application simply by copying your JAR file along with /lib subdirectory.

    Also see Exec Maven Plugin and Playing with Scala and Maven.

    0 讨论(0)
提交回复
热议问题