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.
The environment is ApacheMave:3.5.0 java:1.8.0 scala:2.11.6
$ 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
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.
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)
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 =
<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