|
36 | 36 | import edu.umd.cs.findbugs.annotations.CheckForNull; |
37 | 37 | import edu.umd.cs.findbugs.annotations.NonNull; |
38 | 38 | import hudson.slaves.WorkspaceList; |
| 39 | +import hudson.util.DirScanner; |
| 40 | +import hudson.util.FileVisitor; |
39 | 41 | import hudson.util.io.ArchiverFactory; |
| 42 | +import java.io.File; |
40 | 43 | import java.io.IOException; |
41 | 44 | import java.io.OutputStream; |
42 | 45 | import java.nio.file.Path; |
@@ -84,35 +87,28 @@ public void retrieveJar(@NonNull String name, @NonNull String version, boolean c |
84 | 87 | * into a JAR file with Groovy in classpath orientation and {@code resources/} as a ZIP folder. |
85 | 88 | */ |
86 | 89 | static void dir2Jar(@NonNull String name, @NonNull FilePath dir, @NonNull FilePath jar) throws IOException, InterruptedException { |
87 | | - // TODO do this more efficiently by packing JAR directly |
88 | | - FilePath tmp = jar.sibling(jar.getBaseName() + "-repack"); |
89 | | - tmp.mkdirs(); |
| 90 | + lookForBadSymlinks(dir, dir); |
| 91 | + FilePath mf = jar.withSuffix(".mf"); |
90 | 92 | try { |
91 | | - FilePath src = dir.child("src"); |
92 | | - if (src.isDirectory()) { |
93 | | - src.moveAllChildrenTo(tmp); |
94 | | - } |
95 | | - FilePath vars = dir.child("vars"); |
96 | | - if (vars.isDirectory()) { |
97 | | - vars.moveAllChildrenTo(tmp); |
98 | | - } |
99 | | - FilePath resources = dir.child("resources"); |
100 | | - if (resources.isDirectory()) { |
101 | | - resources.renameTo(tmp.child("resources")); |
102 | | - } |
103 | | - lookForBadSymlinks(tmp, tmp); |
104 | | - try (OutputStream os = tmp.child(JarFile.MANIFEST_NAME).write()) { |
| 93 | + try (OutputStream os = mf.write()) { |
105 | 94 | Manifest m = new Manifest(); |
106 | 95 | m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
107 | 96 | // Informational debugging aid, since the hex JAR basename will be meaningless: |
108 | 97 | m.getMainAttributes().putValue(ATTR_LIBRARY_NAME, name); |
109 | 98 | m.write(os); |
110 | 99 | } |
111 | 100 | try (OutputStream os = jar.write()) { |
112 | | - tmp.archive(ArchiverFactory.ZIP, os, "**"); |
| 101 | + dir.archive(ArchiverFactory.ZIP, os, new DirScanner() { |
| 102 | + @Override public void scan(File dir, FileVisitor visitor) throws IOException { |
| 103 | + scanSingle(new File(mf.getRemote()), JarFile.MANIFEST_NAME, visitor); |
| 104 | + new DirScanner.Glob("**/*.groovy", null).scan(new File(dir, "src"), visitor); |
| 105 | + new DirScanner.Glob("*.groovy,*.txt", null).scan(new File(dir, "vars"), visitor); |
| 106 | + new DirScanner.Glob("resources/", null).scan(dir, visitor); |
| 107 | + } |
| 108 | + }); |
113 | 109 | } |
114 | 110 | } finally { |
115 | | - tmp.deleteRecursive(); |
| 111 | + mf.delete(); |
116 | 112 | } |
117 | 113 | } |
118 | 114 |
|
|
0 commit comments