Calling Groovy Scripts/Function From Java (Gradle) Project - java

I've a Gradle Java project. Currently we're not using Groovy at all so no configuration for Groovy at all. Now I need to provide support for groovy scripts/class/function(s) which can be called from Java code.
To give you context - We need to transform some fields before writing them in a file. We want that user himself can write those transformations and need not to compile code. And we should be able to call those transformations before writing to file. For this we're thinking that we'll be writing Groovy for those transformations...
So if someone can guide me that how to achieve this step by step, Means what to add/subtract in gradle file .. add src/main/groovy package/classes... I'll really appreciate it.
I tried to search on internet but could not find any help...

Related

What's the recommended way in Java to make a piece of code available only when needed?

The question regards an endpoint that I want to make available only for demoing and should not be part of the project in production. Therefore I need to find a way of making the piece of code that reveals this endpoint available only when it should be.
I thought of using a different .properties file when it is needed, but this requires creating another one and changing the configuration and if there is a more simple way I would like to know.
Maybe building with a different Maven profile? Can I use the Maven profile name inside the code?

IntelliJ DSL plugin provide completion of Java classes and methods

I'm attempting to write an IntelliJ plugin for a DSL which references Java classes and methods. The DSL is exposed in *.conf files within a Java project. A typical snippet of the DSL looks like this:
TASK taskClass=com.example.Foo taskMethod=someMethod;
I've been working through the IJ 'Simple' plugin tutorial to learn about plugins and am able to implement a fair bit of my own plugin. Currently, however, I'm stuck on trying to understand how I can provide auto-completion for the taskClass and taskMethod keywords. Having worked through the Simple tutorial all my completion shows is 'Hello'. Now I want to be able to extend my CompletionContributor to show java classes and methods that exist in the project. This doesn't seem to be addressed in the tutorial, but perhaps I'm just missing it.
What do I need, in order to do this? I would guess that there is already some cached info about all the existing java code in a project that my Contributor could leverage.
I think you are looking for the existing stub indices.
For example, to get all class names in your project, you can use
val classNames = JavaFullClassNameIndex.getInstance().getAllKeys(project)
When using indices - your own or already existing ones - it is often useful to use the Index Viewer plugin so you can have a look at which things are in which index.
To add it as a dependency of your plugin (so you don't have to install it manually each time you build your plugin) add the following to intellij block of your build.gradle.kts:
plugins.set(listOf("java", "com.jetbrains.hackathon.indices.viewer:1.19"))
It should appear as a tool window on the right.
Don't forget to remove it once you are done with debugging the indices.
I also found the PsiShortNamesCache which might be useful.

Use and Configure Maven by Java program, using Maven Java API

I'm using Maven Java API to configure Maven in a custom Java project.
In particular I need to configure some Maven settings, among which there are proxy settings.
How can i do this? I googled a lot, but I found no examples on how to use Maven from Java.
Can You give me an example or a guide, a snippet of code, whatever you want to clarify HOW TO USE (AND CONFIGURE) Maven by Java API, i.e from Java code?
I found this maven reference, but what do I specifically need?
Thanks in advance.
I've already seen this question, but unfortunately there is no mention on how to edit settings.xml from maven api, I suppose it is possible, but I'm not sure of it, so I asked a new question, wider than that one, how can I manage Maven from Java? settings, run, properties, whatever... is it possible?
For example, about settings management, I found this API maven-settings, it can be useful? It's "read-only" API? I guess it isn't, but I've found no way how to "write" modifications to file, there are no examples on how to use it.
Well, yes, you are a bit crazy. You can take a look at some plug-ins which modify pom.xml files. For example, the versions-set facility shown here:
http://www.mojohaus.org/versions-maven-plugin/set-mojo.html
The source code for that plug-in will show you how to modify pom.xml files, but you also want to modify the settings.xml file.
All of these files are XML. Basically, you want to obtain a DOM for the .xml file. So, you can use generic XML tools to (1) read the file, (2) modify the document model, (3) write the data back to disk.
Note well: Maven caches the .xml files. You have to stop the maven executable and restart it to force it to re-read the .xml files. It sounds like you'll probably be doing this as a matter of course. :-)

Java code to use variable value depending on build flag

makefile that compiles all java files.
The way I have done this multiple times in past is to generate a Java file depending on the flag. If you are using ant then this code generation is very simple. Otherwise, you can use a template file with placeholder and do some shell-scripting or similar to generate the file.
In ant you can use the replace task to modify files as part of your build.
We do this in our builds, but we use it to modify a Java .properties file which the application will read for its configurable behavior.
I've written rather pleasant flag-controlled systems using a combination of Google Guice and Apache CLI to inject flag-controlled variables into constructors.

calling plugin from java code

hi i am using eclipse as IDE for the development of my application. I have one doubt.
I have one plugin that is capable to creating a class on from one xml file. Now, the problem is that i have many xml files and classses to generate..
can anyone please tell me how to invoke the plugin from my java test class, so that i can create classes all together..
Please help me.
I'm not sure which plugin you have in mind, but considering that Eclipse is written in Java itself, you are likely to be able to find a suitable jar file that implement what you need. How its API is documented depends on the plugin (in some cases, you might even be able to find its source code). It's possible that using such a plugin may require using OSGi, since it's what Eclipse uses for its plugins.
However, if your goal is to generate classes from XML (presumably XML shemas) there are libraries for this that you can use directly, for example:
JAXB
Apache XML Beans
Relaxer (for Relax-NG, not for XML shema, with some brief documentation in English within the zip file)
The Eclipse Dali Plugin is able to generate Java classes from an XML schema. In the link below see the section on "JAXB Class Generation".
ttp://www.eclipse.org/webtools/releases/3.2.0/NewAndNoteworthy/jpa.php

Categories

Resources