Sorry for the late reply, I've been short on free time lately.  How about moving the scripts somewhere else, then create unit tests to compile your script classes (using the parseClass like you do in production), and better yet, also instantiate the class and invoke methods in the scripts that can be tested without side-effects?

-Keegan

On Sun, Oct 4, 2015 at 3:29 AM, Maarten Boekhold <boekhold@gmx.com> wrote:
Hi Keegan,

Let me describe how everything is normally run in "deployed mode", and then what my development environment in maven/IntelliJ looks like.

Deployed mode:
  • Java code/jars are installed in ${dir}/lib
  • Groovy code installed in ${dir}/scripts
    • This is a mix of interface declarations, 'real classes' and scripts, uncompiled
  • Start/stop scripts in ${dir}/etc
  • CLASSPATH only has the JAR files from under ${lib} in it!
  • When running the program, a configuration file tells the application to load/parseClass() all groovy files under ${dir}/scripts using a GroovyClassLoader instance. There is some logic there to load/parse things in order so as to handle dependency issues between classes/scripts. The resulting classes are cached in a Map. Based on certain events (data mapping requests) a class instance is retrieved from the Map, instantiated and run.
    • In fact, the application uses the JSR223 API to load things, and under the hood, that uses GroovyClassLoader.parseClass()

Maven/IntelliJ:
  • Java code under src/main/java
  • Groovy code under src/main/groovy
  • From the "Maven Projects" tool window, I use "mvn clean compile" to compile all code. This *also* compiles all groovy files and generates class files under target/classes!
  • I have a "Run Configuration" that uses the "classpath of module".
  • The configuration file tells the main application "load all groovy files under src/main/groovy"
  • The problem here is that when building the project, 'mvn clean compile' also compiles all groovy scripts to target/classes.
  • When I now run my run configuration, the groovy classes/interfaces are already on the CLASSPATH, so there's a conflict between running the scripts as loaded by GroovyClassLoader.parseClass() and the ones that are already on the classpath. I'm getting lots of GroovyClassCastExceptions because it's trying to cast to classes that are loaded by a different classloader (eg from the classpath). This is the thing I need to avoid/fix.
    • I also need to write unit tests for the groovy code, and for that I do need all groovy code on the test-classpath, just to complicate things a bit more. Btw for those unit tests there's no loading of classes via GroovyClassLoader/jsr223 involved, so that makes things a bit simpler again.

I could of course just remove GMavenPlus from my pom.xml, but then my groovy code isn't compiled at all, and I do want that "safety net" to capture errors in that part of the (groovy) code.

Still a bit of a complicated description I guess, but I hope it's a little bit more clear what I'm trying to achieve now.

Maarten

On 2015-10-04 07:16, Keegan Witt wrote:
How about adding them in a non-standard directory (to avoid IntelliJ knowing where they are) then add them as test sources to GMavenPlus?  If that'd work, please see here for an example.  It might not though, because I'm not sure I'm fully understanding your use case.  It wasn't clear to me how you were deploying the scripts so you could do the GroovyClassLoader.parseClass() calls in production.

-Keegan

On Thu, Oct 1, 2015 at 3:29 AM, Maarten Boekhold <boekhold@gmx.com> wrote:
Hi all,

Not strictly a groovy-specific question I believe, but I also believe that if anybody knows the answer to this, they'll be on this list.

I am working on a project that is mixed Java and groovy code. I'm developing in IntelliJ, it's a maven project that is using GMavenPlus (1.5).

When the resulting artifact is run after I have installed it, the groovy scripts should not be on the CLASSPATH of the application; instead they are loaded dynamically (GroovyClassLoader.parseClass()). So, also inside IntelliJ, when running or debugging the full application, I want to exclude the groovy sources (or compiled artifacts) from the CLASSPATH. However, while developing I do want these scripts to be compiled so I can see any errors. Also when running unit tests the scripts will need to be on the classpath.

I'm looking for a way to achieve the following:
- Groovy sources are compiled when building the project
- Groovy artifacts are NOT on the CLASSPATH when running/debugging the application inside IntelliJ
- Groovy artifacts ARE on the CLASSPATH when running unit tests

Does anybody have any idea how to do this?

Maarten