IBM MobileFirst: using external jar files during command line build - ibm-mobilefirst

We are trying to use a org.JSON.JSONObject library for some intense json processing in the adapter side. We have Java classes which processes the data received from http adapters.
mobilefirst 6.3.0 and using cli 20150701 build
(the recent one).
This JSON referencing has no issues when building from eclipse mobilefirst studio environment.
We are building this environment in ubuntu linux 14.04. There is an error in referencing org.JSON.JSONObject..x.jar file when we execute
mfp start or mfp build or mfp deploy
Is there a way to reference this jar file during mfp start or mfp build or mfp deploy as a classpath.
We need to have something like
mfp -classpath "path/to/json.jar" build
please help.

As it turns out, the CLI does not yet recognize jars placed into the server/lib folder of your project. In oder to make this work, you can make a simple edit to the following file:
[cli install folder]/mobilefirst-cli/node_modules/generator-worklight-server/lib/build.xml
At or about line 123, add the third fileset element shown below:
<!-- Classpath for server runtime libraries used when building the WAR -->
<path id="server-classpath">
<fileset dir="${worklight.jars.dir}" includes="worklight-jee-library.jar" />
<fileset dir="${worklight.server.install.dir}/wlp/dev" includes="**/*.jar" />
<!-- add server/lib folder to classpath -->
<fileset dir="${worklight.app.dir}/../server/lib" includes="**/*.jar" />
</path>
After that, running 'mfp start' (or 'mfp restart' if your server is already running) will compile your custom Java code with any jars that you add to the server/lib folder included in the classpath.

JARs for use by your adapters should be added to you Project's server directory in the folder lib. They will be included in your Projects WAR file when the project is built (in Studio or by the ant tasks) and when you deploy that WAR it will be visible to your adapters.

I agree with #bjustin_ibm. Thanks for that. While the above approach works, there's also another way of doing this.
Alternative hack
Just add your required .jars to the following location, it gets added to the classpath during mfp start
/home/instanceubuntu/.ibm/mobilefirst/6.3.0/server/wlp/dev/spi/third-party
This solution is more simple and doesn't really have to maintain the build.xml file.
Hope this helps.

Related

Where is the "jrebel JAR-file" in the my WAR-file?

I want to use jrebel with intellij IDEA and JBOSS AS7.
(have a web application(so have WAR)).
By apache-ANT , build my-WAR and deploy it under JBOSS AS.
I know how introduce any changes of classes or resources to jrebel in the my-WAR (If I'm not mistaken!) , as follows:
(In rebel.xml)
<classpath>
<dir name="D:/project/myProject/out/production/myProject">
</dir>
</classpath>
<web>
<link target="/">
<dir name="D:/project/myProject/resources">
</dir>
<dir name="D:/project/myProject/view">
</dir>
</link>
</web>
But, really, "jerebel jar-file" Where is the my-WAR?
in => "warFile >WEB-INF > lib"?I did not see it.
please help me.
jrebel.jar is packaged inside the JRebel plugin for IntelliJ IDEA. It doesn't have to be deployed with the WAR itself. JRebel plugin will set the correct JVM parameters to the command line when you start via "Run with JRebel" or "Debug with JRebel":
-javaagent:/path/to/jrebel.jar
UPDATE: the newer versions of JRebel are configured using -agentpath JVM option instead:
-agentpath:${JREBEL_HOME}/lib/<platform-specific-binary>
See the documentation reference of the correct settings.
You don't have to do it yourself if you start the server from the IDE. If you start the server from the command line then you would have to add the JVM argument yourself with the correct path to jrebel.jar as described here: http://manuals.zeroturnaround.com/jrebel/standalone/launch-from-command-line.html#jboss-7-x
Java agents intercept class loading and thus have to be loaded before other classes. As you might have guessed, jrebel.jar is a Java agent and therefor nothing requires it to be packaged inside a WAR.
Instead, rebel.xml, the configuration file, has to be packaged in the WAR, in WEB-INF/classes directory. JRebel uses rebel.xml to detect, where the compiled classes and resources are. So when the application is deployed, JRebel finds rebel.xml configuration file and won't load the application classes from WAR itself, but instead it will use the path that is specified in that rebel.xml file. This is why rebel.xml has to be inside the war, as you may also start the server from command line instead of the IDE.

How to add Java classes to a Worklight adapter when using CLI

I am trying to add Java classes to my adapter as described in the tutorial Using Java in Adapters. However, I am trying to do that from the CLI.
When I do:
wl create WLProject
cd WLProject
wl add adapter
copy the Java class (Calculator1.java) to server/java/com/worklight/customcode
wl start
The build process fails and no war file is created;
When I do:
wl create WLProject
cd WLProject
wl add adapter
wl start
copy the Java class (Calculator1.java) to server/java/com/worklight/customcode
wl build
wl deploy
wl invoke
The invocation fails. When I inspect the war file, the Calculator1.class has not been deployed.
So the question is: how can I add Java classes to an adapter when using the CLI?
Sounds like a bug to me. It looks like the build process fails when introducing server-side artifacts.
This might be not currently supported in the CLI, but I can't imagine such a limitation...
I've opened a defect to have this looked at.
You can open a PMR (support ticket) to receive a fix if/when available.
If I do the following,
the .war file is being generated.
wl create myTestProject
cd myTestProject/
wl build
But if I do this,
the .war file is not generated which is why everything else then fails:
wl create myTestProject
mkdir -p myTestProject/server/java/com/worklight/customcode
cp Calculator.java myTestProject/server/java/com/worklight/customcode
cd myTestProject/
wl build
There's an invalid classpathref in the build file that's producing the WAR.
You can edit [CLI Install Location]/worklight-cli/node_modules/generator-worklight-server/lib/build.xml to fix this issue with the classpathref.
If you're not planning on using any server runtime libraries:
On line 132, you can remove the attribute classpathref="server-classpath".
If you plan on using server runtime libraries:
You'll need to add the "server-classpath" to be used when compiling your code. Before the build-WAR target in the build.xml file, you can add the following
<path id="server-classpath">
<fileset dir="${worklight.jars.dir}" includes="worklight-jee-library.jar" />
<fileset dir="[your home directory]/.worklight/6.2.0/server/wlp/dev" includes="**/*.jar" />
</path>
Please note that the second fileset, you'll have to change [your home directory] to the appropriate path. Adding both of these filesets includes the worklight runtime and the server runtime when compiling your classes.
When you've made your changes:
Restart the server. At this point, the Calculator1 class should be added to your WAR.

Configure IDEA to deploy using ivy

I have a project that uses IVY. My build script resolves the dependencies well.
I have a library (lets call it Project_libs) configured in IntelliJ iml file so that when I deploy the application the jars get published to the server.
I have an ant task ide-setup which copies the necessary jars from Ivy local repo to Project_libs so these jars are available to IDEA during deployement.
Lets say I make changes to a common library like utils.
Build the common library.
Go to the projects that declared this common library as a dependency and run ide-setup
Then start the server.
Is there a direct way in which I can configure IDEA to read the dependencies from the ivy.xml file instead of doing this convoluted process of setting up the whole thing.
I don't know anything about InelliJ IDEA, but I believe you could accomplish what you want using ant and IVY. From the IVY documentation
<ivy:buildlist reference="build-path" ivyfilepath="ivy/ivy.xml" leaf="mymodule">
<fileset dir="projects" includes="**/build.xml"/>
</ivy:buildlist>
Builds a list of build.xml files sorted according to the ivy.xml files found in an
ivy directory relative to those build files. Only build.xml files of modules which
have dependencies (direct or transitive) on mymodule are put in the result list.
So if you set leaf equal to utils, it could return you a list of build.xml files that depend on utils in dependency order. Then all you would have to do is go through those build.xml files in order and invode their build process (probably through a subant call)

Intellij IDEA: Server Configuration cannot be run cause file does not yet exist. Use maven goal result as artifact?

until now I deployed to a Glassfish server with the default IntelliJ artifact "Web Application: Archive". Now my built process got a bit more complicated with maven modifying several files.
In the glassfish configuration under Deployment, I substituted the Artifact to deploy with the direct war file and put the maven goal in "Before Launch: Another Configuration"
However, if mvn clean was run the war does not exist and I cannot run the configuration (because the external file doesn't exist).
Can I avoid having to run mvn package once manually? E.g. by making the output of a maven goal an Artifact?
Thanks!

how to manage dependency of shared libraries using IVY

I have a EAR -
This EAR contains multiple WAR
Inside these WAR files there is a common JAR which is getting downloaded within each WAR file.
I want to remove this common JAR file into a shared Library. Also i want the latest version of JAR file everytime i rebuild my WAR, I am using Tomcat in Dev and Websphere min production.
We are using IVY for dependency management, Now. I have removed the common jar from the war file, but i dont know how to access the latest version of jar and download it in the shared library everytime i restart my server.
Can anyone help on this.?
http://ant.apache.org/ivy/history/latest-milestone/ivyfile/dependency.html
You'll need to add a dependency to your ivy.xml file, something along the lines of:
<dependency org="com.orgname" name="jarname" rev="latest.release"/>
Is this a JAR file you make yourself, or is it publicly available? If you make it yourself, you will have to publish it in a repository somewhere in your system.