PlayPlugin onApplicationStart does not work - java

I try to write a small playframework-module and a plugin in it, and want be sure that it will start when I'm starting up the application.
[myapp] -- uses --> [registration(module)].[plugin(RegistrtionPlugin)]
There is my plugin class I've put to /src/play/modules/registration int he module source
public class RegistrationPlugin extends PlayPlugin {
public void onApplicationStart() {
Logger.info("Yeeha, firstmodule started");
}
}
I have dependency to this module form my application, when i do play deps - it shows me that everything good. Or when i type in console in my app-home folder: play registration:hello - it replies me 'Hello' back.
I've got this code from PlayFramework Cook book (chapter 5), but can not make it working as I expect.

Have you added a play.plugins file to your module src folder configuring the RegistrationPlugin plugin?
The play.plugins of e.g. the spring plugins looks like this:
1000:play.modules.spring.SpringPlugin
You might also take a deeper look at the spring plugin:
At last before the module can be used following command has to be executed:
play build-module

Try to move your RegistrationPlugin to regular /app folder
and don't forget to include path to plugin in /src/play.plugins file.
f.e.
1000:RegistrationPlugin
if RegistrationPlugin is in default package

Related

Multiple steps for spotless format java error while gradle build

There is an IntelliJ project using Gradle, Java and in one of the modules, I put the Java code which is working independently. But on moving the code within this project and running the build.gradle I am getting the following error:
Could not create task ':wlcr-auto-api:spotlessJavaApply'. Could not
create task ':wlcr-auto-api:spotlessJava'. Multiple steps with name
'google-java-format' for spotless format 'java'
I am not sure what is the reason for this error. Also, I am not sure if the project structure is correct in Intellij.
In the above image auto is the project that was already created. I created a new module auto-api and put all my code inside it. There is an Application.java inside it, the main file to run the spring boot application. It works when the code is not put inside this auto project. But not when included in the project.
I have no clue how to debug this.
check out your build.gradle - probably you missed something inside block:
spotless {
...
}
at least, if you simply remove it - the issue will gone

How to run only one (current) file and ignore others in intellij gradle project?

This must be a very simple task for many of you. Let me explain the scenario.
I recently started practicing questions on HackerRank. But, I found the platform not so friendly for debugging. No online platform is or can be because of its own limitations. HackerRank provides question and stub code for many of the problems on its problem page.
For example, let us consider, https://www.hackerrank.com/challenges/java-datatypes/problem
But, because of it's debugging limitations I can't make the best use of portal. Hence, I wrote a PHP script to scrape all the content from the website and generated problem statements in HTML/PDF formats and solutions in java format.
Here's the GitHub project for the same.
https://github.com/saint1729/hr-idea-integration
The main intention of this activity is to have an integration of the website with an IDE like Intellij IDEA. This is now complete.
I created a gradle project with existing sources. But, the project contains many java files (almost 500+ files and each file has it's own main method). My intention is to solve one problem at a time and see if it compiles and submit it using a REST API provided by HackerRank.
But, when I am trying to Right Click and Click on Solution.main() for every file, it tries to compile all files in the project and because there are some compilation issues with the project, I am unable to test my code for the current file. This is not productive for me.
Please let me know if it's possible to compile and run a single file in IDEA (without compiling the whole project). If the idea of creating a gradle project for this activity is not necessary, can somebody recommend me another efficient solution?
NOTE: Every scraped java file contains it's own main method. I know that a project can contain only 1 main method. But, I don't know a coherent solution to solve my problem.
If you want to continue using gradle, you create a module per solution.
Let's suppose you have 3 solutions. canyouaccess, duplicateword and java1darray.
So your repository looks like this:
java
canyouaccess
src/main/java
package
Solution.java
duplicateword
src/main/java
package
Solution.java
java1darray
src/main/java
package
Solution.java
build.gradle
settings.gradle
Each module can have its own main. Inside a settings.gradle file the modules can be defined or disabled by commenting it out.
Your build.gradle looks like this:
...
subprojects { project ->
apply plugin: "java"
sourceCompatibility = 11
}
...
For the settings.gradle looks like this:
include 'java:canyouaccees'
include 'java:dublicateword'
include 'java:java1darray'
Each module can be build separately, you could even group modules by creating a sub module structure.
And each module can have it's own debug configuration, where the module and the main is selected. If your set them as shared, they are stored in xml format under .idea/runConfigurations. So your script can create them as well.
Each module needs it's own gradle.build file, where the main class is declared.
jar {
manifest {
attributes('Main-Class': 'your.main.class.goes.here')
}
}
Something like this should do.

Developing application with plugin support in Java

I have been researching on how to develop an application that can load plugins.
So far, I've seen that this can be done by defining an Interface, and have the plugins implement it.
However, my current issue is on how to load the plugins when they're packed in Jars. Is there a "best" way to do it?
The current logic I'm thinking of is to get each plugin and inside their Jar look for the class that implements the Interface. But I don't know how to do such lookup. I think that this logic may not be a good one, but I couldn't find any useful information on this specific topic.
**Edit1: **
Adding more information:
The intended plugins would be Jar files contained inside a subdirectory where the main application's Jar would be located, like this:
Application's folder
|- Main_Application.jar
|- Plugins
|- Plugin1.jar
|- Plugin2.jar
|- Steve's_plugin.jar
And so on.
What I expect is that the Application will be able to load all plugins inside the folder at runtime. So in the code, it would only be aware that the plugin's folder should exist and there should be Jars inside such folder.
Let's say I have a plugin interface like this:
interface Plugin
{
public void run();
}
Plugins would be identified by a class that implements such interface, like so
class Plugin1 implements Plugin
{
//attributes and other methods
#override
public void run()
{
//something happens here
}
}
class Plugin2 implements Plugin
{
//attributes and other methods
#override
public void run()
{
//something happens here
}
}
The Application should be compiled only once, and be able to load any Plugins added to the folder when it is executed.
For the Application to be able to load any Plugin, do I need to establish rules on the contents of the Jar, like package name and the class that implements the interface? Or it is expected that the class implementing the plugin interface could be in any package within the Jar, and have any name?
This is the more generic approach to what I would like to do with such plugins. In short, I'm planning to build an Application that will have tabs, and each plugin will provide the Interface and Functionality of each tab. I'm trying this because I want to be able to maintain each tab separately, and don't want to recompile the whole application because of changes in only one component that don't affect the others at all.
Get the list of plugin jars:
File[] jars = new File("Plugins").listFiles();
Then, use the code from this answer about loading all classes from a JAR file, but run it once for each file in jars whose name ends in ".jar". At the bottom of the loop body, after
Class c = cl.loadClass(className);
continue with
if (Plugin.class.isAssignableFrom(c)) {
Plugin plugin = (Plugin) c.newInstance();
// And then, do something with the plugin here
}
I share #Mifeet's concerns about security - you might want to use a SecurityManager to limit what the plugin code is allowed to do.
Very old question, but still relevant if some one searches.. Adding to the accepted answer,
Use OSGI framework
Refer Apache Felix for a reference implementation
If you wanted to have a light version of OSGi, try to use apache connect - pojosr

How do I create an Android Test Module in IntelliJ 13 for a Gradle Android project?

I have successfully create a Android project in IntelliJ 13 and I want to setup the Android Testing Framework. I used the new project wizard to create the android project using Gradle. When I go to add a new module I only have options for "Gradle: Android Module" and "Gradle: Java Library", the "Test Module" option is missing.
How do I generate an Android Test Module? I have read http://www.jetbrains.com/idea/webhelp/testing-android-applications.html but I can never find any "test" option.
If an Android Test Module can not be automatically generated, then how do I configure and use the Android Testing Framework with a Gradle Android Project? Links to examples or documentation is very much appreciated.
Details: IntelliJ 13.1.3
So currently your directory structure should look something like this:
ProjectDirectory/
res/
src/
main/
java/
your.package.name
MyClass.java
All you need to do is add a src directory for androidTest:
ProjectDirectory/
res/
src/
main/
java/
your.package.name
MyClass.java
androidTest/
java/
your.package.name
MyClassTest.java
and then resync your Gradle file with your project, and it should be detected as a test directory. Then you can run the connectedCheck or connectedAndroidTest tasks (I'm unclear on the difference in the two) to run the tests.
If your directory structure differs from the above (if, for instance, you important an Eclipse-style project), you can specify the alternate src directory in your build.gradle script:
android {
sourceSets {
androidTest {
java.srcDirs = ['path/to/test/src']
}
}
}
As an alternative You could try switching to Android Studio, Its built on top of ItelliJ. If you are creating a new project Android Studio creates the test directory automatically for you.
After a lot of tries I've found the way that's definitively good:
Create a test folder under your package.
Create a class (call it ExampleTest would be good) extending InstrumentationTestCase into this folder.
Lets add a simple test which we know will fail:
public class ExampleTest extends InstrumentationTestCase {
public void test() throws Exception {
final int expected = 1;
final int reality = 5;
assertEquals(expected, reality);
}
}
Remeber that all test methods MUST start with the “test-” prefix or Android Studio will not detect them as tests and you will get all kinds of weird errors and nothing will work.
Now that we have a test case which is doomed for failure, we must now run it.
Start by clicking “Run -> Edit Configurations.”
Now select “+ -> Android Tests” from the upper left hand corner and select “Android Tests” and name your new test configuration “test” or something equally relevant.
Select your current module and next select the “All in Package” option and navigate to your “test” folder you just created. You can also select “All in Module” and Android Studio will automatically find any test inside your whole Module! You can also get more specific and select “Class” or even “Method” option to narrow the scope of your testing down further.
Apply, close, and run this configuration! The test will run..!
Information are taken by this link:
http://rexstjohn.com/unit-testing-with-android-studio/
It's written for android studio, but it work for intelliJ 13 too! ;)
have fun!
I've had trouble with creating android projects in Intellij, so now I create projects in Eclipse and then import them into Intellij. It's annoying, but it's an easy fix and it works.

Using Libraries in Vaadin: com.vaadin.server.ServiceException

Iam trying to use the matlabcontrol library in vaadin. I basically want use vaadin as a GUI for better configuration of the variables.
I have a test GUI running, everything works fine, until I try to add matlabcontrol specific variables or calls. I did add the library and the matlab interface works great when testing.
I will show you an abstract example:
public class UI_Matlab extends CustomComponent {
public UI_Matlab{
Label matlabRox = new Label("Matlab rocks!");
setCompositionRoot(matlabRox);
}
}
This works fine as expected!
But when I change it too:
public class UI_Matlab extends CustomComponent {
public UI_Matlab{
MatlabProxyFactory factory = new MatlabProxyFactory();
Label matlabRox = new Label("Matlab rocks!");
setCompositionRoot(matlabRox);
}
}
I already get:
"HTTP Status 500 - com.vaadin.server.ServiceException: java.lang.NoClassDefFoundError: matlabcontrol/MatlabProxyFactory"
Additional Information:
Vaadin 7.0
Tomcat v7.0
Eclipse Kepler
matlabcontrol 4.1.0 (edited)
The widget is trying to load additional classes it is depending on (MatlabProxyFactory) and cannot find them (NoClassDefFoundError is like a ClassNotFoundException, but one level "deeper", like a field or return type of the class you are loading cannot be found).
--> Check your build path or whether what you are deploying is complete wrt. dependencies.
I found the answere in another post:
External project dependency in Vaadin
They describe it a little different, so I will add what I did.
What "hiergiltdiestfu" probably meant worked out well, but I accidently was checking the project classpath.
The solution is to add the libraries to the server classpath, which means you have to add it to the tomcat classpath in my case.
Open the following in eclipse:
Run > Run Configurations > Apache Tomcat > (your Tomcat instance) > Classpath
Then add to user entries the library of your need.

Categories

Resources