Setting fork and spawn in a maven plugin - java

We're converting a buildfile from ant to a maven plugin. We're trying to start/stop a database (hsqldb) in a maven plugin.
We succeeded to start the database. But we think that the plugin stops the database when the plugin is executed. The database should keep on running after the execution, but it seems to stop right away.
Our guess is that we should use the 'fork' and 'spawn' attributes (they are also in our build.xml from ant), but we don't have an idea how to implement them in our java class from our DatabaseController (which extends from AbstractMojo).
Any ideas?
We're using hsqldb, thisis the code how we initialize it:
hsqlServer = new Server();
hsqlServer.setLogWriter(null);
hsqlServer.setSilent(true);
hsqlServer.setDatabaseName(0, "database");
hsqlServer.setDatabasePath(0, "file:data/database");
getLog().info("Starting server!");
hsqlServer.start();
When we run the plugin, the database starts, we even managed to create tables and write data to it. Then the plugin stops, and the server stops automatically with it.
If we run another plugin, one to stop the server, we always get a nullPointerException at this line:
hsqlServer.stop();
Kind regards,
Jeroen

One thing you can do is, looking at existing maven plugins that does the forking of new java processes. Maven-surefire-plugin for one, do something similar. Surefire has a configuration (which will be specified in the pom.xml) called forkMode, which controlls the forking.
You may have to go through the source (svn checkout the code) of the plugin to figure it out.
I'm not much familiar with surefire much. But as a start, you may read the following class (#fork( Object testSet, Properties...)!
./maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java

Any code executed in maven is forked in another process. I worked with processes in some projects realm, it doesn't have a clear documentation about that.
You could call your code something like that:
Thread.currentThread().setDaemon(true);
hsqlServer = new Server();
hsqlServer.setLogWriter(null);
hsqlServer.setSilent(true);
hsqlServer.setDatabaseName(0, "database");
hsqlServer.setDatabasePath(0, "file:data/database");
getLog().info("Starting server!");
hsqlServer.start();
With an daemon thread, maven could thing witch your code is to be runned in background.
It´s supposition, but you could try it.

Related

how to add new azure function via maven plugin at intellij IDE

I want to use timerTrigger in azure functions.
I created function according this guide:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij
it is generated by default httpTrigger. I tried command of azure-functions:add.
I succeeded adding timerTrigger but I encountered with a lot of errors
How I can delete httpTrigger and declaring only timerTrigger?
before I run the azure-functions:add:
after running the command:
I guess it is related to azure plugin and maven plugin conflict but not sure and I don't know how to handle such this situation
any help will be appreciated
thanks
For now, I suppose it doesn't support create java function with default timer trigger. However you could delete the HTTP one then create the timer, cause you don't provide much information so maybe you could refer to my steps.
Create the function with maven.(I'm using the 1.22 version)
Delete the HTTP function, and delete the test folder.
Create the Timer Trigger function.
After this the project will be like this.(Before packaging remember set the storage connection string in the local.settings.json)
Package it.(Use the package method in the Lifecycle.)
Run it with azure-functions:run under azure-functions plugin.

Eclipse plugin: attach a java process to a code launch

So I am doing an eclipse plugin for educational purposes.
My goal now is to have an additional java thread running , when the Run button is hit.
So, alongside with the normally launched programme , I want to have my own plugin code running (which I specified in the plugin).
I thought about trying to create a new java thread , running my code when I execute DebugUITools.launch(config, mode); .
But I am not sure how I would attach the thread to the launched process so that I could stop the thread too...
I also thought about creating java launch configuration for my plugin code, but it's probably impossible because as far as I can see, we need a Project for that.
So, are there any possibilities to programmatically attach new threads to a launched program? Or maybe create a second launch configuration out of plugin code, and launch it?
I hope I was able to explain my struggle to you.
Each Launch runs the code in a completely new JVM. You can't take a thread in the current JVM and move it to a different one.
A second launch configuration in your code would only end up launching yet another JVM which doesn't get you anywhere.
You would have the modify the existing launch configuration to add a jar containing your code and change the program entry point to point to your code so you can start your thread.

Java Maven Build: Thread.sleep doesn't work (in eclipse it does)

I've got a strange error. My code is the following:
LoginQueue queue = new LoginQueue(server);
QueueTimer timer = queue.waitInQueue(this.username, this.password);
Thread.currentThread().sleep(6000);
while (!timer.isFinished()) {
if (timer.getPosition() != 0L) {
log("Queue Position" + server.toString() + ":"
+ String.valueOf(timer.getPosition()), LogLevel.DEBUG);
Thread.currentThread().sleep(3000);
}
My goal is to login to the League of Legends servers using the open source riotapi: https://github.com/loldevs/riotapi/
The problem isn't LoL specific, so no problem there. When I create the QueueTimer, it needs some time to retrieve some data from the server, otherwise timer.getPosition() will throw a NullPointerException. That's why I wait 6 seconds before going on. In Eclipse debugging it all works fine, but after exporting it with m2eclipse (clean package as goals; I'm new to Maven if that matters), the compiled JAR doesn't sleep. It just goes on and then throws my NullPointerException because it didn't wait. Why does my code behave in Eclipse and doesn't work outside? What am I doing wrong? I've tried Thread.sleep() instead of Thread.currentThread().sleep(), doesn't work either.
Edit:
I changed it to:
LoginQueue queue = new LoginQueue(server);
String authkey = queue.waitInQueueBlocking(this.username, this.password);
So I am using synchronous methods now. Now I get the following stacktrace:
Exception in thread "Queue Timer for LeagueAlerter" java.lang.NullPointerException
at org.apache.cxf.jaxrs.client.AbstractClient.setupOutInterceptorChain(AbstractClient.java:840)
at org.apache.cxf.jaxrs.client.AbstractClient.createMessage(AbstractClient.java:902)
at org.apache.cxf.jaxrs.client.WebClient.finalizeMessage(WebClient.java:1068)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1049)
at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:854)
at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:825)
at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:393)
at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1582)
at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1577)
at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.post(WebClient.java:1517)
at org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl.post(InvocationBuilderImpl.java:141)
at net.boreeas.riotapi.loginqueue.LoginQueue.getAuthToken(LoginQueue.java:54)
at net.boreeas.riotapi.loginqueue.QueueTimer.run(QueueTimer.java:89)
The error seems to be somewhere in the Riot API. But why does it work inside eclipse? I am too stupid to understand the API source, would be so cool if someone helped (Github is https://github.com/loldevs/riotapi/ ). My pom.xml: http://pastebin.com/jik5ba8J
Hey fellow ${search engine of choice}ers!
I had the same problem when running the .jar of my application which was built by maven.
Turns out that
I was using the maven assembly plugin to generate the jar. This plugin will (at least by default) put all the dependency resources directly in the produced jar file
CXF has some overlapping resources (which means trouble because of pt. 1)
I replaced the assembly plugin with the one-jar plugin (which adds dependencies as jar files in the produced binary) and after this everything worked fine.

Detect application mode (DEV, TEST, PROD) during build in Play Framework 2.x application

Good day all,
I'd like to be able to detect the mode that a Play application will use during build. Meaning I'd like to execute certain tasks within my Build.scala/build.sbt depending on whether the application is started in DEV or in PROD mode for instance.
The reason I need this is because we (the team) have implemented Grunt.js into the build process by adding it to the playRunHooks. Depending on whether the application is running in DEV mode or not we want to enable/disable some Grunt tasks.
I know I can check the application mode from within the actual application using Play.isDev and the like, is there a similar mechanism available for within the build files?
If not I would really only need to know the command that was issued by the developer (run, start, dist, stage etc.) but I can't seem to find a straight forward way of getting to know this either.
Can anyone point me in the right direction? Thank you in advance!
Any build tasks that are added to playRunHooks are only executed on "play run". If you do "play stage" or "play dist" those tasks are never executed.
The reason I need this is because we (the team) have implemented Grunt.js into the build process by adding it to the playRunHooks. Depending on whether the application is running in DEV mode or not we want to enable/disable some Grunt tasks.
Since you say build process and are looking to hook into the application when it's running in prod mode I think the place you're really looking to hook into is the dist command. In that case you'll want to create some tasks for your build file and get creative with .dependsOn to incorporate them.
SBT has a way of running external processes such that you could define a simple inputKey in your build.sbt file like so:
val doGulpRelease = inputKey[Unit]("Runs gulp --release")
doGulpRelease := {
val s = streams.value
s.log.info("Preparing to run my task")
"gulp --release" ! s.log
s.log.info("Done with my task")
}
Then, to hook into the dist process of play, you can set it to depend on a task created from the above:
dist in Universal <<= (dist in Universal).dependsOn(doGulpRelease.toTask(""))
This will cause your dist command to also run your custom build command, which in this case would do whatever the release task was defined to do.
In the same vein, if you need some build process to run for your tests, use test in Test and dependsOn to run whatever you need to. It seems to me you're asking an XY problem but if is the case that you're not and you really do need to run grunt subprocess's from within your running play app then you'll need to use the Global Object and hook into the onStart/onShutDown hooks and create some type of job runner for yourself. You could start here for some hints on running background tasks in play and besides that google is your friend.
Note: You may need to do some imports at the top of your build.sbt file to use the above code, and it will also depend on your sbt version, but with 0.13.5 I believe it is:
import sbt.complete._
import complete.DefaultParsers._
import com.typesafe.sbt.packager.Keys._

Vert.x fails to "Run your module and see your changes immediately"

I am working with the Vert.x Gradle template hosted at the Vert.x Github space.
The build file suggests that there is a runModIDEA target that runs IDEA-built class files so that rebuild/redeploy is not required to pick up changes:
runModIDEA - run the module from the project resources in IDEA. This allows you to run the module without building it
first!
... yet the task does not exist per ./gradlew tasks.
I am not tied to this particular build task per se.
I just want a working auto-redeploy solution that enables me to see updates without a two minute rebuild/redeploy cycle.
EDIT: I also tried running it directly, pointing to InteliJ IDEA output classpath. It works fine, but doesn't pick up changes.
vertx runmod com.mycompany~vert-x-reverse-proxy~1.0.0-final -c conf.json -cp out/production/vert-x-reverse-proxy
EDIT: I also tried ./gradlew runmod -m, first changing vertx_classpath.txt so that the IDEA files (out/production) are looked at first. Still no redeploy. In fact, while it was running, I deleted the out directory and it continued working.
EDIT: I also tried vertx run com.mycompany.myproject.ReverseProxyVerticle -c conf.json -cp out/production/vert-x-reverse-proxy... same results. It ran as expected but did not pick up changes. Only way to pick up changes was to gradlew clean and re-assemble.
EDIT: I have been through these instructions as well.
For anyone who stumbles upon this question, I had the same problem and managed to fix it by deleting everything under the /mods folder in the /target directory. This is in fact mentioned in the vertx documentation - though maybe could be a little more emphatic. Once everything under /mods is removed, start up the application and it redeploys whenever anything is changed.
If you are new to vertx and stumble with this problem or similar, it might be worth to have a look at this vertx google group entry. It describes the changes that need to be done to the generated project by the Vertx Gradle Template to get it running.
I know, this does not answer directly the question posted here but I hope it helps you further.

Categories