blob: 10829ca0a75c706780b80a82ba0d75b43e97b753 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.bnd.maven;
2
3import java.io.*;
4import java.util.*;
5import java.util.jar.*;
6
7import aQute.bnd.build.*;
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000010import aQute.libg.command.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +000011import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000012
13public 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 McCulloch7adbc952012-07-12 22:12:58 +000033 System.err.printf("Usage:%n");
Stuart McCullochbb014372012-06-07 21:57:32 +000034 System.err
Stuart McCulloch2286f232012-06-15 13:27:53 +000035 .println(" deploy [-url repo] [-passphrase passphrase] [-homedir homedir] [-keyname keyname] bundle ...");
Stuart McCullochbb014372012-06-07 21:57:32 +000036 System.err.println(" settings");
37 return;
38 }
39
40 /* skip first argument */
41 i++;
Stuart McCulloch2286f232012-06-15 13:27:53 +000042
Stuart McCullochbb014372012-06-07 21:57:32 +000043 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 McCullochbb014372012-06-07 21:57:32 +000057 }
58
Stuart McCulloch2286f232012-06-15 13:27:53 +000059 public void setProperties(Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +000060 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 McCulloch2286f232012-06-15 13:27:53 +000079 Parameters deploy = project.parseHeader(project.getProperty(Constants.DEPLOY));
Stuart McCullochbb014372012-06-07 21:57:32 +000080
Stuart McCulloch2286f232012-06-15 13:27:53 +000081 Map<String,String> maven = deploy.get(repository);
Stuart McCullochbb014372012-06-07 21:57:32 +000082 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 McCulloch2286f232012-06-15 13:27:53 +0000103 Parameters exports = project.parseHeader(manifest.getMainAttributes()
104 .getValue(Constants.EXPORT_PACKAGE));
Stuart McCullochbb014372012-06-07 21:57:32 +0000105 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 McCulloch2286f232012-06-15 13:27:53 +0000123 }
124 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000125 main.close();
126 src.close();
127 }
128 }
129 return true;
130 }
131
132 private void split(Jar original, Jar main, Jar src) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000133 for (Map.Entry<String,Resource> e : original.getResources().entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000134 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 McCulloch2286f232012-06-15 13:27:53 +0000152 private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +0000153 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 McCulloch2286f232012-06-15 13:27:53 +0000171 b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, file, "" + stdout
172 + stderr);
Stuart McCullochbb014372012-06-07 21:57:32 +0000173 }
174 }
175
Stuart McCullochd4826102012-06-26 16:34:24 +0000176 private void optional(Command command, @SuppressWarnings("unused") String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000177 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 McCulloch2286f232012-06-15 13:27:53 +0000215 }
216 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000217 out.close();
218 }
219 return f;
220 }
221
222}