Kotlin JSR-223 ScriptEngineFactory within the fat jar - Cannot find kotlin compiler jar - maven

I have a fat jar where I'm trying to get the instance of Kotlin's ScriptEngine.
For the debugging purposes I'm iterating through available Script Engine Factories and getting the engines.
val scriptEngineManager = ScriptEngineManager()
for (factory in scriptEngineManager.engineFactories) {
val scriptEngine = factory.scriptEngine
}
When it hits the Kotlin's engine, it fails with following exception:
Exception in thread "main" java.io.FileNotFoundException: Cannot find kotlin compiler jar, set kotlin.compiler.jar property to proper location
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt:100)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.getKotlinCompilerJar(KotlinJsr223ScriptEngineFactoryExamples.kt)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.access$getKotlinCompilerJar$p(KotlinJsr223ScriptEngineFactoryExamples.kt:1)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory.getScriptEngine(KotlinJsr223ScriptEngineFactoryExamples.kt:56)
at davidsiro.invoices.InvoiceGeneratorKt.generateInvoice(invoiceGenerator.kt:16)
at davidsiro.invoices.MainKt.main(main.kt:11)
My fat jar contains all of the dependencies (though unpacked), including Kotlin Compiler. I'm using Maven Assembly Plugin to build it, which configured like these:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
Any ideas?
Update
For the record, I tried to both KotlinJsr223JvmLocalScriptEngineFactory and KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory with the same result.

The JSR223 factories in the kotlin-script-util are trying to find the compiler jar in order to setup the compilation. In your case, you'll need to write your own factory to supply the script compilation classpath explicitly, e.g.
class MyScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
override fun getScriptEngine(): ScriptEngine =
KotlinJsr223JvmLocalScriptEngine(
Disposer.newDisposable(),
this,
classpath, // !!! supply the script classpath here
KotlinStandardJsr223ScriptTemplate::class.qualifiedName!!,
{ ctx, types -> ScriptArgsWithTypes(arrayOf(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), types ?: emptyArray()) },
arrayOf(Bindings::class)
)
}
You need to put the following jars into classpath:
kotlin-script-util.jar - it contains the template class used as a superclass for the script
kotlin-script-runtime.jar - for base classes used in scripting
any other jars you'll need to use in your scripts, quite likely - kotlin-stdlib.jar
You may put your fat jar there instead, but that would mean that everything from it will be accessible from your scripts. Not talking about the overheads for the compiler.

Related

How do I overcome "object literals cannot contain duplicate keys in ES5 strict mode" error with Maven minify plugin?

I'm using the Maven minify plugin with Maven 3.5. I have the below configuration ...
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>verify</phase>
<configuration>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssTargetDir>../${project.artifactId}/css</cssTargetDir>
<jsSourceExcludes>
<jsSourceExclude>lib/pdf.js</jsSourceExclude>
<jsSourceExclude>lib/pdf.worker.js</jsSourceExclude>
<jsSourceExclude>ckeditor_4.2/**</jsSourceExclude>
<jsSourceExclude>geogebra/**</jsSourceExclude>
<jsSourceExclude>contextMenu/**</jsSourceExclude>
<jsSourceExclude>wPaint/**</jsSourceExclude>
</jsSourceExcludes>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsTargetDir>../${project.artifactId}/js</jsTargetDir>
<jsEngine>CLOSURE</jsEngine>
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir>
<skipMerge>true</skipMerge>
<nosuffix>true</nosuffix>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
However, I'm getting this error on the below file ...
SEVERE: panel.js:845: ERROR - with
SIDE_PANEL_BEGIN_CLOSE_EVENT:SIDE_PANEL_BEGIN_CLOSE_EVENT,
^
Nov 27, 2018 2:57:06 PM com.google.javascript.jscomp.LoggerErrorManager printSummary
WARNING: 1 error(s), 0 warning(s)
Everything works in my application fine with my uncompressed file so I'm thinking I just made a mistake with my configuration but I can't see what. How can I correct the configuration so that the optimization occurs without the error without affecting the underlying functionality of the code?
While Google's Closure Compiler (which is used under the hood by the minify plugin to optimize the source code) by default expects ECMAScript 6 as input language it seems to run additional diagnostic checks which do not take the language level into account.
These diagnostic checks include one for ECMAScript 5 strict mode compliance which forbids duplicate keys in objects.
As described in this answer by Sebstian Häger you can turn this diagnostic check of in the minify configuration by adding
<closureWarningLevels>
<es5Strict>OFF</es5Script>
</closureWarningLevels>

OSGi bundle doesn't start after inserting a Bundle Activator

I'm implementing a maven web project in Netbeans (version 8.0.2) with some build profiles (jetty, OSGi..) managed by POM file using plugins and custom goals of project in Netbeans.
I need to manage the lifecycle of the generated OSGi bundle, so I created an activator with the following class:
package org.activiti.explorer.conf;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class OSGiActivator implements BundleActivator {
#Override
public void start(BundleContext context) throws Exception {
System.out.println("BUNDLE STARTED");
}
#Override
public void stop(BundleContext context) throws Exception {
System.out.println("BUNDLE STOPPED");
}
}
After that I updated the maven bundle <plugin> element in the POM file adding the informaton about the activator, so the plugin is:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-Activator>org.activiti.explorer.conf.OSGiActivator</Bundle-Activator>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Embed-Dependency>*</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Web-ContextPath>activiti-explorer</Web-ContextPath>
<Import-Package> org.eclipse.jetty.servlet.listener,
org.apache.jasper.servlet,
javax.naming,
javax.el;resolution:=optional,
com.sun.el;resolution:=optional,
org.apache.el;resolution:=optional,
javax.servlet.jsp;resolution:=optional,
org.apache.jasper.runtime;resolution:=optional,
org.apache.tomcat;resolution:=optional,
javax.servlet;version="[2.6.0,4.0.1]",
javax.servlet.http;version="[2.6.0,4.0.1]",
org.apache.log4j,
javax.inject,
org.slf4j.impl,
org.slf4j,
org.slf4j.spi,
org.apache.commons.io;version="[1.4,2)",
org.apache.commons.io.output;version="[1.4,2)",
org.apache.commons.lang3;version="[3.1,4)",
org.apache.commons.lang3.builder;version="[3.1,4)",
org.apache.commons.lang3.exception;version="[3.1,4)"
</Import-Package>
<Export-Package>!*</Export-Package>
</instructions>
<manifestLocation>${basedir}/target/META-INF</manifestLocation>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when I build the bundle and load it on OSGi with Felix, the bundle is activated but it doesn't start, whereas if I delete the row about the Bundle-Activator from POM file it works properly.
Note that from Felix console no error is displayed about Build classpath, imported packages and exported packages and if I try to start bundle from osgi line command, this exception is logged:
gogo: BundleException: Error starting module.
Can someone help me? If I don't use the class which implements the Activator and I don't refer it in the POM file, the bundle created runs properly in OSGi.
You are listing all the Import-Package statements explicitly. This means that the maven-bundle-plugin does not add auto detected package usages. You are missing the Import-Package for org.osgi.framework. So the bundle resolves but the package is not wired and when loading the OSGiActivator class it fails.
So the simple solution is to add the package import.
I rather recommend though to not define all the imports and let the maven bundle plugin figure them out automatically. You can do this by adding a ",*". You should then remove all the packages it figures out by itself.

ERROR [WsdlTestCase] Failed to create test step for [X]

I get this error ERROR [WsdlTestCase] Failed to create test step for [X] when executing my soapui project from Maven.
Tests work fine from the SOAPUI client software. My Soap-ui.error log is empty.
There doesn't seem to be enough information to let me debug this - I'm guessing there's some dependency I'm missing, but my tests are fairly simple (REST requests with a few assertions on the HTTP response). I assumed the core maven plugin would suffice.
My maven config is as below (I've also included the eviware repository -http://www.eviware.com/repository/maven2/)
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>2.0.2</version>
<executions>
<execution>
<phase>integration-test</phase>
<id>soapui-tests</id>
<configuration>
<projectFile>${basedir}/src/test/resources/MyTestSuite.xml</projectFile>
<outputFolder>${basedir}/target/soapui</outputFolder>
<junitReport>true</junitReport>
<exportwAll>true</exportwAll>
<printReport>true</printReport>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
the preceeding messages are :
[DefaultSoapUICore] Missing folder [/var/lib/jenkins/jobs/Build-Mule-Configuration/workspace/ext] for external libraries 2015-07-01 15:10:48,961 INFO
[DefaultSoapUICore] Creating new settings at [/var/lib/jenkins/jobs/Build-Mule-Configuration/workspace/soapui-settings.xml]
I've moved up to the 4.6.1 version of the soapui plugin as detailed here
(http://www.soapui.org/test-automation/maven/maven-2-x.html), the tests are now at least attempting to execute. As I understand it the version I've outlined above should work but perhaps it doesn't recognise the current SOAP UI project syntax.
As an aside the 4.6.1 version of the plugin seems to have a much larger number of dependancies and for me is causing memory issues.

Got NoClassDefFoundError when invoking Axis2 webservice deployed with maven in weblogic

I'm trying to deploy axis2 webservices in weblogic server using maven. The project has maven modules and one of that is a war that i have defined the axis servlet in. The wsdl was there, so i used wsdl2code plugin to generate the xmlbean and schema and put that in a jar module. Structure is as below.
--lv-ear (ear with dependency on war)
|
--lv-ws
|
--lv-ws-ccid (jar module with skeleton and xmlbeans)
|
--lv-ws-ecs (jar module with skeleton and xmlbeans)
|
--lv-ws-web (war module with dep on jar modules)
|
--WEB-INF
|
--conf/axis2.xml
--services/ccid/services.xml
I built and deployed the ear to weblogic domain. The war was deployed successfully as part of ear and services were deployed. I am able to access wsdl files. When I tried to call the service, i got the below ClassNotFoundException for a schema file.
Caused by: java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.s2104B1E6E09A2A85656B3E630BA151C1.TypeSystemHolder
I saw that the random string in that path differed fo me. So I tried to call again and got below NoClassDefFoundError, which persists even after I tried with different approaches.
java.lang.NoClassDefFoundError: Could not initialize class com.lv.ws.ccid.xmlbean.InputDocument
at com.lv.ws.ccid.xmlbean.InputDocument$Factory.parse(InputDocument.java:463)
at com.lv.ws.ccid.CcidMessageReceiverInOut.fromOM(CcidMessageReceiverInOut.java:332)
at com.lv.ws.ccid.CcidMessageReceiverInOut.invokeBusinessLogic(CcidMessageReceiverInOut.java:46)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:173)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:144)
I searched for this and found something that told to configure app server for axis2 based on http://axis.apache.org/axis2/java/core/docs/app_server.html . When I tried it I got below error.
weblogic.xml.stax.XmlStreamInputFactory can not be cast to javax.xml.stream.XmlInputFactory
After discarding that configuration, I did some other possible deployments by having the webservice skeleton and xmlbean files in an aar and put the aar inside WEB-INF/services. I also tried putting Class-Path entry in MANIFEST.MF in ear / war for the jar files, to no avail. Still I got the same NoClassDefFoundError. Can you please give me suggestions on fixing that?
Fixed it now. This was due to my lack of experience with Axis. Issue was that, I had the generated schema and xmlbean files moved to src folder and then just tried to use normal jar function and dependency to deploy.
Now, I removed them from src folder and use wsdl2code and axis2-aar plugins to generate the xmlbean and schema files dynamically and then package them in aar. Then I deployed the aar to webapp and it worked fine. I have listed the plugin configuration inside <build> below.
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.4</version>
<executions>
<execution>
<id>ccid-ws</id>
<goals>
<goal>wsdl2code</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.lv.ws.ccid</packageName>
<wsdlFile>${basedir}/src/main/resources/META-INF/ccid.wsdl</wsdlFile>
<databindingName>xmlbeans</databindingName>
<syncMode>sync</syncMode>
<unpackClasses>true</unpackClasses>
<namespaceToPackages>https://mdm.com/portal/ws/services/ccid=com.lv.ws.ccid.xmlbean</namespaceToPackages>
<outputDirectory>${basedir}/target/generated-sources</outputDirectory> <generateServerSide>false</generateServerSide>
<generateServicesXml>true</generateServicesXml>
<skipWSDL>true</skipWSDL>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.6.2</version>
<extensions>true</extensions>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>aar</goal>
</goals>
</execution>
</executions>
<configuration>
<aarName>ccid</aarName>
<includeDependencies>false</includeDependencies>
<outputDirectory>${project.build.directory}/aar</outputDirectory>
</configuration>
</plugin>
</plugins>

jaxws:wsgen fails to find Web Service

I have the jaxws-maven-plugin set up in the following way:
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-wsdl-for-random-num-generator</id>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
<sei>mydomain.sib.RandomNumberGeneratorEndpoint</sei>
<destDir>${basedir}\wsdls\</destDir>
</configuration>
</execution>
</executions>
</plugin>
When I try to run:
mvn jaxws:wsgen
I get the following error:
[ERROR] Failed to execute goal org.jvnet.jax-ws-commons:jaxws-maven-plugin:2.2:w
sgen (default-cli) on project soap-ws: No #javax.jws.WebService found. -> [Help
1]
The mydomain.sib.RandomNumberGeneratorEndpoint does have the #javax.jws.WebService annotation:
#WebService(endpointInterface = "mydomain.RandomNumberGenerator")
public class RandomNumberGeneratorEndpoint implements RandomNumberGenerator {
public double getRandomNumber(long limit) {
return Math.random() * limit;
}
}
Does anyone know what is wrong with my setup?
Cheers.
As it turns out, the issue was rather simple: I missed the genWsdl tag from the plugin setup.
The setup now looks like:
<configuration>
<sei>mydomain.sib.RandomNumberGeneratorEndpoint</sei>
<genWsdl>true</genWsdl>
<resourceDestDir>${basedir}\wsdls\randomNumberGenerator</resourceDestDir>
</configuration>
I did see this tag when I was reading through the documentation here - but I did not realize that was the problem... I dropped the ball.
Thanks anyway and hope this answer is useful for someone some day.

Resources