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