Created
May 7, 2013 16:23
-
-
Save anonymous/5533941 to your computer and use it in GitHub Desktop.
Gradle issue files: Custom gradle task extending the 'War' task does not see some required classes http://stackoverflow.com/questions/16408102/custom-gradle-task-extending-the-war-task-does-not-see-some-required-classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just a standard War task | |
task okWar(type: War) { | |
from("buildSrc") | |
archiveName = "sample-ok.war" | |
} | |
// Custom subclass of War task that cannot find ZipOutputStream class in classpath | |
// Uncomment *after* you try others because nothing will work while this is here. | |
/* | |
task badWar(type: sample.ProblemWarTask) { | |
from("buildSrc") | |
archiveName = "sample-bad.war" | |
} | |
*/ | |
// This is using a DummyWarTask which is the same ProblemWarTask with all references | |
// to missing classes removed (and, thus, won't actually create any wars, here just | |
// to illustrate that the classpath issue is restricted to those classes | |
task dummyWar(type: sample.DummyWarTask) { | |
from("buildSrc") | |
archiveName = "sample-dummy.war" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.gradle.api.GradleException; | |
import org.gradle.api.UncheckedIOException; | |
import org.gradle.api.tasks.bundling.War; | |
import org.gradle.api.tasks.bundling.Zip; | |
import org.gradle.api.internal.file.FileResolver; | |
import org.gradle.api.internal.file.archive.ZipCopyAction; | |
import org.gradle.api.internal.file.archive.ZipCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopyAction; | |
import org.gradle.api.internal.file.copy.CopyActionImpl; | |
import org.gradle.api.internal.file.copy.CopySpecImpl; | |
import org.gradle.api.internal.file.copy.ZipDeflatedCompressor; | |
import org.gradle.api.internal.file.copy.ZipCompressor; | |
import org.gradle.api.file.FileVisitDetails; | |
import org.gradle.api.internal.file.copy.MappingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.NormalizingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopySpecVisitor; | |
import org.gradle.api.internal.file.copy.ReadableCopySpec; | |
import org.gradle.internal.nativeplatform.filesystem.FileSystems; | |
import java.io.File; | |
import java.io.IOException; | |
public class DummyWarCopySpecVisitor extends ZipCopySpecVisitor { | |
public void startVisit(CopyAction action) { | |
// Do nothing - this a dummy | |
} | |
public void endVisit() { | |
// Do nothing - this a dummy | |
} | |
public void visitFile(FileVisitDetails fileDetails) { | |
// Do nothing - this a dummy | |
} | |
public void visitDir(FileVisitDetails dirDetails) { | |
// Do nothing - this a dummy | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.gradle.api.GradleException; | |
import org.gradle.api.UncheckedIOException; | |
import org.gradle.api.tasks.bundling.War; | |
import org.gradle.api.tasks.bundling.Zip; | |
import org.gradle.api.internal.file.FileResolver; | |
import org.gradle.api.internal.file.archive.ZipCopyAction; | |
import org.gradle.api.internal.file.archive.ZipCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopyAction; | |
import org.gradle.api.internal.file.copy.CopyActionImpl; | |
import org.gradle.api.internal.file.copy.CopySpecImpl; | |
import org.gradle.api.internal.file.copy.ZipDeflatedCompressor; | |
import org.gradle.api.internal.file.copy.ZipCompressor; | |
import org.gradle.api.file.FileVisitDetails; | |
import org.gradle.api.internal.file.copy.MappingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.NormalizingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopySpecVisitor; | |
import org.gradle.api.internal.file.copy.ReadableCopySpec; | |
import org.gradle.internal.nativeplatform.filesystem.FileSystems; | |
import java.io.File; | |
import java.io.IOException; | |
class DummyWarTask extends War { | |
private ZipCopyActionImpl copyAction = null; | |
public DummyWarTask() {} | |
protected synchronized Zip.ZipCopyActionImpl getCopyAction() { | |
if (copyAction == null) { | |
copyAction = new DummyWarCopyActionImpl(getServices().get(FileResolver.class)); | |
} | |
return copyAction; | |
} | |
protected class DummyWarCopyActionImpl extends Zip.ZipCopyActionImpl { | |
private final CopySpecVisitor visitor; | |
public DummyWarCopyActionImpl(FileResolver fileResolver) { | |
super((FileResolver) fileResolver); | |
visitor = new MappingCopySpecVisitor(new NormalizingCopySpecVisitor(new DummyWarCopySpecVisitor()), FileSystems.getDefault()); | |
} | |
public void execute() { | |
visitor.startVisit(this); | |
for (ReadableCopySpec spec : getRootSpec().getAllSpecs()) { | |
visitor.visitSpec(spec); | |
spec.getSource().visit(visitor); | |
} | |
visitor.endVisit(); | |
} | |
public boolean getDidWork() { | |
return visitor.getDidWork(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.apache.tools.zip.ZipEntry; | |
import org.apache.tools.zip.ZipOutputStream; | |
import org.apache.tools.zip.UnixStat; | |
import org.gradle.api.GradleException; | |
import org.gradle.api.UncheckedIOException; | |
import org.gradle.api.tasks.bundling.War; | |
import org.gradle.api.tasks.bundling.Zip; | |
import org.gradle.api.internal.file.FileResolver; | |
import org.gradle.api.internal.file.archive.ZipCopyAction; | |
import org.gradle.api.internal.file.archive.ZipCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopyAction; | |
import org.gradle.api.internal.file.copy.CopyActionImpl; | |
import org.gradle.api.internal.file.copy.CopySpecImpl; | |
import org.gradle.api.internal.file.copy.ZipDeflatedCompressor; | |
import org.gradle.api.internal.file.copy.ZipCompressor; | |
import org.gradle.api.file.FileVisitDetails; | |
import org.gradle.api.internal.file.copy.MappingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.NormalizingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopySpecVisitor; | |
import org.gradle.api.internal.file.copy.ReadableCopySpec; | |
import org.gradle.internal.nativeplatform.filesystem.FileSystems; | |
import java.io.File; | |
import java.io.IOException; | |
public class ProblemWarCopySpecVisitor extends ZipCopySpecVisitor { | |
private ZipOutputStream zipOutStr; | |
private File zipFile; | |
public void startVisit(CopyAction action) { | |
// Copy of code from ZipCopySpecVisitor.startVisit() | |
ZipCopyAction archiveAction = (ZipCopyAction) action; | |
zipFile = archiveAction.getArchivePath(); | |
try { | |
zipOutStr = archiveAction.getCompressor().createArchiveOutputStream(zipFile); | |
} catch (Exception e) { | |
throw new GradleException(String.format("Could not create ZIP '%s'.", zipFile), e); | |
} | |
} | |
public void endVisit() { | |
// Copy of code from ZipCopySpecVisitor.endVisit() | |
try { | |
zipOutStr.close(); | |
} catch (IOException e) { | |
throw new UncheckedIOException(e); | |
} finally { | |
zipOutStr = null; | |
} | |
} | |
public void visitFile(FileVisitDetails fileDetails) { | |
// Copy of code from ZipCopySpecVisitor.visitFile() | |
try { | |
ZipEntry archiveEntry = new ZipEntry(fileDetails.getRelativePath().getPathString()); | |
archiveEntry.setTime(fileDetails.getLastModified()); | |
archiveEntry.setUnixMode(UnixStat.FILE_FLAG | fileDetails.getMode()); | |
zipOutStr.putNextEntry(archiveEntry); | |
fileDetails.copyTo(zipOutStr); | |
zipOutStr.closeEntry(); | |
} catch (Exception e) { | |
throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e); | |
} | |
// Other stuff from here not needed for the example | |
} | |
public void visitDir(FileVisitDetails dirDetails) { | |
// Copy of code from ZipCopySpecVisitor.visitDir() | |
try { | |
// Trailing slash in name indicates that entry is a directory | |
ZipEntry archiveEntry = new ZipEntry(dirDetails.getRelativePath().getPathString() + '/'); | |
archiveEntry.setTime(dirDetails.getLastModified()); | |
archiveEntry.setUnixMode(UnixStat.DIR_FLAG | dirDetails.getMode()); | |
zipOutStr.putNextEntry(archiveEntry); | |
zipOutStr.closeEntry(); | |
} catch (Exception e) { | |
throw new GradleException(String.format("Could not add %s to ZIP '%s'.", dirDetails, zipFile), e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.apache.tools.zip.ZipEntry; | |
import org.apache.tools.zip.ZipOutputStream; | |
import org.apache.tools.zip.UnixStat; | |
import org.gradle.api.GradleException; | |
import org.gradle.api.UncheckedIOException; | |
import org.gradle.api.tasks.bundling.War; | |
import org.gradle.api.tasks.bundling.Zip; | |
import org.gradle.api.internal.file.FileResolver; | |
import org.gradle.api.internal.file.archive.ZipCopyAction; | |
import org.gradle.api.internal.file.archive.ZipCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopyAction; | |
import org.gradle.api.internal.file.copy.CopyActionImpl; | |
import org.gradle.api.internal.file.copy.CopySpecImpl; | |
import org.gradle.api.internal.file.copy.ZipDeflatedCompressor; | |
import org.gradle.api.internal.file.copy.ZipCompressor; | |
import org.gradle.api.file.FileVisitDetails; | |
import org.gradle.api.internal.file.copy.MappingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.NormalizingCopySpecVisitor; | |
import org.gradle.api.internal.file.copy.CopySpecVisitor; | |
import org.gradle.api.internal.file.copy.ReadableCopySpec; | |
import org.gradle.internal.nativeplatform.filesystem.FileSystems; | |
import java.io.File; | |
import java.io.IOException; | |
import java.lang.reflect.Constructor; | |
class ProblemWarTask extends War { | |
private ZipCopyActionImpl copyAction = null; | |
public ProblemWarTask() {} | |
protected synchronized Zip.ZipCopyActionImpl getCopyAction() { | |
if (copyAction == null) { | |
copyAction = new ProblemWarCopyActionImpl(getServices().get(FileResolver.class)); | |
} | |
return copyAction; | |
} | |
protected class ProblemWarCopyActionImpl extends Zip.ZipCopyActionImpl { | |
private final CopySpecVisitor visitor; | |
public ProblemWarCopyActionImpl(FileResolver fileResolver) { | |
super((FileResolver) fileResolver); | |
visitor = new MappingCopySpecVisitor(new NormalizingCopySpecVisitor(new ProblemWarCopySpecVisitor()), FileSystems.getDefault()); | |
} | |
public void execute() { | |
// Copy of code from CopyActionImpl.execute() | |
visitor.startVisit(this); | |
for (ReadableCopySpec spec : getRootSpec().getAllSpecs()) { | |
visitor.visitSpec(spec); | |
spec.getSource().visit(visitor); | |
} | |
visitor.endVisit(); | |
} | |
public boolean getDidWork() { | |
return visitor.getDidWork(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment