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