Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

Possible to create global object? #3963

OnQuickBuild ·

Hello Robin

I wonder if there's a way to create global class object for one build.

I tried to use "pre-build" script in the advanced settings, and I declared a class with the following code

groovy: 
class Foo { 
  String name 
  Integer age 

  def setAge(Integer years) { 
    this.age = years 
  } 

  def increaseAge(Integer years) { 
    this.age += years 
  } 

  def printAge() { 
    println this.age 
  } 
}

So I was hoping that I could do def f = new Foo() in any step with groovy, but instead, I get this configuration error before the build can even run:

java.lang.RuntimeException: Failed to evaluate below expression in configuration 'root/my/path/test':
    groovy:
    class Foo {
     String name
     Integer age
     def setAge(Integer years) {
     this.age = years
     }
     def increaseAge(Integer years) {
     this.age += years
     }
     def printAge() {
     println this.age
     }
    }
        at com.pmease.quickbuild.util.ExceptionUtils.wrapException(ExceptionUtils.java:87)
        at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:321)
        at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:74)
        at com.pmease.quickbuild.model.Configuration.runPreBuildScript(Configuration.java:1737)
        at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
        at com.pmease.quickbuild.model.Configuration_$$_jvst27f_0.runPreBuildScript(Configuration_$$_jvst27f_0.java)
        at com.pmease.quickbuild.DefaultBuildEngine.process(DefaultBuildEngine.java:436)
        at com.pmease.quickbuild.DefaultBuildEngine.access$000(DefaultBuildEngine.java:142)
        at com.pmease.quickbuild.DefaultBuildEngine$2.run(DefaultBuildEngine.java:1230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: com.pmease.quickbuild.QuickbuildException: java.lang.ClassCastException: Foo cannot be cast to groovy.lang.Script
        at com.pmease.quickbuild.plugin.basis.BasisPlugin$32.evaluate(BasisPlugin.java:365)
        at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:305)
        at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:74)
        at com.pmease.quickbuild.model.Configuration.runPreBuildScript(Configuration.java:1737)
        at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
        at com.pmease.quickbuild.model.Configuration_$$_jvst27f_0.runPreBuildScript(Configuration_$$_jvst27f_0.java)
        at com.pmease.quickbuild.DefaultBuildEngine.process(DefaultBuildEngine.java:436)
        at com.pmease.quickbuild.DefaultBuildEngine.access$000(DefaultBuildEngine.java:142)
        at com.pmease.quickbuild.DefaultBuildEngine$2.run(DefaultBuildEngine.java:1230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
        at com.pmease.quickbuild.plugin.basis.BasisPlugin$32.evaluate(BasisPlugin.java:406)
        at com.pmease.quickbuild.DefaultScriptEngine.evaluate(DefaultScriptEngine.java:305)
        ... 13 more

Is there something wrong with the way I use "pre-build script"? Or is there a way to create a global class object at all?

Thank you!

  • replies 5
  • views 1538
  • stars 0
robinshen ADMIN ·

Defining a class in pre-build script and using it later is not possible, as QB runs each script with a separate class loader to avoid caching groovy objects to cause OOM.

OnQuickBuild ·

Hi Robin

Thanks for your reply, but I have a new question following this:

I'm just trying to find a way to store parameters/values needed for the build, without having to create a QB variable for each one.

So I have a JSON file called config.json, and I try to access it whenever I need a specific parameter/value. This is working, except that I'm getting "file not found" error, in steps that are disabled.

I'm using repeat parameters to access the json file and return the value I need, so I can directly use it in the step itself. I understand that repeat parameters are established when the parent step is executed, but the step itself is disabled, why would the parent step still go establish the repeat parameters of a disabled child step?

Thanks

robinshen ADMIN ·

Can you please reproduce this issue with a sample database and send it to [robin AT pmease DOT com] for diagnostics?

OnQuickBuild ·

Hi Robin

Thank you for your concern.

But I don't think I can recreate that scenario now. I solved my issue using a few necessary (or unnecessary?) "sequential steps" as container, and made sure the file exists before the child steps try to open it in the repeat parameter scripts.

I understand the rule, where the parent step needs to know how many child steps are there. But all I can tell you is when I was testing the configuration, I used to have the steps like this
Master
-- sequential
-- -- checkout
-- -- child steps that try to open a file from checkout

So when the sequential step executes, it will fail due to the child steps try to open a file that's not there yet.
But I disabled all the child steps but the checkout step, the sequential step still failed, the checkout step didn't get executed even. That's why I asked.

Hope it's still useful information to you.
Again thanks!

robinshen ADMIN ·

The disabled steps will still be expanded if they rely on repeat parameters. The disable flag only disables the execution.