I want to create a new instance of a class which is in src folder in my shared library. Of cource I can do a simple def object = new myClass() with an import on the top but I want to do it dynamically initiate classes (trying to use Class.forName failed for me and I'm not going to use that solution).
I'm trying to do this from a groovy file which is under vars folder and not src.
So what I do is : def customized = library("mySharedLib").com.x.x.MyClass.new(this)
As it is specified in the documentation for Shared libraries : Step library
But I'm getting the error :
java.lang.IllegalAccessException: com.x.x.MyClass was defined in file:///Path/to/master/workspace/jobs/project/builds/297/libs/mySharedLib/vars/generic.groovy which was not inside file:///Path/to/master/workspace/jobs/project/branches/PR-50/builds/297/libs/mySharedLib/src/
In the Jenkins Jira Here, there is the same issue... any ideas ?? I can't understand wht is going on! I tried making a method in a class under src folder that does the step library call but it returns same error.
No need to load the library from within the vars folder (I assume it’s in the same repository like the src folder). Simply import the class with a plain import and use it like in plain groovy, e.g.
import org.pack.Myclass
def call() {
def myClass = new MyClass()
}
Related
I would like to persist the state of some logic between calls in my Jenkins shared library. I don't want to pass in an object/map/... with the state in the arguments but use some kind of global variable/map for this. I failed with global varibales in the foobar.groovy below or use of environment variables. I found a work-around by creating a small helper class with a static map to store my data but this does not seem to be the right solution. Is there a cleaner way?
src/pkg/GlobalConfig.groovy
package pkg;
class GlobalConfig {
// define a static map
static Map data = new HashMap()
}
use map in shared library e.g. vars/foobar.groovy
import pkg.GlobalConfig
def do_something() {
def data = pkg.GlobalConfig.data
// do something with data
}
I am making a flutter application and I created a file/class for each custom widget I am using. Then, I imported all of these files into the main screen but I don't like how it looks. Especially because if I want to add another widget or delete the one I would need to fiddle with the imports.
Is there something like C# namespaces where I can just make one import for all files in folder/namespace?
I already tried using library/part with success but then in https://www.dartlang.org/guides/libraries/create-library-packages says that I should avoid using part/part of. So, are we expected to import each and every file?
Instead of having:
import 'package:custom_widgets/custom_multiplechoice.dart';
import 'package:custom_widgets/custom_singlechoice.dart';
import 'package:custom_widgets/custom_time.dart';
import 'package:custom_widgets/custom_yesnochoice.dart';
import 'package:custom_widgets/custom_date.dart';
I would like to have:
import 'package:custom_widgets';
Yes there is, you can use export to achieve what you want.
You can place all your widgets in a folder, for example libs/src/ and then create file custom_widgets.dart in libs/ and use export like this inside custom_widgets.dart:
export 'src/custom_multiplechoice.dart';
export 'src/custom_singlechoice.dart';
export 'src/custom_time.dart';
export 'src/custom_widgets/src/custom_yesnochoice.dart';
export 'src/custom_date.dart';
Once you import custom_widgets.dart, all those widgets will be available to you.
Check this out, its all explained here: Dart: Organizing a library package
Update:
In Dart there are no namespaces like in most other languages.
Dart uses libraries for encapsulation, data hiding.
Only way to import a class into your code is to use import in the beginning of your file, which should also be a library.
I have an issue with this too. Imagine a situation where you want to import library dynamically. Let's say you would like to implement MVC pattern in your application, if you are doing this on the server, you would have a Router class that would analyze URL and decide what Controller class to instantiate and what Method from that Controller to invoke. Now every URL would trigger different Controller, and you don't know it in advance, its up to your Router to detect a Class to instantiate. What you need to do in this situation is import every Controller that could get instantiated at the beginning of your file. And I have issues with that. What if your application gets big and you have to import lets say 20 Controller classes, just so Router / Dispatcher can invoke one of them, and in reality you will invoke only one Controller, as there is only Controller per URL.
Don't have issues with manual loading of Libraries, if they are going to be used, but for situation like described above, Dart fails as there is no "auto loading" of classes like in for example PHP, where you can use auto loaders that use namespaces to find out about location of your class and instantiate a class in the middle of the code dynamically.
There is this extension for VSCode which can help to create this.
Dart Barrel File Generator
Upgrading a legacy system of grails. One of the controllers implements Serializable. This is throwing the following error in newer versions of grails:
Invalid duplicate class definition of class com.regional.ScheduleController :
The source contains at least two definitions of the class.
One of the classes is an explicit generated class using the class statement,
the other is a class generated from the script body based on the file name.
Solutions are to change the file name or to change the class name.
The solution mentioned would break (previous) grails convention. Anyone know how to handle this in grails 2.5+?
EDIT
Serializable is not the issue. I tried removing it and got the same error.
I found this explanation from another question:
IN groovy.. class B{} is a class structure and defines a class B.
Scripts are classes too.
Now you may create a B.groovy, with the content "class B{}; def b = new B()".
There would be one class named B, and a script with the very same name.
This is a conflict.
However this does not explain why it runs fine below grails 2.5 and not above it. And I can't find a def conflict like the one mentioned above in that controller.
ANSWER:
One of the imports was what was actually failing- in a way that caused groovy to generate a class definition based on the current file name. When it hit the class definition, there was already an auto generated class name to collide with.
I need use a domain class define in one inplacePlugin to a controller class in another inplacePlugin inside my app, but when I try to define the class IDE "cannot resolve the symbol". AutorDef domain class I define public in my app. What i need to do for reselve this issue?
def autor = AutorDef.findAll()
I already know how can do it that, it is very simple, (of course after a few day of deep thinking and search). Here is the solution.
In BuildConfig.groovy inside the inplace plugin where we need import the domain class from other inplace plugin we add next code,
grails.plugin.location.'common'="../sdl-common"
We add this code directly below
grails.project.test.reports.dir = "target/test-reports"
The import code before equals
grails.plugin.location.'common'
is the name use for import the plugin data(domain class, etc) and
"../sdl-common"
is the route to inplace plugin, and when we need to import the data in our classes, or controllers, we only need add
import common.*
in the part where we import hte packages.
According to Grails API, we can retrieve all domain classes definition through GrailsApplication as such
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
AH.application.domainClasses
I have classes defined under src/groovy and would like to do the same. I tried using the follwing code but in vain
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
AH.application.allClasses
According to Grails API, call to getAllClasses should return all classes loaded by Grails class loader
Class[] getAllClasses()
Retrieves all java.lang.Class instances loaded by the Grails class loader
Am i missing something
Any Java or Groovy code can be placed in the src directory and does not have to integrate with Grails. The Grails class loader only loads Grails specific classes such as pluggins, controllers, and domain classes that your grails application knows about at run-time. In order to find out about classes located in your src directory you will need to know the package name your interested in and then iterate through everything contained in that package.
.
Well the classes under 'src' are not registered with grails classLoader. But you can use the "in context class loader" to get those classes easily using one single line of code (Java Style though)
Class fetchedClass = Class.forName(
"Class Full Name Here",
true,
Thread.currentThread().getContextClassLoader()
)
.
.
Thats It .. :) All Done!! Easily.
.
.
Example to get the class "com.myPackage.dto.AddressDTO"
We will Do:
EXAMPLE: get class "com.myPackage.dto.AddressDTO"
Class fetchedClass = Class.forName(
"com.myPackage.dto.AddressDTO",
true,
Thread.currentThread().getContextClassLoader()
)