Twirl with maven - Exception in thread "main" java.lang.NoSuchMethodError: - scala

I have a maven project, simple twirl template and twirl plugin for maven - https://github.com/JakeWharton/twirl-maven-plugin
After compiling I tried to print generated result but got an runtime error. Also I have a compile error when use a Scala Collections in this template. There is no such errors when I do it with sbt, but I have to use maven.
Could you help me resolve it?
<plugin>
<groupId>com.jakewharton.twirl</groupId>
<artifactId>twirl-maven-plugin</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>twirl-api_2.12</artifactId>
<version>1.5.0</version>
</dependency>
Error
Exception in thread "main" java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
at play.twirl.api.BaseScalaTemplate.<init>(BaseScalaTemplate.scala:12)
at txt.test$.<init>(test.template.scala:15)
at txt.test$.<clinit>(test.template.scala)
at ru.russianpost.dc.AszupGeneratorMain$.main(AszupGeneratorMain.scala:9)
at ru.russianpost.dc.AszupGeneratorMain.main(AszupGeneratorMain.scala)
/twirl/test.scala.txt
#(customer: String)
#customer
main
println(txt.test("Test"))

Related

Exclude Maven Dependency during Eclipse Application Runtime

When I try to run a Java Application on Eclipse, a conflict between two dependencies is causing the application to fail.
I am trying to upgrade my project Java version from OracleJDK 8 to OpenJDK 11. As a result, I also had to also needed to update a GWT dependency from 2.6.0 to 2.8.2, as well as switch from MOJO gwt-maven-plugin to the tboyer version as well. Additionally, the build is done on Maven 4.0.0 on Eclipse IDE 4.9.0.
Snippet of the pom.xml
<properties>
<gwt.version>2.8.2</gwt.version>
...
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>${gwt.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<scope>provided</scope>
</dependency>
...
<build>
<plugins>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-10</version>
<executions>
<execution>
<id>compile-common</id>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
<configuration>
<moduleName>XXXXModule</moduleName>
</configuration>
</execution>
<execution>
<id>XXXX</id>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
<configuration>
<moduleName>XXXXModule</moduleName>
</configuration>
</execution>
<execution>
<id>XXXX</id>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
<configuration>
<moduleName>XXXXModule</moduleName>
</configuration>
</execution>
<execution>
<id>XXXX</id>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
<configuration>
<moduleName>XXXXModule</moduleName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So when I run the application on Eclipse, I get the following error:
SEVERE: Application encountered an exception during Start.
XXXX.ApplicationException: Failed to reflect on start method.
at XXXX.ApplicationLauncher.startApplication(ApplicationLauncher.java:471)
at XXXX.ApplicationLauncher.doRun(ApplicationLauncher.java:185)
at XXXX.ApplicationLauncher.main(ApplicationLauncher.java:67)
Caused by: 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 XXXX.ApplicationLauncher.startApplication(ApplicationLauncher.java:469)
... 2 more
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.ExceptionUtils.preload()V
at org.apache.catalina.startup.Tomcat.<init>(Tomcat.java:181)
....
After investigating, I discovered that two of my dependencies tomcat-embed-core-7.0.91 & apache-jsp-8.0.9.M3 both contain ExceptionUtils with several difference, hence the Exception. Furthermore, the apache-jsp is being pulled by the gwt-dev dependency which is only required during the Maven build to run the gwt-maven-plugin.
gwt-dev:2.8.2
apache-jsp:9.2.14.v20151106
apache-jsp:8.0.9.M3
But for some reason, Eclipse is including gwt-dev and it's dependency as part of the runtime path even though I have the 'provided' scope tag in the pom.xml.
I have tried excluding the apache-jsp dependency, but a compile issue occurs because apparently gwt-dev is still being added which depends on apache-jsp.
Summary
The TBroyer GWT-Maven-Plugin requires the pom file to include gwt-dev as a dependency in order to run correctly during a build.
However, when running the application on Eclipse, a NoSuchMethodException occurs on org.apache.tomcat.util.ExceptionUtils.preload()V
The issue is there are two ExceptionUtil classes with same package from different jars and methods.
One of the jars is apache-jsp which is a dependency of gwt-dev. Gwt-dev and it's dependencies should not be part of the runtime classpath.
Question: Is there a way to ensure the gwt-dev is only used during the Maven build and not part of the Eclipse Java Application runtime classpath?
Yes, you can ensure isolated classpath for client and server sides. You need to apply the multimodule layout. This is also the main difference between the old MOJO gwt maven plugin and the new generation tbroyer gwt maven plugin, the new one support multimodule correctly. Check this archetype as a reference for the multimodule layout. Also, use the packaging:gwt-app to configure the gwt maven plugin executions automatically.
Making it work with eclipse run action (tomcat or gwt) is a bit more difficult. Until you correctly apply the tbroyer gwt maven plugin and the multimodule layout, you will be able to use mvn tomcat7:run and mvn gwt:codeserver in the terminal. Then, the recommended strategy to use it in your IDE is to configure this maven goal and run it using the maven tooling of your IDE. This makes it work exactly the same in all your environments (terminal, CI, eclipse, intellij, etc).

scala-maven-plugin and Scala

I have a scala project that depends on 2.11.8. Hence that is the version I am declaring as a dependency in my project. I have configured the maven-scala-plugin as follows.
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.11.8</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
<scalaVersion>${scala.version}</scalaVersion>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
However, when I build my project I receive:
[ERROR] Failed to execute goal org.scala-tools:maven-scala-plugin:2.11:compile (default) on project myproject: wrap: org.apache.maven.project.InvalidProjectModelException: 1 problem was encountered while building the effective model
[ERROR] [FATAL] Non-parseable POM /Users/myuser/.m2/repository/org/scala-lang/scala-compiler/2.11.8/scala-compiler-2.11.8.pom: entity reference name can not contain character =' (position: START_TAG seen ....uk/main?ParticipantID=nmum6rqpq5q6gkm3933n3nf9s76onu6r&FailedURI=... #1:265) # line 1, column 265
Let me know if there is any additional information that would help resolve this issue.
Thanks in advance!
Dependencies for scala in .m2 were corrupt - cleaned them out and did a clean install for everything. All fixed!

increase scala stack size during maven build

While compiling a scala project using maven (mvn compile) , I am getting error: java.lang.StackOverflowError.
I got the same from eclipse as well, but could solve it by giving Additional command line parameters: -J-Xss256m for scala compiler , as given here How to increase scala stack size
But I am getting the same error while doing "mvn compile". How can I solve this ? Basically how to increase scala stack size while building via maven
You can configure the scala-maven-plugin in the pom.xml like bellow
<project>
...
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArgs>
<jvmArg>-Xms256m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
...
</project>
For more see http://davidb.github.io/scala-maven-plugin/example_compile.html

maven slf4j how to add to classpath

I am completely new to maven, took a couple of tutorials and decided to do some logging with slf4j but I keep getting an error that the class cannot be found.
After some internet searches I have tried to add plugins etc. to my pom.xml but nothing seem to work.
The exception is this:
java -cp target/MavenTest-1.0-SNAPSHOT.jar org.nilun.App
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.nilun.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Here is a snippet of my pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
The very simple class
package org.nilun;
import org.slf4j.*;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Hello World!");
}
}
The jar file is located in my $HOME/development/lib
And of course it has been added to my eclipse build path
Any ideas would be welcomed to solve this.
Thanks!
I think the way to execute the program is the problem. If I execute your code in Eclipse, it works just fine (after adding this dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
which you will need to fix another exception, according to SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"). However, when I use "java -cp target/MavenTest-1.0-SNAPSHOT.jar org.nilun.App", I get the same exception as you.
Edit: To create a .jar file containing dependencies, take a look at the Maven Assembly Plugin (here is a nice and short introduction: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html). You need to add the following to your POM:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>de.clanue.MavenTest.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now, building the project with maven creates a file "MavenTest-0.0.1-SNAPSHOT-jar-with-dependencies.jar" in the target folder. It contains your dependencies and you can run it using "java -jar target/MavenTest-0.0.1-SNAPSHOT-jar-with-dependencies.jar".
Hi Lupa and thank you for the help.
With your guidance I got it to work.
What I did was to add the following plugins to
copy the folders to the target/lib folder
Add the /lib....jar to the manifest file.
Thanks again for the help!
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven auto-clean on package

I'm having some troubles trying to auto-clean each time i run mvn package.
I've added in the POM :
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
But i'm getting the following error from M2Eclipse :
Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-clean-plugin:2.5:clean (execution: auto-clean, phase: initialize)
I've tried other phases like "validate" or "generate-resources" but i'm always getting the same error.
It's not a Maven problem, it's m2eclipse. I haven't used it in a long time, but apparently when you add some plugin executions, you have to add additional configuration to m2eclipse. There's a lengthy description of this error on the Eclipse wiki.