Change classpath order in JavaExec

I have a project that depends on many sub-projects. Some of these project has classes with the same name like:FooFactory.

I have two JavaExec tasks. The each need a different classpath.
So task1 needs ProjectA to come before projectB in its classpath.
And task2 needs ProjectB to come before projectA in its classpath.

How can I modify the classpath to contains sub-projects in the rigth order?