public void execute(PersistentCache cache) {
File jarFile = jarFile(cache);
LOGGER.debug("Generating worker process classes to {}.", jarFile);
URL currentClasspath = getClass().getProtectionDomain().getCodeSource().getLocation();
JarJarTask task = new JarJarTask();
task.setDestFile(jarFile);
final List<Resource> classResources = new ArrayList<Resource>();
List<Class<?>> renamedClasses = Arrays.asList(GradleWorkerMain.class,
BootstrapSecurityManager.class,
EncodedStream.EncodedInput.class);
List<Class<?>> classes = new ArrayList<Class<?>>();
classes.add(BootstrapClassLoaderWorker.class);
classes.addAll(renamedClasses);
for (Class<?> aClass : classes) {
final String fileName = aClass.getName().replace('.', '/') + ".class";
// Prefer the class from the same classpath as the current class. This is for the case where we're running in a test under an older
// version of Gradle, whose worker classes will be visible to us.
// TODO - remove this once we have upgraded to a wrapper with these changes in it
Enumeration<URL> resources;
try {
resources = WorkerProcessClassPathProvider.class.getClassLoader().getResources(fileName);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
URL resource = null;
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
resource = url;
if (url.toString().startsWith(currentClasspath.toString())) {
break;
}
}
URLResource urlResource = new URLResource(resource) {
@Override
public synchronized String getName() {
return fileName;
}
};
classResources.add(urlResource);
}
task.add(new ResourceCollection() {
public Iterator iterator() {
return classResources.iterator();
}
public int size() {
return classResources.size();
}
public boolean isFilesystemOnly() {
return true;
}
});
for (Class<?> renamedClass : renamedClasses) {
Rule rule = new Rule();
rule.setPattern(renamedClass.getName());
rule.setResult("jarjar.@0");
task.addConfiguredRule(rule);
}
AntUtil.execute(task);
}