Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.maven; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.jar.*; |
| 6 | |
| 7 | import aQute.bnd.build.*; |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.command.*; |
| 10 | import aQute.libg.header.*; |
| 11 | import aQute.libg.reporter.*; |
| 12 | |
| 13 | public class MavenDeployCmd extends Processor { |
| 14 | |
| 15 | String repository = "nexus"; |
| 16 | String url = "http://oss.sonatype.org/service/local/staging/deploy/maven2"; |
| 17 | String homedir; |
| 18 | String keyname; |
| 19 | |
| 20 | String passphrase; |
| 21 | Reporter reporter; |
| 22 | |
| 23 | /** |
| 24 | * maven deploy [-url repo] [-passphrase passphrase] [-homedir homedir] |
| 25 | * [-keyname keyname] bundle ... |
| 26 | * |
| 27 | * @param args |
| 28 | * @param i |
| 29 | * @throws Exception |
| 30 | */ |
| 31 | void run(String args[], int i) throws Exception { |
| 32 | if (i >= args.length) { |
| 33 | System.err |
| 34 | .println("Usage:%n"); |
| 35 | System.err.println(" deploy [-url repo] [-passphrase passphrase] [-homedir homedir] [-keyname keyname] bundle ..."); |
| 36 | System.err.println(" settings"); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | /* skip first argument */ |
| 41 | i++; |
| 42 | |
| 43 | while (i < args.length && args[i].startsWith("-")) { |
| 44 | String option = args[i]; |
| 45 | if (option.equals("-url")) |
| 46 | repository = args[++i]; |
| 47 | else if (option.equals("-passphrase")) |
| 48 | passphrase = args[++i]; |
| 49 | else if (option.equals("-url")) |
| 50 | homedir = args[++i]; |
| 51 | else if (option.equals("-keyname")) |
| 52 | keyname = args[++i]; |
| 53 | else |
| 54 | error("Invalid command "); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | } |
| 59 | |
| 60 | public void setProperties(Map<String, String> map) { |
| 61 | repository = map.get("repository"); |
| 62 | url = map.get("url"); |
| 63 | passphrase = map.get("passphrase"); |
| 64 | homedir = map.get("homedir"); |
| 65 | keyname = map.get("keyname"); |
| 66 | |
| 67 | if (url == null) |
| 68 | throw new IllegalArgumentException("MavenDeploy plugin must get a repository URL"); |
| 69 | if (repository == null) |
| 70 | throw new IllegalArgumentException("MavenDeploy plugin must get a repository name"); |
| 71 | } |
| 72 | |
| 73 | public void setReporter(Reporter processor) { |
| 74 | this.reporter = processor; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | */ |
| 79 | public boolean deploy(Project project, Jar original) throws Exception { |
| 80 | Parameters deploy = project.parseHeader(project |
| 81 | .getProperty(Constants.DEPLOY)); |
| 82 | |
| 83 | Map<String, String> maven = deploy.get(repository); |
| 84 | if (maven == null) |
| 85 | return false; // we're not playing for this bundle |
| 86 | |
| 87 | project.progress("deploying %s to Maven repo: %s", original, repository); |
| 88 | File target = project.getTarget(); |
| 89 | File tmp = Processor.getFile(target, repository); |
| 90 | tmp.mkdirs(); |
| 91 | |
| 92 | Manifest manifest = original.getManifest(); |
| 93 | if (manifest == null) |
| 94 | project.error("Jar has no manifest: %s", original); |
| 95 | else { |
| 96 | project.progress("Writing pom.xml"); |
| 97 | PomResource pom = new PomResource(manifest); |
| 98 | pom.setProperties(maven); |
| 99 | File pomFile = write(tmp, pom, "pom.xml"); |
| 100 | |
| 101 | Jar main = new Jar("main"); |
| 102 | Jar src = new Jar("src"); |
| 103 | try { |
| 104 | split(original, main, src); |
| 105 | Parameters exports = project.parseHeader(manifest |
| 106 | .getMainAttributes().getValue(Constants.EXPORT_PACKAGE)); |
| 107 | File jdoc = new File(tmp, "jdoc"); |
| 108 | jdoc.mkdirs(); |
| 109 | project.progress("Generating Javadoc for: " + exports.keySet()); |
| 110 | Jar javadoc = javadoc(jdoc, project, exports.keySet()); |
| 111 | project.progress("Writing javadoc jar"); |
| 112 | File javadocFile = write(tmp, new JarResource(javadoc), "javadoc.jar"); |
| 113 | project.progress("Writing main file"); |
| 114 | File mainFile = write(tmp, new JarResource(main), "main.jar"); |
| 115 | project.progress("Writing sources file"); |
| 116 | File srcFile = write(tmp, new JarResource(main), "src.jar"); |
| 117 | |
| 118 | project.progress("Deploying main file"); |
| 119 | maven_gpg_sign_and_deploy(project, mainFile, null, pomFile); |
| 120 | project.progress("Deploying main sources file"); |
| 121 | maven_gpg_sign_and_deploy(project, srcFile, "sources", null); |
| 122 | project.progress("Deploying main javadoc file"); |
| 123 | maven_gpg_sign_and_deploy(project, javadocFile, "javadoc", null); |
| 124 | |
| 125 | } finally { |
| 126 | main.close(); |
| 127 | src.close(); |
| 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | private void split(Jar original, Jar main, Jar src) { |
| 134 | for (Map.Entry<String, Resource> e : original.getResources().entrySet()) { |
| 135 | String path = e.getKey(); |
| 136 | if (path.startsWith("OSGI-OPT/src/")) { |
| 137 | src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue()); |
| 138 | } else { |
| 139 | main.putResource(path, e.getValue()); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // gpg:sign-and-deploy-file \ |
| 145 | // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2 |
| 146 | // \ |
| 147 | // -DrepositoryId=sonatype-nexus-staging \ |
| 148 | // -DupdateReleaseInfo=true \ |
| 149 | // -DpomFile=pom.xml \ |
| 150 | // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \ |
| 151 | // -Dpassphrase=a1k3v3t5x3 |
| 152 | |
| 153 | private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile) |
| 154 | throws Exception { |
| 155 | Command command = new Command(); |
| 156 | command.setTrace(); |
| 157 | command.add(b.getProperty("mvn", "mvn")); |
| 158 | command.add("gpg:sign-and-deploy-file", "-DreleaseInfo=true", "-DpomFile=pom.xml"); |
| 159 | command.add("-Dfile=" + file.getAbsolutePath()); |
| 160 | command.add("-DrepositoryId=" + repository); |
| 161 | command.add("-Durl=" + url); |
| 162 | optional(command, "passphrase", passphrase); |
| 163 | optional(command, "keyname", keyname); |
| 164 | optional(command, "homedir", homedir); |
| 165 | optional(command, "classifier", classifier); |
| 166 | optional(command, "pomFile", pomFile == null ? null : pomFile.getAbsolutePath()); |
| 167 | |
| 168 | StringBuilder stdout = new StringBuilder(); |
| 169 | StringBuilder stderr = new StringBuilder(); |
| 170 | |
| 171 | int result = command.execute(stdout, stderr); |
| 172 | if (result != 0) { |
| 173 | b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, |
| 174 | file, "" + stdout + stderr); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void optional(Command command, String key, String value) { |
| 179 | if (value == null) |
| 180 | return; |
| 181 | |
| 182 | command.add("-D=" + value); |
| 183 | } |
| 184 | |
| 185 | private Jar javadoc(File tmp, Project b, Set<String> exports) throws Exception { |
| 186 | Command command = new Command(); |
| 187 | |
| 188 | command.add(b.getProperty("javadoc", "javadoc")); |
| 189 | command.add("-d"); |
| 190 | command.add(tmp.getAbsolutePath()); |
| 191 | command.add("-sourcepath"); |
| 192 | command.add(Processor.join(b.getSourcePath(), File.pathSeparator)); |
| 193 | |
| 194 | for (String packageName : exports) { |
| 195 | command.add(packageName); |
| 196 | } |
| 197 | |
| 198 | StringBuilder out = new StringBuilder(); |
| 199 | StringBuilder err = new StringBuilder(); |
| 200 | Command c = new Command(); |
| 201 | c.setTrace(); |
| 202 | int result = c.execute(out, err); |
| 203 | if (result == 0) { |
| 204 | Jar jar = new Jar(tmp); |
| 205 | b.addClose(jar); |
| 206 | return jar; |
| 207 | } |
| 208 | b.error("Error during execution of javadoc command: %s / %s", out, err); |
| 209 | return null; |
| 210 | } |
| 211 | |
| 212 | private File write(File base, Resource r, String fileName) throws Exception { |
| 213 | File f = Processor.getFile(base, fileName); |
| 214 | OutputStream out = new FileOutputStream(f); |
| 215 | try { |
| 216 | r.write(out); |
| 217 | } finally { |
| 218 | out.close(); |
| 219 | } |
| 220 | return f; |
| 221 | } |
| 222 | |
| 223 | } |