Until you build a project described in scala with Maven and execute it with the scala command.

Maven -scala run-

Since I had the opportunity to use maven for the first time, I will summarize the places where I got caught. See https://kengotoda.gitbooks.io/what-is-maven/primer/ for what maven itself looks like I got a rough image by reading. This article describes the procedure for achieving the following objectives.

  1. Create a project containing scala code
  2. Settings for executing scala code in maven
  3. Generating and executing a jar file containing dependencies

The environment is ApacheMave:3.5.0 java:1.8.0 scala:2.11.6

Project creation

$ mvn archetype:generate
Choose a number or apply filter : scala
# 7: remote -> net.alchim31.maven:scala-archetype-Check simple
Choose a number or apply filter : 7
Choose a number : 3
Define value for property 'groupId': project.test
Define value for property 'artifactId': mavenTest
Define value for property 'version' 1.0-SNAPSHOT: [Enter]
Define value for property 'package' project.test: [Enter]
Y: [Enter]

This will create a mavenTest folder.

$ tree mavenTest
mavenTest/
├── pom.xml
└── src
    ├── main
    │   └── scala
    │       └── project
    │           └── test
    │               └── App.scala
    └── test
        └── scala
            └── samples
                ├── junit.scala
                ├── scalatest.scala
                └── specs.scala

Settings to run

If you compile as it is,

$ cd mavenTest
$ mvn compile
...
[ERROR] scalac error: bad option: '-make:transitive'
[INFO]   scalac -help  gives more information
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.446 s
[INFO] Finished at: 2017-08-11T21:39:24+09:00
[INFO] Final Memory: 11M/300M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.0:compi
...

Error appears. Therefore, edit pom.xml as follows.

pom.xml


<!--Delete this line-->
     <arg>-make:transitive</arg>

Then the compilation is successful, so execute it. Then I get the following error:

$ mvn scala:run
...
[ERROR] /home/yoshiki/xgboost/jvm-packages/project/mavenTest/src/test/scala/samples/specs.scala:18: error: not found: type JUnitRunner
[ERROR] @RunWith(classOf[JUnitRunner])
[ERROR]                  ^
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.391 s
[INFO] Finished at: 2017-08-11T21:43:41+09:00
[INFO] Final Memory: 11M/300M
[INFO] ------------------------------------------------------------------------
...

Apparently it was moss to build the test code. Therefore, change pom.xml as follows.

By the way, mvn compile and mvn scala: run compile and scala: run specify the execution goal, so

mvn scala: run includes mvn compile operations. So, in reality, you only need to execute mvn scala: run.

pom.xml


  <dependencies>
    ...
    <!-- JUnitRunner -->
    <dependency>
        <groupId>org.specs2</groupId>
        <artifactId>specs2-junit_${scala.compat.version}</artifactId>
        <!--version is before and after specs2-Match with core etc.-->
        <version>2.4.16</version>
        <scope>test</scope>
    </dependency>
    <!----------------->
    ...
  </dependencies>

Run

$ mvn scala:run

[WARNING] warning: there was one deprecation warning; re-run with -deprecation for details
[WARNING] one warning found
[INFO] prepare-compile in 0 s
[INFO] compile in 4 s
[INFO] 
[INFO] <<< scala-maven-plugin:3.2.0:run (default-cli) < test-compile @ mavenTest <<<
[INFO] 
[INFO] 
[INFO] --- scala-maven-plugin:3.2.0:run (default-cli) @ mavenTest ---
[WARNING] Not mainClass or valid launcher found/define

Since there is no main class, specify it in scala-maven-plugin. (Reference: http://qiita.com/motokazu/items/57540c80509d83c6a168)

pom.xml


<!-- scala-maven-Add to plugin of plugin-->
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
        <!--from here-->
        <configuration>
          <launchers>
            <launcher>
              <id>test</id>
              <mainClass>project.test.App</mainClass>
              <args>
                <arg>${basedir}</arg>
              </args>
            </launcher>
          </launchers>
        </configuration>
        <!--Add up to here-->
      </plugin>

When mvn scala: run is executed,'Hello world!' Is output safely. Next, add the library you want to use freely to the dependencies of pom.xml. If xgboost,

pom.xml


<dependency>
  <groupId>ml.dmlc</groupId>
  <artifactId>xgboost4j</artifactId>
  <version>0.7</version>
</dependency>

Add it like that. (To actually use xgboost, you have to mvn install in advance according to the official procedure)

I just want to do it

mvn scala: run compiles and executes the application, but it even compiles without permission. If you just want to run it, you first need to generate a jar file containing the dependent libraries. for that reason Add maven-assembly-plugin to pom.xml.

pom.xml


<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

Run

$ ls target
archive-tmp                  maven-status                                      test-classes
classes                      mavenTest-1.0-SNAPSHOT.jar                        test-classes.-2096028797.timestamp
classes.936127338.timestamp  mavenTest-1.0-SNAPSHOT-jar-with-dependencies.jar
maven-archiver               specs2-reports

$ scala -cp target/mavenTest-1.0-SNAPSHOT-jar-with-dependencies.jar project.test.App
Hello World!
concat arguments = 

Add repository url

<project>
  <repositories>
      <id>my-repo1</id>
      <name>central-repo</name>
      <url>https://repo.maven.apache.org/maven2/</url>
    </repository>
    <repository>
      <id>my-repo2</id>
      <name>maven2-repo</name>
      <url>https://dl.bintray.com/spark-packages/maven/</url>
    </repository>
  </repositories>
</project>

end.

Recommended Posts

Until you build a project described in scala with Maven and execute it with the scala command.
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Until you create a Spring Boot project in Intellij and push it to Github
Create a Maven project with a command
Run JUnit and Spock in a maven project
Notify the build result with slack with CircleCI. You can do it in 5 minutes.
Getting started with Maven (until you create a Java project and combine external libraries into a single executable JAR)
21 Load the script from a file and execute it
Create a Java (Maven) project with VS Code and develop it on a Docker container
How to add another project as Maven library with CircleCI and use it for build
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
Until you build the docker environment and start / stop the Ubuntu container
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
[Gradle] Build a Java project with a configuration different from the convention
Build a Java project with Gradle
How to download and run a Jar package directly from the Maven repository with just the command line
What to do if Operation not permitted is displayed when you execute a command in the terminal
Make a daily build of the TOPPERS kernel with Gitlab and Docker
Create a Spring Boot app development project with the cURL + tar command
[Java] Create a jar file with both compressed and uncompressed with the jar command
I wrote a Lambda function in Java and deployed it with SAM
Until you can use the H2 database in server mode with Dropwizard using Eclipse and connect with DB Viewer.
Create a jar file with the command
Getting started with Gradle (until you create a Java project and combine external libraries into one executable JAR)
Get YouTube video information with Retrofit and keep it in the Android app.
Until you run a Java program with the AWS SDK local to Windows
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
I wanted to start the AP server and debug with just the Maven command
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
[Laravel] Command memorandum until the start of the project
Add a project in any folder with Gradle
Automate Java (Maven) project build with CircleCI + Orbs
Docker command to create Rails project with a single blow in environment without Ruby
What to do if you cannot execute with the command "Java package name / class name"