Spring-Boot and scala cannot find main class - spring

I am trying to make a simple hello world web application in Scala which bootstraps spring-boot as its framework using maven as build tool.
I looked around a bit (there are a few tutorials with Gradle and much less information with maven, is there a reason for it?)
I Managed to get everything wired up but now i am stuck with this error when trying to run the application:
Error: Could not find or load main class SampleWebApplication
Here is the source code,
parent pom from github removing some code about git and deployment leaving the structure intact
Then my web application pom:
<parent>
<groupId>X.Y.Z</groupId>
<artifactId>spring-boot-scala-app-parent</artifactId>
<version>1.0.5-SNAPSHOT</version>
</parent>
<artifactId>webservice</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>SampleWebApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
SampleWebApplication:
import org.springframework.boot.SpringApplication
class SampleWebApplication extends App {
SpringApplication.run(classOf[SampleConfig]);
}
SampleConfig:
#Configuration
#EnableAutoConfiguration
#ComponentScan
class SampleConfig {
}

Add it inside your build tag
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>process-resources</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>process-test-resources</phase>
</execution>
</executions>
<configuration>
<scalaVersion>2.11.5</scalaVersion>
</configuration>
</plugin>

Related

how to include static resource from a dependency in spring boot fat jar at the time of build

I have a spring boot application whose UI separately is built and added as a dependency to my application. Now I want to unpack the dependency and add to the resource folder of the spring boot application at the time of build so that it becomes a part of fat jar. Could someone guide me as to how this can be done with spring-boot-maven-plugin.
note: the project is using maven for build
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>nflow-explorer</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>io.nflow</includeGroupIds>
<includeArtifactIds>nflow-explorer</includeArtifactIds>
<outputDirectory>
${project.build.directory}/resources/main/static/explorer
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You don't have to unpack the JAR.
Simply use the Maven Resource Plugin http://maven.apache.org/plugins/maven-resources-plugin/index.html
You can specify directories to include like this:
<project>
...
<build>
...
<resources>
<resource>
<directory>[your folder here]</directory>
</resource>
</resources>
...
</build>
...
</project>

Intellij IDEA does not add maven dependencies to kotlin script classpath

I am trying to run a Kotlin script from Intellij Idea but the dependencies defined in the pom.xml are not added to the classpath. On the Run configuration page there is no Use classpath of module option like it is for Kotlin or Java. I would expect the IDE to pass a -classpath parameter to the org.jetbrains.kotlin.cli.jvm.K2JVMCompiler because if I do it manually all is working fine.
Is this a bug or am I missing something?
noclasspath.kts
import org.slf4j.MDC
val osf = MDC.getMDCAdapter()
val a = "Monkey"
println("$a is big")
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.whatever</groupId>
<artifactId>whyunowork</artifactId>
<version>1.2.3</version>
<properties>
<kotlin.version>1.0.4</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs><sourceDir>${project.basedir}/src/main/kotlin</sourceDir><sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs><sourceDir>${project.basedir}/src/main/kotlin</sourceDir><sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is what I get when I run it from IDEA:
/usr/lib/jvm/jdk1.8.0_92/bin/java -Dfile.encoding=UTF-8 -classpath /home/Development/.IdeaIC2016.2/config/plugins/Kotlin/kotlinc/lib/kotlin-compiler.jar:/home/Development/.IdeaIC2016.2/config/plugins/Kotlin/kotlinc/lib/kotlin-reflect.jar:/home/Development/.IdeaIC2016.2/config/plugins/Kotlin/kotlinc/lib/kotlin-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -script /home/Development/project/src/main/kotlin/noclasspath.kts
noclasspath.kts:1:12: error: unresolved reference: slf4j
import org.slf4j.MDC
^
onyad.kts:3:11: error: unresolved reference: MDC
val osf = MDC.getMDCAdapter()
^
Process finished with exit code 1
IDEA about page:
IntelliJ IDEA 2016.2.5
Build #IC-162.2228.15, built on October 14, 2016
JRE: 1.8.0_112-release-287-b2 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Kotlin plugin version
1.0.4-release-IJ2016.1-112

maven create two jars one for src/main code and other for src/test

I have two sub folders:
src/main/scala
src/test/scla
I want to create two jars out of it. Can you please help me with the maven pom.xml to create the two jars.
Note: I am beginner in maven.
<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>testProject</groupId>
<artifactId>Pricing</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin> -->
</plugins>
</build>
</project>
with this it is creating two jars one containing all the code and the other having test code ...my requirement is to have main code one and test code other...not to mix...please suggest
Use the test-jar goal of the jar plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
I agree with #tdrury... I would try to set testOutputDir in the scala plugin to a different folder so test classes and main classes are generated in different folders. Then using the test-jar goal in the maven-jar-plugin configure the testClassesDirectory property accordingly.

Using fmpp in gradle to generate java files

I have an existing maven project which I am attempting to port to gradle.
One sub module uses fmpp/freemarker to generate a whole lot of java files which are then fed back into the build.
I'm new to gradle, and was wondering if anyone knows of an easy way to do this.
Any help would be appreciated.
My current pom.xml looks like this:
<build>
<plugins>
<!-- Freemarker maven plugin for code generation -->
<plugin>
<groupId>com.googlecode.fmpp-maven-plugin</groupId>
<artifactId>fmpp-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.unix4j</groupId>
<artifactId>unix4j-tools</artifactId>
<version>0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies>
<configuration>
<cfgFile>src/main/resources/codegen/config.fmpp</cfgFile>
<outputDirectory>target/generated-sources/main/java</outputDirectory>
<templateDirectory>src/main/resources/codegen/templates</templateDirectory>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Sorry, should have spent more time googling first. This is the solution which worked for me:
project(':unix4j-core:unix4j-command') {
configurations {pmd}
dependencies {
compile project(':unix4j-core:unix4j-base')
compile project(':unix4j-tools')
pmd project(':unix4j-tools')
}
task generateFmppSources(dependsOn: ":unix4j-tools:compileJava") << {
println "Generating sources...."
ant.taskdef(name:'fmpp', classname:'fmpp.tools.AntTask', classpath:configurations.pmd.asPath);
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp", sourceRoot:"src/main/resources/codegen/templates", outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn generateFmppSources
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
}

Reference resources from a test-jat artifact

I have two artifact:
artifact-A: contains resources in src/test/resources/
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
artifact B: uses resources from artifact A
<dependency>
<groupId>com.xxxx.yyy</groupId>
<artifactId>artifact-A</artifactId>
<version>3.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
The problem is that the resources are never extracted in the project artifact-B.
How can I do that ?
If you define a dependency like this the used jar will never be extracted cause it will be put on the classpath during compilation etc. This means to access the resources from artifact-A you need to access them via the classpath.
In artifact-B, I used the maven-dependency-plugin to extract resources from the test-jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-test-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>artifact-A</includeArtifactIds>
<includes>**/db-test/*</includes>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Resources