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); |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 88 | if (!tmp.exists() && !tmp.mkdirs()) { |
| 89 | throw new IOException("Could not create directory " + tmp); |
| 90 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 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); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 105 | Parameters exports = project.parseHeader(manifest.getMainAttributes() |
| 106 | .getValue(Constants.EXPORT_PACKAGE)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | File jdoc = new File(tmp, "jdoc"); |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 108 | if (!jdoc.exists() && !jdoc.mkdirs()) { |
| 109 | throw new IOException("Could not create directory " + jdoc); |
| 110 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 111 | project.progress("Generating Javadoc for: " + exports.keySet()); |
| 112 | Jar javadoc = javadoc(jdoc, project, exports.keySet()); |
| 113 | project.progress("Writing javadoc jar"); |
| 114 | File javadocFile = write(tmp, new JarResource(javadoc), "javadoc.jar"); |
| 115 | project.progress("Writing main file"); |
| 116 | File mainFile = write(tmp, new JarResource(main), "main.jar"); |
| 117 | project.progress("Writing sources file"); |
| 118 | File srcFile = write(tmp, new JarResource(main), "src.jar"); |
| 119 | |
| 120 | project.progress("Deploying main file"); |
| 121 | maven_gpg_sign_and_deploy(project, mainFile, null, pomFile); |
| 122 | project.progress("Deploying main sources file"); |
| 123 | maven_gpg_sign_and_deploy(project, srcFile, "sources", null); |
| 124 | project.progress("Deploying main javadoc file"); |
| 125 | maven_gpg_sign_and_deploy(project, javadocFile, "javadoc", null); |
| 126 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 127 | } |
| 128 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 129 | main.close(); |
| 130 | src.close(); |
| 131 | } |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | private void split(Jar original, Jar main, Jar src) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 137 | for (Map.Entry<String,Resource> e : original.getResources().entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 138 | String path = e.getKey(); |
| 139 | if (path.startsWith("OSGI-OPT/src/")) { |
| 140 | src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue()); |
| 141 | } else { |
| 142 | main.putResource(path, e.getValue()); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // gpg:sign-and-deploy-file \ |
| 148 | // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2 |
| 149 | // \ |
| 150 | // -DrepositoryId=sonatype-nexus-staging \ |
| 151 | // -DupdateReleaseInfo=true \ |
| 152 | // -DpomFile=pom.xml \ |
| 153 | // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \ |
| 154 | // -Dpassphrase=a1k3v3t5x3 |
| 155 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 156 | 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] | 157 | Command command = new Command(); |
| 158 | command.setTrace(); |
| 159 | command.add(b.getProperty("mvn", "mvn")); |
| 160 | command.add("gpg:sign-and-deploy-file", "-DreleaseInfo=true", "-DpomFile=pom.xml"); |
| 161 | command.add("-Dfile=" + file.getAbsolutePath()); |
| 162 | command.add("-DrepositoryId=" + repository); |
| 163 | command.add("-Durl=" + url); |
| 164 | optional(command, "passphrase", passphrase); |
| 165 | optional(command, "keyname", keyname); |
| 166 | optional(command, "homedir", homedir); |
| 167 | optional(command, "classifier", classifier); |
| 168 | optional(command, "pomFile", pomFile == null ? null : pomFile.getAbsolutePath()); |
| 169 | |
| 170 | StringBuilder stdout = new StringBuilder(); |
| 171 | StringBuilder stderr = new StringBuilder(); |
| 172 | |
| 173 | int result = command.execute(stdout, stderr); |
| 174 | if (result != 0) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 175 | b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, file, "" + stdout |
| 176 | + stderr); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 180 | private void optional(Command command, @SuppressWarnings("unused") String key, String value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 181 | if (value == null) |
| 182 | return; |
| 183 | |
| 184 | command.add("-D=" + value); |
| 185 | } |
| 186 | |
| 187 | private Jar javadoc(File tmp, Project b, Set<String> exports) throws Exception { |
| 188 | Command command = new Command(); |
| 189 | |
| 190 | command.add(b.getProperty("javadoc", "javadoc")); |
| 191 | command.add("-d"); |
| 192 | command.add(tmp.getAbsolutePath()); |
| 193 | command.add("-sourcepath"); |
| 194 | command.add(Processor.join(b.getSourcePath(), File.pathSeparator)); |
| 195 | |
| 196 | for (String packageName : exports) { |
| 197 | command.add(packageName); |
| 198 | } |
| 199 | |
| 200 | StringBuilder out = new StringBuilder(); |
| 201 | StringBuilder err = new StringBuilder(); |
| 202 | Command c = new Command(); |
| 203 | c.setTrace(); |
| 204 | int result = c.execute(out, err); |
| 205 | if (result == 0) { |
| 206 | Jar jar = new Jar(tmp); |
| 207 | b.addClose(jar); |
| 208 | return jar; |
| 209 | } |
| 210 | b.error("Error during execution of javadoc command: %s / %s", out, err); |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | private File write(File base, Resource r, String fileName) throws Exception { |
| 215 | File f = Processor.getFile(base, fileName); |
| 216 | OutputStream out = new FileOutputStream(f); |
| 217 | try { |
| 218 | r.write(out); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 219 | } |
| 220 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 221 | out.close(); |
| 222 | } |
| 223 | return f; |
| 224 | } |
| 225 | |
| 226 | } |