Compiling Scala Using Maven - scala

I want to create a hello world application using maven.
here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ColossusPlay</groupId>
<artifactId>ColossusPlay</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.tumblr</groupId>
<artifactId>colossus-metrics_2.10</artifactId>
<version>0.8.1-RC1</version>
</dependency>
</dependencies>
</project>
and here is my scala code:
object Main extends App{
println( "Helo World" )
}
when I run
mvn package
it generates a jar file in the target directory. Then what I want to be able to do is run that jar file using
scala target/ColossusPlay-0.0.1-SNAPSHOT.jar
However I get NullPointer Exception like this:
java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$tryClass$1.apply(ScalaClassLoader.scala:43)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$tryClass$1.apply(ScalaClassLoader.scala:43)
at scala.util.control.Exception$Catch$$anonfun$opt$1.apply(Exception.scala:119)
at scala.util.control.Exception$Catch$$anonfun$opt$1.apply(Exception.scala:119)
at scala.util.control.Exception$Catch.apply(Exception.scala:103)
at scala.util.control.Exception$Catch.opt(Exception.scala:119)
at scala.reflect.internal.util.ScalaClassLoader$class.tryClass(ScalaClassLoader.scala:42)
at scala.reflect.internal.util.ScalaClassLoader$class.tryToInitializeClass(ScalaClassLoader.scala:39)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.tryToInitializeClass(ScalaClassLoader.scala:101)
at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:63)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
at scala.tools.nsc.JarRunner$.run(MainGenericRunner.scala:13)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.JarRunner$.runJar(MainGenericRunner.scala:25)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:69)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
What am I missing?
Update:
The problem appears to be that the maven build does not see the source files. I tried to force it to have a build error writing nonesense to the source file but the mvn package still says build success. Additionaly when I examine the jar file, there isn't any class files inside. How can I make the maven see the source files.

You have to add a scale compiler plugin such sbt-compiler plugin
SBT compiler plugin
Example pom
<plugin>
<groupId>com.google.code.sbt-compiler-maven-plugin</groupId>
<artifactId>sbt-compiler-maven-plugin</artifactId>
<version>1.0.0-beta9</version>
<executions>
<execution>
<id>default-sbt-compile</id>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Using maven with scala in specific use case ( profiles, complex deployments, deep hierarchy ) is even better than pure sbt , but at the beginning is a bit tricky.

Related

Maven Gatling 3.0.2 - NoSuchMethodError

When I run "mvn clean gatling:test -Dgatling.simulationClass=package.SimpleSim", I get the following error:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.gatling.mojo.MainWithArgsInFile.runMain(MainWithArgsInFile.java:50)
at io.gatling.mojo.MainWithArgsInFile.main(MainWithArgsInFile.java:33)
Caused by: java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
at io.gatling.core.cli.CommandLineConstant.<init>(CommandLineConstant.scala:19)
at io.gatling.app.cli.CommandLineConstants$.<init>(CommandLineConstants.scala:22)
at io.gatling.app.cli.CommandLineConstants$.<clinit>(CommandLineConstants.scala)
at io.gatling.app.cli.ArgsParser$$anon$1.<init>(ArgsParser.scala:30)
at io.gatling.app.cli.ArgsParser.<init>(ArgsParser.scala:28)
at io.gatling.app.Gatling$.fromArgs(Gatling.scala:46)
at io.gatling.app.Gatling$.main(Gatling.scala:39)
at io.gatling.app.Gatling.main(Gatling.scala)
I tried removing a lot of dependencies with only Gatling Highchart and Gatling Plugin remaining. It stills throw the same error.
The funny thing is I copy many code from a working project and the project is still running properly now.
I am not sure why this is happening in the new project...
The pom:
<parent>
<artifactId>abc-pom</artifactId>
<groupId>com.abc.def</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<gatling.version>3.0.3</gatling.version>
<gatling-plugin.version>3.0.2</gatling-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling-plugin.version}</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<configFolder>${project.basedir}/src/test/resources/gatling_conf</configFolder>
<runMultipleSimulations>true</runMultipleSimulations>
<includes>
<simulationClass>package.SimpleSim</simulationClass>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The script:
package package.scalability_test
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class SimpleSim extends Simulation {
val httpProtocolBuilder = http.baseUrl("http://www.google.com")
val scenarioBuilder = scenario("Get").exec(http("Get_Request")
.get("/"))
setUp(
scenarioBuilder.inject(constantUsersPerSec(1) during(60 seconds))
).protocols(httpProtocolBuilder)
}
The issue was actually due to the <parent></parent> tag. This is a project within a larger project. Somehow, inheriting the parent pom causes some conflicts which resulted in this issue.
My solution was to remove the <parent> tag. But, the correct solution could be to actually figure out which dependency is causing a conflict.
First check your all Scala version in your classpath mvn dependency:tree, then override with compatible Scala version with your Gatling version

Cannot add maven dependencies to deployment assembly in ejb packaging

I have a project with a POM that looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>my-parent</artifactId>
<groupId>com.company</groupId>
<version>2</version>
</parent>
<groupId>com.company</groupId>
<artifactId>resources-silo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>Some EJB component</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>simple-command</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- some jars ... -->
</dependencies>
</project>
I want to use m2eclipse with m2e-wtp to deploy this project to Glassfish 3. However, m2e-wtp does not deploy the dependencies. I know that building superjars with jar-with-dependencies is probably not the way to go for EJB projects, but I cannot really change this without messing up the customer's build / deployment processes.
So, the problem is that the deployment assembly wizard does not display the maven dependencies:
I need a solution that works without changing the pom.xml, since that would disturb the customer's build processes. I am aware that using an EAR would be the preferred solution here.
Is the behaviour that I'm seeing a bug or is it an inherent limitation in the way m2e-wtp and the ejb packaging work?
Is it even possible to have m2e-wtp extract dependency jars into the deployed application?

Eclipse could not find artifact on local maven-plugin module

I have a maven project with 1 parent and 5 modules. One of the modules (call it P) is of packaging "maven-plugin", which is configured to be run in the execution phase "process-test-classes of other module (call it T).
The module T has pom.xml as follows. I omitted dependencies, but they don't include P. As in many other cases it all works perfectly in command line.
Question: Whatever I do Eclipse shows "Could not find artifact org.local.enhancer:pom:0.0.2-SNAPSHOT pom.xml /test line 1 Maven Configuration Problem".
Seems like the issue started after installing "m2e-wtp" v0.17 to resolve other issue with offending jars. Configuration is Eclipse Juno SR2, m2e v1.3.1, m2e-wtp v0.17.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>org.local</groupId>
<version>0.0.2</version>
<relativePath>..</relativePath>
</parent>
<artifactId>test</artifactId>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.local</groupId>
<artifactId>enhancer</artifactId>
<version>0.0.2-SNAPSHOT</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Does anybody have any insight on this? Thanks!

In IntelliJ Scala plugin, can I use scala-compiler.jar retrieved via <dependency> in `pom.xml`?

In IntelliJ, I created a blank Maven project, and added to pom.xml:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.11.0-M3</version>
<scope>compile</scope>
</dependency>
I then went to Project Settings -> Modules -> + -> Scala, and specified the compiler jar from the drop-down menu as Maven: org.scala-lang:scala-compiler:2.11.0-M3. I receive the error:
Compiler library: no scala-library*.jar found
Of course, scala-library-2.11.0-M3.jar is among the dependencies (transitively), but not stored in the same folder as scala-compiler-2.11.0-M3.jar, which is what seems to be confusing the Scala plugin.
Is there any way to use scala-compiler*.jar downloaded from a pom.xml dependency? Perhaps by manually specifying where to find each JAR?
Before this, I had been using scala-compiler*.jar downloaded via Project Settings -> Libraries -> + -> From Maven. This seems to work because IntelliJ sticks all the downloaded JARs in the same folder, which is what the Scala plugin assumes. Simply for aesthetic reasons I would like to know if it can be done with pom.xml.
You don't need the scala-compiler as a dependency to be able to build the scala project (neither using maven at command line nor by building from Build | Make Project in IntelliJ).
This is a very simple pom.xml that I use for scala projects where I don't have scala installed at all and just point IntelliJ to this pom and then I can compile and run any scala code directly from within IntelliJ.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>scala-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.10.1</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile</id>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Compiling LESS CSS with Maven and m2e-wtp

I'm trying to compile LESS CSS files with lesscss-maven-plugin, both in pure maven (with command line) and within Eclipse (Juno).
In the lesscss-maven-plugin, I need to define an output directory, but I noticed that in Eclipse WTP copies files from target/m2e-wtp in my server (JBoss), but that this directory is ignored by the war plugin of Maven.
I succeeded to reach my goal with Maven profiles : in Eclipse I use a m2e profile configured in Project settings, so I can define two different destination folders depending on I build in Eclipse or not.
Here is my pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dgadev.motd</groupId>
<artifactId>motd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
....
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.lesscss</groupId>
<artifactId>lesscss-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<outputDirectory>${project.build.directory}/resources/css</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>m2e</id>
<build>
<plugins>
<plugin>
<groupId>org.lesscss</groupId>
<artifactId>lesscss-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<outputDirectory>${project.build.directory}/m2e-wtp/web-resources/resources/css</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This works, but is there a better way to do this, without the profile trick ?
I found another solution with a different less compiler : wro4j. With this compiler, both exists maven and m2e plugins.
In addition, a tutorial (for building boostrap) can be found here: m2e-wro4j
The same solution you provided, a bit cleaner :
https://github.com/marceloverdijk/lesscss-maven-plugin/issues/8
The m2e profile is activated with a m2e variable, so you don't have to select it.