How do you retrieve the classpath of a plugin?


(Jelmer Kuperus) #1

How do you retrieve the classpath of a plugin ? I want to start a java forked ant task from a groovy plugin and need to provide it the classpath of my plugin

My instinct was to look getProject().getBuildscript().getDependencies() but I can’t seem to figure out how to use this untyped undocumented api


(Jelmer Kuperus) #2

Hmm i played around a bit more and this does more or less what i want :

Set pluginClasspath = getProject().getBuildscript().getConfigurations()

.getByName(ScriptHandler.CLASSPATH_CONFIGURATION).resolve();

Is this the correct approach ?

It does for me raise the question of how plugins are isolated from one another since my understanding is that this will give me the classpath of all the plugins in the build script not just the dependencies of this one plugin. What happens if 2 plugins depend on 2 different non backwards compatible versions of a library ?


(Peter Niederwieser) #3

Can you explain in more detail what you are trying to achieve? Which Ant task is your plugin using, and why? And why does it need the plugin’s class path (rather than using some fixed class path)?


(Jelmer Kuperus) #4

I am attempting to transform a series of ant files into a gradle plugin. One of the steps in the current build process is running a sass to css tool via ant’s “java” task. I figured it, at least for now, would be easiest to invoke the ant task from my grails plugin.

Right now i am doing this :

public class SassToCss extends ConventionTask {

private File sassDir;

@TaskAction

public void sassToCss() {

if (getSassDir() == null || !getSassDir().exists()) {

throw new InvalidUserDataException(“Please specify a valid sassDir”);

}

Java javaTask = new Java();

javaTask.setTaskName(“sass to css builder”);

javaTask.setClassname(“com.liferay.portal.tools.SassToCssBuilder”);

Set pluginClasspath = getProject().getBuildscript().getConfigurations()

.getByName(ScriptHandler.CLASSPATH_CONFIGURATION).resolve();

Project antProject = getAnt().getAntProject();

Path classPath = new Path(antProject);

for (File dep : pluginClasspath) {

classPath.createPathElement()

.setLocation(dep);

}

javaTask.setProject(antProject);

javaTask.setClasspath(classPath);

javaTask.setFork(true);

javaTask.setNewenvironment(true);

javaTask.createArg()

.setLine(“sass.dir=” + getSassDir());

javaTask.execute();

}

… }


(Peter Niederwieser) #5

If you choose to run ‘SassToCssBuilder’ in an external process, you shouldn’t need to modify or pass on the build script class path. Just create a regular configuration (or, depending on the circumstances, file collection), put ‘SassToCssBuilder’ and its dependencies on it, and pass it on to ‘javaTask’.