Apache Ant with Graal VM - java-11

We use Apache Ant with Nashorn JavaScript Engine, which became deprecated and removed in jdk 15 and up. I trying find how to switch from Nashorn to Graal VM and didn't find any usable information even on Apache web-site. Please advise which jars I need and were should I put them. what need to be changed in code we have. If somebody already did it, please share your experience.
I have a sample, which run against jdk1.8.0_311:
<?xml version="1.0" ?>
<project name="test" default="test">
<property environment="env"/>
<target name="test" >
<script language="javascript">
<![CDATA[
load("nashorn:mozilla_compat.js");
importPackage(java.time);
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
self.log("This script is for Test Of Nashorn Javascript Engine");
print ("Today is: " + date );
]]>
</script>
</target>
</project>
Result looks like:
test:
[script] Warning: Nashorn engine is planned to be removed from a future JDK release
[script] This script is for Test Of Nashorn Javascript Engine
[script] Today is: 2021-11-3
BUILD SUCCESSFUL
Total time: 0 seconds

You could also add standalone Nashorn as a dependency to your Ant build; putting it in your Ant lib directory should work. By default that's $ANT_HOME/lib but some Ant installations use a different location, e.g. homebrew-installed Ant will use /usr/local/share/ant. If you don't want to put it in the lib, you can also put it with your project and use <classpath> or <classpathref> within the <script> tag to point to it.
Mind you, standalone Nashorn also needs ASM 7.3.1 on the classpath. JARs for both can be downloaded from Maven Central.

If you are using JDK 15 or above. Please add the below jars to the path.
Openjdk nashorn jar
asm jar
asm util jar

Related

What is required to run a hello world in kotlin with IntellJ

I have a Mac (Catalina OS) with Java 15 and IntelliJ 2020.2 (community edition) installed. The kotlin plugin is also installed. When I create a new Kotlin Project, add a main function, then there is way to run it, there is no 'Start' Button next to the main function. The problem is obviously that it doesn't find the runtime. The iml file looks as follows
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
When I click on 'add framework support', I can add a new KotlinJavaRuntime which then just appends an <orderEntry> to the iml file with the name KotlinJavaRuntime (2).
I don't want to use maven or gradle here. I know that gradle can resolve the dependencies for me. I just want a plain vanilla Kotlin hello world project. The documentation has a 'Getting Started with IntelliJ IDEA' section which just mentions the Kotlin plugin for IntelliJ. There seems to be no requirement to have gradle or maven installed, the plugin should do it.
I know that you can use brew install kotlin to install the SDK manually, but then the documentation is wrong and the plugin alone cannot run Kotlin with a prior manual installation of the SDK or the usage of maven/gradle.
I think I know what happened in this case.
The Kotlin Code was loaded as an 'IntelliJ' Module inside a Java project that was using maven. In IntelliJ there's a hidden folder .idea/libraries/ which was missing the description of the Kotlin Runtime (this makes sense, since the dependencies were managed via maven). I have restarted and created a new project from scratch. This time you'll see the file .idea/libraries/KotlinJavaRuntime.xml, which is exactly what is referenced from the iml file. The .idea folder is very magic. For me the confusion is that modules in IntelliJ are not really isolated from the project, they share - like in this case - the .idea folder.
I hope this helps also other users.
If you created the kotlin-file yourself, you have to tell IntelliJ that it should run this file.
Normally shows a run icon next to your main method.
run button/icon
Option 2: Right Click on the file containing the main method and see if there is a run this file option.
Option 3: add run config manually
Click add config which should be on the left of the run button (don't mind that it is disabled)
Click + and choose Kotlin.
Set main class to MainKt which is the default for kotlin

IBM MobileFirst: using external jar files during command line build

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.

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.

What is the Ivy equivalent of Maven's versions:display-dependency-updates?

I have an ivy.xml file where I specify my dependencies explicitly. Is there any functionality built into Ivy that will let me discover or automatically update my dependencies which are out of date?
I don't want to use latest.release because I want a completely stable and reproducible build. But every once in a while I'll want to update some dependencies and at the same time it would be good to answer the question, which other dependencies are out of date?
Like you, I only use dynamic versions for in-house dependencies. When upgrading, at the start of a new development phase, I would use one of the repository search tools to discover new versions of 3rd party libraries:
http://mvnrepository.com/
http://mavencentral.sonatype.com/
As I'm sure you're aware, another problem is that upgrading dependencies can often lead to an involuntary upgrade of your transitive dependencies....
What I'd suggest is to generate an ivy dependency report and use this to review your code's module usage. I find this very useful especially considering that some 3rd party Maven modules are not well behaved and will import many unnecessary libraries onto my classpath.
The following is an example of my standard dependencies target:
<target name='dependencies' description='Resolve project dependencies and set classpaths'>
<ivy:resolve/>
<ivy:report todir='${ivy.reports}' graph='false' xml='false'/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="provided.path" conf="provided"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
<ivy:cachepath pathid="test.path" conf="test"/>
</target>
Hope this helps.... If you find a way to automatically manage this I'd be interested.
One workaround is to use ivy:makepom and then run mvn versions:display-dependency-updates using the generated pom.
I'm not sure if this is the best solution or not, but you can create a configuration (e.g., "checklatest") that asks for the latest versions, then run a report against that.
For example, in your ivy.xml file:
...
<dependencies>
....
<dependency org="somegroup" name="somename"
rev="latest.release" conf="checklatest->default"/>
</dependencies>
and then run an ant task that uses the task for that configuration.
Even there, it isn't necessarily going to pick up the latest version -- e.g., Apache's commons-httpclient eventually got incorporated into the httpcomponents project, so a request for the latest "commons-httpclient" in group "commons-httpclient" will only find version 3.1. But if you look at the publication date on the report Ivy generates, it should be fairly clear that something happened, when the latest publication is 2007. At that point, you would have to investigate.
checkdepsupdate is the rough equivalent in Ivy.
It gives you output like:
[ivy:checkdepsupdate] com.sun.mail#javax.mail 1.5.4 -> 1.6.2
[ivy:checkdepsupdate] commons-codec#commons-codec 1.10 -> 1.11
[ivy:checkdepsupdate] org.apache.commons#commons-compress 1.12 -> 1.18
[ivy:checkdepsupdate] commons-dbutils#commons-dbutils 1.5 -> 1.7
[ivy:checkdepsupdate] commons-io#commons-io 2.4 -> 2.6
[ivy:checkdepsupdate] org.apache.commons#commons-lang3 3.6 -> 3.8.1
[ivy:checkdepsupdate] org.apache.commons#commons-text 1.1 -> 1.6
[ivy:checkdepsupdate] org.apache.poi#poi 3.13 -> 4.0.0

Using Maven ant task to install jar to local repository

At the end of my ant build id like it to call the equivalent of the command line call
mvn install:install-file -Dfile=my.jar -DgroupId=com.company.project -DartifactId=my_project -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
so that it will add the newly built jar to a maven repository which another project will rely on.
Ive tried using the maven-ant-task and have added the maven-ant-task jar to the ant built project and the following code to the build.xml:
<target name ="minstall" depends="jar">
<artifact:pom id="maven_install" file="maven_install.xml" />
<artifact:install file="${out.dir}/my_project.jar">
<pom refid="maven_install"/>
</artifact:install>
</target>
but seem to be missing something as it wont work for me. To begin with i get the error in the build.xml (ant build file) saying
The prefix "artifact" for element "artifact:pom" is not bound.
What am I doing wrong. I am fairly new to ant?
On a realted question what is the purpose of the associated POM file? I would not normally have a POM in this project as it is an ant build
Perhaps maven-ant-task jar is not installed, i.e. not in your ant CLASSPATH. You can follow this instruction for this.
As mentioned previously, you need to make sure the tasks are defined in your ant script, and the artifact namespace is understood.
The POM file is used (in this case) to tell the Maven repositories the dependencies of the JAR you are putting in the repository. The POM should also specify the JAR's identification information (groupId, artifactId, version number, license, etc.).
Strictly speaking, you do not need an external POM, you could define the information in your build.xml file as follows:
<!-- Assuming tasks defined, and 'artifact' namespace exists -->
<artifact:pom id="maven_install" groupId="com.whatever" artifactId="some-jar"
version="1.0" packaging="jar">
<dependency groupId="..." artifactId="..." version="..."/>
<dependency groupId="..." artifactId="..." version="..."/>
<license name="apache" url="http://www.apache.org"/> <!-- can be omitted -->
</artifact:pom>
<target name ="minstall" depends="jar">
<artifact:install file="${out.dir}/my_project.jar" pomRefId="maven_install"/>
</target>
When you install the JAR in the 'minstall' task, the POM should be generated with the appropriate dependencies in the local Repository.
That message means you are missing an xmlns:artifact attribute in your build.xml. Have a look at the installation page in the docs for an example.
As to the purpose of the POM file, it's mostly metadata so that maven can figure out dependencies properly. In a real maven build it also describes how to build, test and package. But in your case all that is done by ant instead.
I think that it makes no sense to put such commands in Ant's build.xml. If you want to have your jar file installed in your maven repo just use mvn install command.
Besides that, I guess that you are somehow confusing the purpose of Maven and Ant tools in your project. What I'd suggest is to use Maven as your main build tool. You can configure invokation of Ant targets in your POM file if you really need that. Personally, I think it is the best solution to have Ant called by Maven. Maven goals (such as clean, test, package, install and so on) are very simple to use and powerful (I guess that you can read it in every Maven tutorial).